repo
stringlengths
5
92
file_url
stringlengths
80
287
file_path
stringlengths
5
197
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:37:27
2026-01-04 17:58:21
truncated
bool
2 classes
grosser/ruco
https://github.com/grosser/ruco/blob/fb4943b0f554d8f68de6d047591bf882af62bb42/lib/ruco/core_ext/file.rb
lib/ruco/core_ext/file.rb
class File def self.write(to, content) File.open(to, 'wb'){|f| f.write(content) } end # Open files in binary mode. On linux this is ignored by ruby. # On Windows ruby open files in text mode by default, so it replaces \r with \n, # so the specs fail. If files are opened in binary mode, which is the only mode # on linux, it does not replace the newlines. This thread has slightly more information: # http://groups.google.com/group/rubyinstaller/browse_thread/thread/c7fbe346831e58cc def self.binary_read(file) io = File.open(file, 'rb') content = io.read io.close content end end
ruby
MIT
fb4943b0f554d8f68de6d047591bf882af62bb42
2026-01-04T17:44:33.904693Z
false
grosser/ruco
https://github.com/grosser/ruco/blob/fb4943b0f554d8f68de6d047591bf882af62bb42/lib/ruco/core_ext/string.rb
lib/ruco/core_ext/string.rb
class String def naive_split(pattern) Dispel::Tools.naive_split(self, pattern) end def tabs_to_spaces! gsub!("\t",' ' * Ruco::TAB_SIZE) end def leading_whitespace match(/^\s*/)[0] end def leading_whitespace=(whitespace) sub!(/^\s*/, whitespace) end # stub for 1.8 unless method_defined?(:force_encoding) def force_encoding(encoding) self end end unless method_defined?(:ord) def ord bytes.first end end def surrounded_in?(*words) first = words.first last = words.last slice(0,first.size) == first and slice(-last.size,last.size) == last end # https://gist.github.com/20844 # remove middle from strings exceeding max length. def ellipsize(options={}) max = options[:max] || 40 delimiter = options[:delimiter] || "..." return self if self.size <= max remainder = max - delimiter.size offset = remainder / 2 (self[0,offset + (remainder.odd? ? 1 : 0)].to_s + delimiter + self[-offset,offset].to_s)[0,max].to_s end unless defined? ellipsize end class String def indexes(needle) Dispel::Tools.indexes(self, needle) end end
ruby
MIT
fb4943b0f554d8f68de6d047591bf882af62bb42
2026-01-04T17:44:33.904693Z
false
grosser/ruco
https://github.com/grosser/ruco/blob/fb4943b0f554d8f68de6d047591bf882af62bb42/lib/ruco/core_ext/object.rb
lib/ruco/core_ext/object.rb
class Object # copy from active_support def delegate(*methods) options = methods.pop unless options.is_a?(Hash) && to = options[:to] raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)." end methods.each do |method| module_eval(<<-EOS, "(__DELEGATION__)", 1) def #{method}(*args, &block) #{to}.__send__(#{method.inspect}, *args, &block) end EOS end end unless defined? delegate end class Object unless defined? instance_exec # 1.9 module InstanceExecMethods #:nodoc: end include InstanceExecMethods # Evaluate the block with the given arguments within the context of # this object, so self is set to the method receiver. # # From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec def instance_exec(*args, &block) begin old_critical, Thread.critical = Thread.critical, true n = 0 n += 1 while respond_to?(method_name = "__instance_exec#{n}") InstanceExecMethods.module_eval { define_method(method_name, &block) } ensure Thread.critical = old_critical end begin send(method_name, *args) ensure InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil end end end end class Object def deep_copy Marshal.load(Marshal.dump(self)) end unless defined? deep_copy end class Object # copy from active_support def silence_warnings with_warnings(nil) { yield } end unless defined? silence_warnings # copy from active_support def with_warnings(flag) old_verbose, $VERBOSE = $VERBOSE, flag yield ensure $VERBOSE = old_verbose end unless defined? with_warnings end # http://grosser.it/2010/07/23/open-uri-without-ssl-https-verification/ module OpenURI def self.without_ssl_verification old = ::OpenSSL::SSL::VERIFY_PEER silence_warnings{ ::OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE } yield ensure silence_warnings{ ::OpenSSL::SSL.const_set :VERIFY_PEER, old } end end class Object def memoize(*names) names.each do |name| unmemoized = "__unmemoized_#{name}" class_eval %{ alias :#{unmemoized} :#{name} private :#{unmemoized} def #{name}(*args) cache = (@#{unmemoized} ||= {}) if cache.has_key?(args) cache[args] else cache[args] = send(:#{unmemoized}, *args).freeze end end } end end # Memoize class methods def cmemoize(*method_names) (class << self; self; end).class_eval do memoize(*method_names) end end end
ruby
MIT
fb4943b0f554d8f68de6d047591bf882af62bb42
2026-01-04T17:44:33.904693Z
false
grosser/ruco
https://github.com/grosser/ruco/blob/fb4943b0f554d8f68de6d047591bf882af62bb42/lib/ruco/core_ext/hash.rb
lib/ruco/core_ext/hash.rb
class Hash # 1.9 does not want index and 1.8 does not have key alias_method(:key, :index) unless method_defined?(:key) # http://www.ruby-forum.com/topic/149449 def slice(*keys, &block) if block each do |key, val| boolean = block.call(key, val) keys << key if boolean end end hash = self keys.inject({}){|returned, key| returned.update key => hash[key]} end def reverse_merge(other) other.merge(self) end end
ruby
MIT
fb4943b0f554d8f68de6d047591bf882af62bb42
2026-01-04T17:44:33.904693Z
false
grosser/ruco
https://github.com/grosser/ruco/blob/fb4943b0f554d8f68de6d047591bf882af62bb42/playground/benchmark_syntax_parser.rb
playground/benchmark_syntax_parser.rb
$LOAD_PATH << 'lib' require 'language_sniffer' gem 'plist' require 'plist' gem 'textpow' require 'textpow' gem 'ultraviolet1x' require 'uv' file = ARGV[0] text = File.read(file) language = LanguageSniffer.detect(file).language require 'ruco/syntax_parser' require 'ruco/array_processor' t = Time.now.to_f Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer]) Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer]) Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer]) Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer]) Ruco::SyntaxParser.syntax_for_lines(text.split("\n"), [language.name.downcase, language.lexer]) puts (Time.now.to_f - t)
ruby
MIT
fb4943b0f554d8f68de6d047591bf882af62bb42
2026-01-04T17:44:33.904693Z
false
adrienkohlbecker/vagrant-fsnotify
https://github.com/adrienkohlbecker/vagrant-fsnotify/blob/2a0b7ac75efdfaf4afa88cffb1eb675686d0098b/lib/vagrant-fsnotify.rb
lib/vagrant-fsnotify.rb
require "vagrant-fsnotify/version" require "vagrant-fsnotify/plugin" module VagrantPlugins module Fsnotify end end
ruby
MIT
2a0b7ac75efdfaf4afa88cffb1eb675686d0098b
2026-01-04T17:44:39.781165Z
false
adrienkohlbecker/vagrant-fsnotify
https://github.com/adrienkohlbecker/vagrant-fsnotify/blob/2a0b7ac75efdfaf4afa88cffb1eb675686d0098b/lib/vagrant-fsnotify/config-fsnotify.rb
lib/vagrant-fsnotify/config-fsnotify.rb
begin require "vagrant" rescue LoadError raise "The vagrant-fsnotify plugin must be run within Vagrant." end if Vagrant::VERSION < "1.7.3" raise <<-ERROR The vagrant-fsnotify plugin is only compatible with Vagrant 1.7.3+. If you can't upgrade, consider installing an old version of vagrant-fsnotify with: $ vagrant plugin install vagrant-fsnotify --plugin-version 0.0.6. ERROR end module VagrantPlugins::Fsnotify class Config < Vagrant.plugin("2", :config) attr_accessor :touch def initialize @touch = UNSET_VALUE end def finalize! @touch = [:modification, :access] if @touch == UNSET_VALUE end end end
ruby
MIT
2a0b7ac75efdfaf4afa88cffb1eb675686d0098b
2026-01-04T17:44:39.781165Z
false
adrienkohlbecker/vagrant-fsnotify
https://github.com/adrienkohlbecker/vagrant-fsnotify/blob/2a0b7ac75efdfaf4afa88cffb1eb675686d0098b/lib/vagrant-fsnotify/version.rb
lib/vagrant-fsnotify/version.rb
module VagrantPlugins module Fsnotify VERSION = "0.3.2" end end
ruby
MIT
2a0b7ac75efdfaf4afa88cffb1eb675686d0098b
2026-01-04T17:44:39.781165Z
false
adrienkohlbecker/vagrant-fsnotify
https://github.com/adrienkohlbecker/vagrant-fsnotify/blob/2a0b7ac75efdfaf4afa88cffb1eb675686d0098b/lib/vagrant-fsnotify/command-fsnotify.rb
lib/vagrant-fsnotify/command-fsnotify.rb
require 'listen' module VagrantPlugins::Fsnotify class Command < Vagrant.plugin("2", :command) include Vagrant::Action::Builtin::MixinSyncedFolders def self.synopsis 'forwards filesystem events to virtual machine' end def execute @logger = Log4r::Logger.new("vagrant::commands::fsnotify") params = OptionParser.new do |o| o.banner = "Usage: vagrant fsnotify [vm-name]" o.separator "" end argv = parse_options(params) return if !argv paths = {} ignores = [] @changes = {} with_target_vms(argv) do |machine| if !machine.communicate.ready? machine.ui.error("Machine not ready, is it up?") return 1 end synced_folders(machine).each do |type, folder| folder.each do |id, opts| if !( (opts[:fsnotify] == true) || ( opts[:fsnotify].respond_to?(:include?) && ( opts[:fsnotify].include?(:modified) || opts[:fsnotify].include?(:added) || opts[:fsnotify].include?(:removed) ) ) ) next end # Folder info hostpath = opts[:hostpath] hostpath = File.expand_path(hostpath, machine.env.root_path) hostpath = Vagrant::Util::Platform.fs_real_path(hostpath).to_s # Make sure the host path ends with a "/" to avoid creating # a nested directory... if !hostpath.end_with?("/") hostpath += "/" end machine.ui.info("fsnotify: Watching #{hostpath}") paths[hostpath] = { id: id, machine: machine, opts: opts } if opts[:exclude] Array(opts[:exclude]).each do |pattern| ignores << exclude_to_regexp(pattern.to_s) end end end end end if paths.empty? @env.ui.info(<<-MESSAGE) Nothing to sync. Note that the valid values for the `:fsnotify' configuration key on `Vagrantfile' are either `true' (which forwards all kinds of filesystem events) or an Array containing symbols among the following options: `:modified', `:added' and `:removed' (in which case, only the specified filesystem events are forwarded). For example, to forward all filesystem events to the default `/vagrant' folder, add the following to the `Vagrantfile': config.vm.synced_folder ".", "/vagrant", fsnotify: true And to forward only added files events to the default `/vagrant' folder, add the following to the `Vagrantfile': config.vm.synced_folder ".", "/vagrant", fsnotify: [:added] Exiting... MESSAGE return 1 end @logger.info("Listening to paths: #{paths.keys.sort.inspect}") @logger.info("Listening via: #{Listen::Adapter.select.inspect}") @logger.info("Ignoring #{ignores.length} paths:") ignores.each do |ignore| @logger.info(" -- #{ignore.to_s}") end listener_callback = method(:callback).to_proc.curry[paths] listener = Listen.to(*paths.keys, ignore: ignores, &listener_callback) # Create the callback that lets us know when we've been interrupted queue = Queue.new callback = lambda do # This needs to execute in another thread because Thread # synchronization can't happen in a trap context. Thread.new { queue << true } end # Run the listener in a busy block so that we can cleanly # exit once we receive an interrupt. Vagrant::Util::Busy.busy(callback) do listener.start queue.pop listener.stop if listener.state != :stopped end return 0 end def callback(paths, modified, added, removed) @logger.info("File change callback called!") @logger.info(" - Modified: #{modified.inspect}") @logger.info(" - Added: #{added.inspect}") @logger.info(" - Removed: #{removed.inspect}") @changes.each do |rel_path, time| @changes.delete(rel_path) if time < Time.now.to_i - 2 end tosync = {} todelete = [] paths.each do |hostpath, folder| toanalyze = [] if folder[:opts][:fsnotify] == true toanalyze += modified + added + removed else if folder[:opts][:fsnotify].include? :modified toanalyze += modified end if folder[:opts][:fsnotify].include? :added toanalyze += added end if folder[:opts][:fsnotify].include? :removed toanalyze += removed end end toanalyze.each do |file| if file.start_with?(hostpath) rel_path = file.sub(hostpath, '') if @changes[rel_path] && @changes[rel_path] >= Time.now.to_i - 2 @logger.info("#{rel_path} was changed less than two seconds ago, skipping") next end @changes[rel_path] = Time.now.to_i if modified.include? file folder[:machine].ui.info("fsnotify: Changed: #{rel_path}") elsif added.include? file folder[:machine].ui.info("fsnotify: Added: #{rel_path}") elsif removed.include? file folder[:machine].ui.info("fsnotify: Removed: #{rel_path}") end guestpath = folder[:opts][:override_guestpath] || folder[:opts][:guestpath] guestpath = File.join(guestpath, rel_path) tosync[folder[:machine]] = [] if !tosync.has_key?(folder[:machine]) tosync[folder[:machine]] << guestpath if removed.include? file todelete << guestpath end end end end tosync.each do |machine, files| touch_flags = get_touch_flags(machine.config.fsnotify.touch) machine.communicate.execute("touch -#{touch_flags} '#{files.join("' '")}'") remove_from_this_machine = files & todelete unless remove_from_this_machine.empty? machine.communicate.execute("rm -rf '#{remove_from_this_machine.join("' '")}'") end end rescue => e @logger.error("#{e}: #{e.message}") end def exclude_to_regexp(exclude) # This is REALLY ghetto, but its a start. We can improve and # keep unit tests passing in the future. exclude = exclude.gsub("**", "|||GLOBAL|||") exclude = exclude.gsub("*", "|||PATH|||") exclude = exclude.gsub("|||PATH|||", "[^/]*") exclude = exclude.gsub("|||GLOBAL|||", ".*") Regexp.new(exclude) end def get_touch_flags(touch) if touch.include? :modification and not touch.include? :access return "m" elsif not touch.include? :modification and touch.include? :access return "a" else return "am" end end end end
ruby
MIT
2a0b7ac75efdfaf4afa88cffb1eb675686d0098b
2026-01-04T17:44:39.781165Z
false
adrienkohlbecker/vagrant-fsnotify
https://github.com/adrienkohlbecker/vagrant-fsnotify/blob/2a0b7ac75efdfaf4afa88cffb1eb675686d0098b/lib/vagrant-fsnotify/plugin.rb
lib/vagrant-fsnotify/plugin.rb
begin require "vagrant" rescue LoadError raise "The vagrant-fsnotify plugin must be run within Vagrant." end if Vagrant::VERSION < "1.7.3" raise <<-ERROR The vagrant-fsnotify plugin is only compatible with Vagrant 1.7.3+. If you can't upgrade, consider installing an old version of vagrant-fsnotify with: $ vagrant plugin install vagrant-fsnotify --plugin-version 0.0.6. ERROR end module VagrantPlugins::Fsnotify class Plugin < Vagrant.plugin("2") name "vagrant-fsnotify" command "fsnotify" do require_relative "command-fsnotify" Command end config "fsnotify" do require_relative "config-fsnotify" Config end end end
ruby
MIT
2a0b7ac75efdfaf4afa88cffb1eb675686d0098b
2026-01-04T17:44:39.781165Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/spec_helper.rb
spec/spec_helper.rb
if RUBY_ENGINE == "rbx" require "codeclimate-test-reporter" CodeClimate::TestReporter.start end require 'coercible' require 'rspec/its' include Coercible ENV['TZ'] = 'UTC' Dir[File.expand_path('../shared/**/*.rb', __FILE__)].each { |file| require file } Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file } # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # Require this file using `require "spec_helper"` to ensure that it is only # loaded once. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.filter_run :focus # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 config.order = 'random' end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/support/mutant.rb
spec/support/mutant.rb
module Mutant class Subject def tests config.integration.all_tests end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/integration/configuring_coercers_spec.rb
spec/integration/configuring_coercers_spec.rb
require 'spec_helper' describe "Configuring coercers" do it "allows to configure coercers" do coercer = Coercer.new do |config| config.string.boolean_map = { 'yup' => true, 'nope' => false } end expect(coercer[String].to_boolean('yup')).to be(true) expect(coercer[String].to_boolean('nope')).to be(false) expect { coercer[String].to_boolean('1') }.to raise_error end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/configuration/class_methods/build_spec.rb
spec/unit/coercible/configuration/class_methods/build_spec.rb
require 'spec_helper' describe Configuration, '.build' do subject { described_class.build(keys) } let(:keys) { [ :foo, :bar ] } it { is_expected.to be_instance_of(described_class) } it { is_expected.to respond_to(:foo) } it { is_expected.to respond_to(:foo=) } it { is_expected.to respond_to(:bar) } it { is_expected.to respond_to(:bar=) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string_spec.rb
spec/unit/coercible/coercer/string_spec.rb
require 'spec_helper' describe Coercer::String do it_behaves_like 'Configurable' do describe '.config_name' do subject { described_class.config_name } it { is_expected.to be(:string) } end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/element_reader_spec.rb
spec/unit/coercible/coercer/element_reader_spec.rb
require 'spec_helper' describe Coercer, '#[]' do subject { object[type] } let(:object) { described_class.new } context "with a known type" do let(:type) { ::String } it { is_expected.to be_instance_of(Coercer::String) } end context "with an unknown type" do let(:type) { Object } it { is_expected.to be_instance_of(Coercer::Object) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer_spec.rb
spec/unit/coercible/coercer/integer_spec.rb
require 'spec_helper' describe Coercer::Integer do it_behaves_like 'Configurable' do describe '.config_name' do subject { described_class.config_name } it { is_expected.to be(:integer) } end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/coerced_predicate_spec.rb
spec/unit/coercible/coercer/string/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::String, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { 'a string' } let(:non_primitive_value) { :a_symbol } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_integer_spec.rb
spec/unit/coercible/coercer/string/to_integer_spec.rb
require 'spec_helper' describe Coercer::String, '.to_integer' do subject { described_class.new.to_integer(string) } min_float = Float::MIN max_float = (Float::MAX / 10).to_s.to_f # largest float that can be parsed { '1' => 1, '+1' => 1, '-1' => -1, '1.0' => 1, '1.0e+1' => 10, '1.0e-1' => 0, '1.0E+1' => 10, '1.0E-1' => 0, '+1.0' => 1, '+1.0e+1' => 10, '+1.0e-1' => 0, '+1.0E+1' => 10, '+1.0E-1' => 0, '-1.0' => -1, '-1.0e+1' => -10, '-1.0e-1' => 0, '-1.0E+1' => -10, '-1.0E-1' => 0, '.1' => 0, '.1e+1' => 1, '.1e-1' => 0, '.1E+1' => 1, '.1E-1' => 0, '1e1' => 10, '1E+1' => 10, '+1e-1' => 0, '-1E1' => -10, '-1e-1' => 0, min_float.to_s => min_float.to_i, max_float.to_s => max_float.to_i, }.each do |value, expected| context "with #{value.inspect}" do let(:string) { value } it { is_expected.to be_kind_of(Integer) } it { is_expected.to eql(expected) } end end context 'with an invalid integer string' do let(:string) { 'non-integer' } specify { expect { subject }.to raise_error(UnsupportedCoercion) } end context 'when integer string is big' do let(:string) { '334490140000101135' } it { is_expected.to eq(334490140000101135) } end context 'string starts with e' do let(:string) { 'e1' } specify { expect { subject }.to raise_error(UnsupportedCoercion) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_datetime_spec.rb
spec/unit/coercible/coercer/string/to_datetime_spec.rb
require 'spec_helper' shared_examples_for 'a correct datetime object' do it { is_expected.to be_instance_of(DateTime) } its(:year) { should == year } its(:month) { should == month } its(:day) { should == day } its(:hour) { should == hour } its(:min) { should == min } its(:sec) { should == sec } end describe Coercer::String, '.to_datetime' do subject { object.to_datetime(string) } let(:object) { described_class.new } context 'with a valid date string' do let(:year) { 2011 } let(:month) { 7 } let(:day) { 22 } context 'not including time part' do let(:string) { "July, #{day}th, #{year}" } let(:hour) { 0 } let(:min) { 0 } let(:sec) { 0 } it_should_behave_like 'a correct datetime object' end context 'including time part' do let(:string) { "July, #{day}, #{year}, #{hour}:#{min}:#{sec}" } let(:hour) { 13 } let(:min) { 44 } let(:sec) { 50 } it_should_behave_like 'a correct datetime object' end end context 'with an invalid date time string' do let(:string) { 'non-datetime' } specify { expect { subject }.to raise_error(UnsupportedCoercion) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_time_spec.rb
spec/unit/coercible/coercer/string/to_time_spec.rb
require 'spec_helper' shared_examples_for 'a correct time object' do it { is_expected.to be_instance_of(Time) } its(:year) { should == year } its(:month) { should == month } its(:day) { should == day } its(:hour) { should == hour } its(:min) { should == min } its(:sec) { should == sec } end describe Coercer::String, '.to_time' do subject { object.to_time(string) } let(:object) { described_class.new } context 'with a valid time string' do let(:year) { 2011 } let(:month) { 7 } let(:day) { 22 } context 'not including time part' do let(:string) { "July, #{day}th, #{year}" } let(:hour) { 0 } let(:min) { 0 } let(:sec) { 0 } it_should_behave_like 'a correct time object' end context 'including time part' do let(:string) { "July, #{day}, #{year}, #{hour}:#{min}:#{sec}" } let(:hour) { 13 } let(:min) { 44 } let(:sec) { 50 } it_should_behave_like 'a correct time object' end end context 'with an invalid date time string' do let(:string) { '2999' } specify do expect { subject }.to raise_error(UnsupportedCoercion) end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_boolean_spec.rb
spec/unit/coercible/coercer/string/to_boolean_spec.rb
require 'spec_helper' describe Coercer::String, '.to_boolean' do subject { object.to_boolean(string) } let(:object) { described_class.new } %w[ 1 on ON t true T TRUE y yes Y YES ].each do |value| context "with #{value.inspect}" do let(:string) { value } it { is_expected.to be(true) } end end %w[ 0 off OFF f false F FALSE n no N NO ].each do |value| context "with #{value.inspect}" do let(:string) { value } it { is_expected.to be(false) } end end context 'with an invalid boolean string' do let(:string) { 'non-boolean' } specify do expect { subject }.to raise_error end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_date_spec.rb
spec/unit/coercible/coercer/string/to_date_spec.rb
require 'spec_helper' describe Coercer::String, '.to_date' do subject { object.to_date(string) } let(:object) { described_class.new } context 'with a valid date string' do let(:string) { 'July, 22th, 2011' } it { is_expected.to be_instance_of(Date) } its(:year) { should == 2011 } its(:month) { should == 7 } its(:day) { should == 22 } end context 'with an invalid date string' do let(:string) { 'non-date' } specify do expect { subject }.to raise_error(UnsupportedCoercion) end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_decimal_spec.rb
spec/unit/coercible/coercer/string/to_decimal_spec.rb
require 'spec_helper' describe Coercer::String, '.to_decimal' do subject { object.to_decimal(string) } let(:object) { described_class.new } { '1' => BigDecimal('1.0'), '+1' => BigDecimal('1.0'), '-1' => BigDecimal('-1.0'), '1.0' => BigDecimal('1.0'), '1.0e+1' => BigDecimal('10.0'), '1.0e-1' => BigDecimal('0.1'), '1.0E+1' => BigDecimal('10.0'), '1.0E-1' => BigDecimal('0.1'), '+1.0' => BigDecimal('1.0'), '+1.0e+1' => BigDecimal('10.0'), '+1.0e-1' => BigDecimal('0.1'), '+1.0E+1' => BigDecimal('10.0'), '+1.0E-1' => BigDecimal('0.1'), '-1.0' => BigDecimal('-1.0'), '-1.0e+1' => BigDecimal('-10.0'), '-1.0e-1' => BigDecimal('-0.1'), '-1.0E+1' => BigDecimal('-10.0'), '-1.0E-1' => BigDecimal('-0.1'), '.1' => BigDecimal('0.1'), '.1e+1' => BigDecimal('1.0'), '.1e-1' => BigDecimal('0.01'), '.1E+1' => BigDecimal('1.0'), '.1E-1' => BigDecimal('0.01'), }.each do |value, expected| context "with #{value.inspect}" do let(:string) { value } it { is_expected.to be_instance_of(BigDecimal) } it { is_expected.to eql(expected) } end end context 'with an invalid decimal string' do let(:string) { 'non-decimal' } specify { expect { subject }.to raise_error(UnsupportedCoercion) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_float_spec.rb
spec/unit/coercible/coercer/string/to_float_spec.rb
require 'spec_helper' describe Coercer::String, '.to_float' do subject { described_class.new.to_float(string) } { '1' => 1.0, '+1' => 1.0, '-1' => -1.0, '1.0' => 1.0, '1.0e+1' => 10.0, '1.0e-1' => 0.1, '1.0E+1' => 10.0, '1.0E-1' => 0.1, '+1.0' => 1.0, '+1.0e+1' => 10.0, '+1.0e-1' => 0.1, '+1.0E+1' => 10.0, '+1.0E-1' => 0.1, '-1.0' => -1.0, '-1.0e+1' => -10.0, '-1.0e-1' => -0.1, '-1.0E+1' => -10.0, '-1.0E-1' => -0.1, '.1' => 0.1, '.1e+1' => 1.0, '.1e-1' => 0.01, '.1E+1' => 1.0, '.1E-1' => 0.01, '1e1' => 10.0, '1E+1' => 10.0, '+1e-1' => 0.1, '-1E1' => -10.0, '-1e-1' => -0.1, }.each do |value, expected| context "with #{value.inspect}" do let(:string) { value } it { is_expected.to be_instance_of(Float) } it { is_expected.to eql(expected) } end end context 'with an invalid float string' do let(:string) { 'non-float' } specify { expect { subject }.to raise_error(UnsupportedCoercion) } end context 'string starts with e' do let(:string) { 'e1' } specify { expect { subject }.to raise_error(UnsupportedCoercion) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_constant_spec.rb
spec/unit/coercible/coercer/string/to_constant_spec.rb
require 'spec_helper' describe Coercer::String, '.to_constant' do subject { object.to_constant(string) } let(:object) { described_class.new } context 'with a non-namespaced name' do let(:string) { 'String' } it { is_expected.to be(String) } end context 'with a non-namespaced qualified name' do let(:string) { '::String' } it { is_expected.to be(String) } end context 'with a namespaced name' do let(:string) { 'Coercible::Coercer::String' } it { is_expected.to be(Coercer::String) } end context 'with a namespaced qualified name' do let(:string) { '::Coercible::Coercer::String' } it { is_expected.to be(Coercer::String) } end context 'with a name outside of the namespace' do let(:string) { 'Virtus::Object' } specify { expect { subject }.to raise_error(NameError) } end context 'when the name is unknown' do let(:string) { 'Unknown' } specify { expect { subject }.to raise_error(NameError) } end context 'when the name is invalid' do let(:string) { 'invalid' } specify { expect { subject }.to raise_error(NameError) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/to_symbol_spec.rb
spec/unit/coercible/coercer/string/to_symbol_spec.rb
require 'spec_helper' describe Coercer::String, '.to_symbol' do subject { described_class.new.to_symbol(value) } let(:value) { 'value' } it { is_expected.to be(:value) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/string/class_methods/config_spec.rb
spec/unit/coercible/coercer/string/class_methods/config_spec.rb
require 'spec_helper' describe Coercer::String, '.config' do subject { described_class.config } its(:boolean_map) { should be(described_class::BOOLEAN_MAP) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/configurable/config_name_spec.rb
spec/unit/coercible/coercer/configurable/config_name_spec.rb
require 'spec_helper' describe Coercer::Configurable, '.config_name' do subject { object.config_name } let(:object) { Class.new { extend Coercer::Configurable, Options config_keys [ :one, :two ] def self.name "Some::Class::Test" end } } it { is_expected.to be(:test) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/configurable/config_spec.rb
spec/unit/coercible/coercer/configurable/config_spec.rb
require 'spec_helper' describe Coercer::Configurable, '.config' do subject { object.config(&block) } let(:object) { Class.new { extend Coercer::Configurable, Options config_keys [ :one, :two ] } } let(:block) { Proc.new { |config| config.test } } let(:configuration) { double('configuration') } let(:configuration_class) { double('configuration_class') } before do allow(object).to receive_messages(:configuration_class => configuration_class) expect(configuration_class).to receive(:build).with(object.config_keys). and_return(configuration) expect(configuration).to receive(:test) end it { is_expected.to be(configuration) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/hash/coerced_predicate_spec.rb
spec/unit/coercible/coercer/hash/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Hash, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { Hash.new } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/hash/to_datetime_spec.rb
spec/unit/coercible/coercer/hash/to_datetime_spec.rb
require 'spec_helper' describe Coercer::Hash, '.to_datetime' do subject { object.to_datetime(hash) } let(:object) { described_class.new } context 'when time segments are missing' do let(:time_now) { Time.local(2011, 1, 1) } let(:hash) { {} } before do allow(Time).to receive(:now).and_return(time_now) # freeze time end it { is_expected.to be_instance_of(DateTime) } it 'uses the Time now to populate the segments' do is_expected.to eql(DateTime.new(2011, 1, 1)) end end context 'when time segments are integers' do let(:hash) { { :year => 2011, :month => 1, :day => 1, :hour => 1, :min => 1, :sec => 1 } } it { is_expected.to be_instance_of(DateTime) } it { is_expected.to eql(DateTime.new(2011, 1, 1, 1, 1, 1)) } end context 'when time segments are strings' do let(:hash) { { :year => '2011', :month => '1', :day => '1', :hour => '1', :min => '1', :sec => '1' } } it { is_expected.to be_instance_of(DateTime) } it { is_expected.to eql(DateTime.new(2011, 1, 1, 1, 1, 1)) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/hash/to_time_spec.rb
spec/unit/coercible/coercer/hash/to_time_spec.rb
require 'spec_helper' describe Coercer::Hash, '.to_time' do subject { object.to_time(hash) } let(:object) { described_class.new } context 'when time segments are missing' do let(:time_now) { Time.local(2011, 1, 1) } let(:hash) { {} } before do allow(Time).to receive(:now).and_return(time_now) # freeze time end it { is_expected.to be_instance_of(Time) } it 'uses the Time now to populate the segments' do is_expected.to eql(time_now) end end context 'when time segments are integers' do let(:hash) { { :year => 2011, :month => 1, :day => 1, :hour => 1, :min => 1, :sec => 1 } } it { is_expected.to be_instance_of(Time) } it { is_expected.to eql(Time.local(2011, 1, 1, 1, 1, 1)) } end context 'when time segments are strings' do let(:hash) { { :year => '2011', :month => '1', :day => '1', :hour => '1', :min => '1', :sec => '1' } } it { is_expected.to be_instance_of(Time) } it { is_expected.to eql(Time.local(2011, 1, 1, 1, 1, 1)) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/hash/to_date_spec.rb
spec/unit/coercible/coercer/hash/to_date_spec.rb
require 'spec_helper' describe Coercer::Hash, '.to_date' do subject { object.to_date(hash) } let(:object) { described_class.new } context 'when time segments are missing' do let(:time_now) { Time.local(2011, 1, 1) } let(:hash) { {} } before do allow(Time).to receive(:now).and_return(time_now) # freeze time end it { is_expected.to be_instance_of(Date) } it 'uses the Time now to populate the segments' do is_expected.to eql(Date.new(2011, 1, 1)) end end context 'when time segments are integers' do let(:hash) { { :year => 2011, :month => 1, :day => 1 } } it { is_expected.to be_instance_of(Date) } it { is_expected.to eql(Date.new(2011, 1, 1)) } end context 'when time segments are strings' do let(:hash) { { :year => '2011', :month => '1', :day => '1' } } it { is_expected.to be_instance_of(Date) } it { is_expected.to eql(Date.new(2011, 1, 1)) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/true_class/coerced_predicate_spec.rb
spec/unit/coercible/coercer/true_class/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::TrueClass, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { true } let(:non_primitive_value) { false } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/true_class/to_string_spec.rb
spec/unit/coercible/coercer/true_class/to_string_spec.rb
require 'spec_helper' describe Coercer::TrueClass, '.to_string' do subject { object.to_string(true_class) } let(:object) { described_class.new } let(:true_class) { true } it { is_expected.to be_instance_of(String) } it { is_expected.to eql('true') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date/coerced_predicate_spec.rb
spec/unit/coercible/coercer/date/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Date, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { Date.new } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date/to_datetime_spec.rb
spec/unit/coercible/coercer/date/to_datetime_spec.rb
require 'spec_helper' describe Coercer::Date, '.to_datetime' do subject { object.to_datetime(date) } let(:object) { described_class.new } let(:date) { Date.new(2011, 1, 1) } context 'when Date does not support #to_datetime' do if RUBY_VERSION < '1.9' before do expect(date).not_to respond_to(:to_datetime) end end it { is_expected.to be_instance_of(DateTime) } it { is_expected.to eql(DateTime.new(2011, 1, 1)) } end context 'when Date supports #to_datetime' do let(:datetime) { DateTime.new(2011, 1, 1) } before do allow(date).to receive(:to_datetime).and_return(datetime) end it { is_expected.to equal(datetime) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date/to_time_spec.rb
spec/unit/coercible/coercer/date/to_time_spec.rb
require 'spec_helper' describe Coercer::Date, '.to_time' do subject { object.to_time(date) } let(:object) { described_class.new } let(:date) { Date.new(2011, 1, 1) } it { is_expected.to be_instance_of(Time) } it { is_expected.to eql(Time.local(2011, 1, 1)) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date/to_date_spec.rb
spec/unit/coercible/coercer/date/to_date_spec.rb
require 'spec_helper' describe Coercer::Date, '.to_date' do subject { object.to_date(date) } let(:object) { described_class.new } let(:date) { Date.new(2012, 1, 1) } it { is_expected.to equal(date) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date/to_string_spec.rb
spec/unit/coercible/coercer/date/to_string_spec.rb
require 'spec_helper' describe Coercer::Date, '.to_string' do subject { object.to_string(date) } let(:object) { described_class.new } let(:date) { Date.new(2011, 1, 1) } it { is_expected.to be_instance_of(String) } it { is_expected.to eql('2011-01-01') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/float/coerced_predicate_spec.rb
spec/unit/coercible/coercer/float/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Float, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { 1.2 } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/float/to_integer_spec.rb
spec/unit/coercible/coercer/float/to_integer_spec.rb
require 'spec_helper' describe Coercer::Float, '.to_integer' do subject { object.to_integer(float) } let(:object) { described_class.new } let(:float) { 1.0 } it { is_expected.to be_kind_of(Integer) } it { is_expected.to eql(1) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/float/to_datetime_spec.rb
spec/unit/coercible/coercer/float/to_datetime_spec.rb
require 'spec_helper' describe Coercer::Float, '#to_float' do subject { object.to_datetime(value) } let(:object) { described_class.new } let(:value) { 1361036672.12 } specify do expect(subject.strftime('%Y-%m-%d %H:%M:%S.%L')).to eql('2013-02-16 17:44:32.120') end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/float/to_decimal_spec.rb
spec/unit/coercible/coercer/float/to_decimal_spec.rb
require 'spec_helper' describe Coercer::Float, '.to_decimal' do subject { object.to_decimal(float) } let(:object) { described_class.new } let(:float) { 1.0 } it { is_expected.to be_instance_of(BigDecimal) } it { is_expected.to eql(BigDecimal('1.0')) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/float/to_string_spec.rb
spec/unit/coercible/coercer/float/to_string_spec.rb
require 'spec_helper' describe Coercer::Float, '.to_string' do subject { object.to_string(float) } let(:object) { described_class.new } let(:float) { 1.0 } it { is_expected.to be_instance_of(String) } it { is_expected.to eql('1.0') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/float/to_float_spec.rb
spec/unit/coercible/coercer/float/to_float_spec.rb
require 'spec_helper' describe Coercer::Float, '.to_float' do subject { described_class.new.to_float(value) } let(:value) { 1.0 } it { is_expected.to be(value) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/class_methods/new_spec.rb
spec/unit/coercible/coercer/class_methods/new_spec.rb
require 'spec_helper' describe Coercer, '.new' do subject { described_class.new(&block) } let(:block) { Proc.new {} } it { is_expected.to be_instance_of(Coercer) } its(:config) { should be_instance_of(Coercible::Configuration) } its(:config) { should respond_to(:string) } its(:config) { should respond_to(:string=) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/coerced_predicate_spec.rb
spec/unit/coercible/coercer/integer/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Integer, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { 1 } let(:non_primitive_value) { 1.0 } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/to_integer_spec.rb
spec/unit/coercible/coercer/integer/to_integer_spec.rb
require 'spec_helper' describe Coercer::Integer, '.to_integer' do subject { described_class.new.to_integer(value) } let(:value) { 1 } it { is_expected.to be(value) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/to_datetime_spec.rb
spec/unit/coercible/coercer/integer/to_datetime_spec.rb
require 'spec_helper' describe Coercer::Integer, '#to_datetime' do subject { object.to_datetime(value) } let(:object) { described_class.new } let(:value) { 1361036672 } specify do expect(subject.strftime('%Y-%m-%d %H:%M:%S.%L')).to eql('2013-02-16 17:44:32.000') end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/datetime_format_spec.rb
spec/unit/coercible/coercer/integer/datetime_format_spec.rb
require 'spec_helper' describe Coercer::Integer, '#datetime_format' do subject { object.datetime_format } let(:object) { described_class.new } context "with Rubinius" do before do unless Coercible.rbx? allow(Coercible).to receive_messages(:rbx? => true) end end it { is_expected.to eq('%Q') } end context "with other Ruby VMs" do before do if Coercible.rbx? allow(Coercible).to receive_messages(:rbx? => false) end end it { is_expected.to eq('%s') } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/to_boolean_spec.rb
spec/unit/coercible/coercer/integer/to_boolean_spec.rb
require 'spec_helper' describe Coercer::Integer, '.to_boolean' do subject { object.to_boolean(fixnum) } let(:object) { described_class.new } context 'when the fixnum is 1' do let(:fixnum) { 1 } it { is_expected.to be(true) } end context 'when the fixnum is 0' do let(:fixnum) { 0 } it { is_expected.to be(false) } end context 'when the fixnum is not 1 or 0' do let(:fixnum) { -1 } specify do expect { subject }.to raise_error(UnsupportedCoercion) end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/datetime_proc_spec.rb
spec/unit/coercible/coercer/integer/datetime_proc_spec.rb
require 'spec_helper' describe Coercer::Integer, '#datetime_proc' do subject { object.datetime_proc } let(:object) { described_class.new } context "with Rubinius" do before do unless Coercible.rbx? allow(Coercible).to receive_messages(:rbx? => true) end end it { is_expected.to be_instance_of(Proc) } end context "with other Ruby VMs" do it { is_expected.to be_instance_of(Proc) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/to_decimal_spec.rb
spec/unit/coercible/coercer/integer/to_decimal_spec.rb
require 'spec_helper' describe Coercer::Integer, '.to_decimal' do subject { object.to_decimal(fixnum) } let(:object) { described_class.new } let(:fixnum) { 1 } it { is_expected.to be_instance_of(BigDecimal) } it { is_expected.to eql(BigDecimal('1.0')) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/to_string_spec.rb
spec/unit/coercible/coercer/integer/to_string_spec.rb
require 'spec_helper' describe Coercer::Integer, '.to_string' do subject { object.to_string(integer) } let(:object) { described_class.new } let(:integer) { 1 } it { is_expected.to be_instance_of(String) } it { is_expected.to eql('1') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/integer/to_float_spec.rb
spec/unit/coercible/coercer/integer/to_float_spec.rb
require 'spec_helper' describe Coercer::Integer, '.to_float' do subject { object.to_float(fixnum) } let(:object) { described_class.new } let(:fixnum) { 1 } it { is_expected.to be_instance_of(Float) } it { is_expected.to eql(1.0) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/false_class/coerced_predicate_spec.rb
spec/unit/coercible/coercer/false_class/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::FalseClass, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { false } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/false_class/to_string_spec.rb
spec/unit/coercible/coercer/false_class/to_string_spec.rb
require 'spec_helper' describe Coercer::FalseClass, '.to_string' do subject { object.to_string(false_class) } let(:object) { described_class.new } let(:false_class) { false } it { is_expected.to be_instance_of(String) } it { is_expected.to eql('false') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/time_coercions/to_datetime_spec.rb
spec/unit/coercible/coercer/time_coercions/to_datetime_spec.rb
require 'spec_helper' describe Coercer::TimeCoercions, '.to_datetime' do subject { object.to_datetime(value) } let(:object) { coercer.new } let(:coercer) { Class.new(Coercer::Object) { include Coercer::TimeCoercions } } let(:value) { double('value') } after do Coercer::Object.descendants.delete(coercer) end context 'when the value responds to #to_datetime' do before do object.extend Coercer::TimeCoercions expect(value).to receive(:to_datetime).and_return(DateTime.new(2011, 1, 1, 0, 0, 0)) end it { is_expected.to be_instance_of(DateTime) } it { is_expected.to eql(DateTime.new(2011, 1, 1, 0, 0, 0)) } end context 'when the value does not respond to #to_datetime' do before do object.extend Coercer::TimeCoercions # use a string that DateTime.parse can handle expect(value).to receive(:to_s).and_return('2011-01-01T00:00:00+00:00') end it { is_expected.to be_instance_of(DateTime) } it { is_expected.to eql(DateTime.new(2011, 1, 1, 0, 0, 0)) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/time_coercions/to_time_spec.rb
spec/unit/coercible/coercer/time_coercions/to_time_spec.rb
require 'spec_helper' describe Coercer::TimeCoercions, '.to_time' do subject { object.to_time(value) } let(:object) { coercer.new } let(:coercer) { Class.new(Coercer::Object) { include Coercer::TimeCoercions } } let(:value) { double('value') } after do Coercer::Object.descendants.delete(coercer) end context 'when the value responds to #to_time' do before do object.extend Coercer::TimeCoercions expect(value).to receive(:to_time).and_return(Time.utc(2011, 1, 1)) end it { is_expected.to be_instance_of(Time) } it { is_expected.to eql(Time.utc(2011, 1, 1)) } end context 'when the value does not respond to #to_time' do before do object.extend Coercer::TimeCoercions # use a string that Time.parse can handle expect(value).to receive(:to_s).and_return('Sat Jan 01 00:00:00 UTC 2011') end it { is_expected.to be_instance_of(Time) } it { is_expected.to eql(Time.utc(2011, 1, 1)) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/time_coercions/to_date_spec.rb
spec/unit/coercible/coercer/time_coercions/to_date_spec.rb
require 'spec_helper' describe Coercer::TimeCoercions, '.to_date' do subject { object.to_date(value) } let(:object) { coercer.new } let(:coercer) { Class.new(Coercer::Object) { include Coercer::TimeCoercions } } let(:value) { double('value') } after do Coercer::Object.descendants.delete(coercer) end context 'when the value responds to #to_date' do before do expect(value).to receive(:to_date).and_return(Date.new(2011, 1, 1)) end it { is_expected.to be_instance_of(Date) } it { is_expected.to eql(Date.new(2011, 1, 1)) } end context 'when the value does not respond to #to_date' do before do # use a string that Date.parse can handle expect(value).to receive(:to_s).and_return('2011-01-01') end it { is_expected.to be_instance_of(Date) } it { is_expected.to eql(Date.new(2011, 1, 1)) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/time_coercions/to_string_spec.rb
spec/unit/coercible/coercer/time_coercions/to_string_spec.rb
require 'spec_helper' describe Coercer::TimeCoercions, '.to_string' do subject { object.to_string(value) } let(:object) { coercer.new } let(:coercer) { Class.new(Coercer::Object) { include Coercer::TimeCoercions } } let(:value) { double('value') } after do Coercer::Object.descendants.delete(coercer) end before do object.extend Coercer::TimeCoercions expect(value).to receive(:to_s).and_return('2011-01-01') end it { is_expected.to be_instance_of(String) } it { is_expected.to eql('2011-01-01') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/time/coerced_predicate_spec.rb
spec/unit/coercible/coercer/time/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Time, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { Time.now } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/time/to_integer_spec.rb
spec/unit/coercible/coercer/time/to_integer_spec.rb
require 'spec_helper' describe Coercer::Time, '.to_integer' do subject { described_class.new.to_integer(value) } let(:time) { Time.now } let(:value) { time } it { is_expected.to eql(time.to_i) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/time/to_time_spec.rb
spec/unit/coercible/coercer/time/to_time_spec.rb
require 'spec_helper' describe Coercer::Time, '.to_time' do subject { object.to_time(time) } let(:object) { described_class.new } let(:time) { Time.local(2012, 1, 1) } it { is_expected.to equal(time) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/object/coerced_predicate_spec.rb
spec/unit/coercible/coercer/object/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Object, '#coerced?' do subject { object.coerced?(value) } let(:object) { described_class.new } let(:value) { 'something' } it { is_expected.to be(true) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/object/to_integer_spec.rb
spec/unit/coercible/coercer/object/to_integer_spec.rb
require 'spec_helper' describe Coercer::Object, '.to_integer' do subject { object.to_integer(value) } let(:object) { described_class.new } let(:value) { double('value') } context 'when the value responds to #to_int' do let(:coerced) { double('coerced') } before do expect(value).to receive(:to_int).and_return(coerced) end it { is_expected.to be(coerced) } end context 'when the value does not respond to #to_int' do specify { expect { subject }.to raise_error(UnsupportedCoercion) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/object/method_missing_spec.rb
spec/unit/coercible/coercer/object/method_missing_spec.rb
require 'spec_helper' describe Coercer::Object, '#method_missing' do subject { object.send(method_name, value) } let(:object) { described_class.new } let(:value) { Object.new } context "when method matches coercion method regexp" do let(:method_name) { :to_whatever } it { is_expected.to be(value) } end context "when method doesn't match coercion method regexp" do let(:method_name) { :not_here } specify { expect { subject }.to raise_error(NoMethodError) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/object/to_array_spec.rb
spec/unit/coercible/coercer/object/to_array_spec.rb
require 'spec_helper' describe Coercer::Object, '.to_array' do subject { object.to_array(value) } let(:object) { described_class.new } let(:value) { Object.new } let(:coerced) { [ value ] } context 'when the value responds to #to_ary' do before do expect(value).to receive(:to_ary).and_return(coerced) end it { is_expected.to be(coerced) } it 'does not call #to_a if #to_ary is available' do expect(value).not_to receive(:to_a) subject end end context 'when the value responds to #to_a but not #to_ary' do before do expect(value).to receive(:to_a).and_return(coerced) end it { is_expected.to be(coerced) } end context 'when the value does not respond to #to_ary or #to_a' do it { is_expected.to be_instance_of(Array) } it { is_expected.to eq(coerced) } end context 'when the value returns nil from #to_ary' do before do expect(value).to receive(:to_ary).and_return(nil) end it 'calls #to_a as a fallback' do expect(value).to receive(:to_a).and_return(coerced) is_expected.to be(coerced) end it 'wraps the value in an Array if #to_a is not available' do is_expected.to eq(coerced) end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/object/inspect_spec.rb
spec/unit/coercible/coercer/object/inspect_spec.rb
require 'spec_helper' describe Coercer::Object, '#inspect' do subject { object.inspect } let(:object) { described_class.new } it { is_expected.to eq('#<Coercible::Coercer::Object primitive=Object>')} end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/object/to_string_spec.rb
spec/unit/coercible/coercer/object/to_string_spec.rb
require 'spec_helper' describe Coercer::Object, '.to_string' do subject { object.to_string(value) } let(:object) { described_class.new } let(:value) { Object.new } context 'when the value responds to #to_str' do let(:coerced) { double('coerced') } before do expect(value).to receive(:to_str).and_return(coerced) end it { is_expected.to be(coerced) } end context 'when the value does not respond to #to_str' do specify { expect { subject }.to raise_error(UnsupportedCoercion) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/object/to_hash_spec.rb
spec/unit/coercible/coercer/object/to_hash_spec.rb
require 'spec_helper' describe Coercer::Object, '.to_hash' do subject { object.to_hash(value) } let(:object) { described_class.new } let(:value) { double('value') } context 'when the value responds to #to_hash' do let(:coerced) { double('coerced') } before do expect(value).to receive(:to_hash).and_return(coerced) end it { is_expected.to be(coerced) } end context 'when the value does not respond to #to_hash' do specify { expect { subject }.to raise_error(UnsupportedCoercion) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/array/coerced_predicate_spec.rb
spec/unit/coercible/coercer/array/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Array, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { [ 1, 2, 3 ] } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/array/to_set_spec.rb
spec/unit/coercible/coercer/array/to_set_spec.rb
require 'spec_helper' describe Coercer::Array, '#to_set' do subject { object.to_set(array) } let(:object) { described_class.new } let(:array) { [ 'a', 1, 'b', 2, 'b', 1, 'a', 2 ] } it { is_expected.to be_instance_of(Set) } it { is_expected.to eql(Set[ 'a', 1, 'b', 2 ]) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/decimal/coerced_predicate_spec.rb
spec/unit/coercible/coercer/decimal/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Decimal, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { BigDecimal('1.2') } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/decimal/to_integer_spec.rb
spec/unit/coercible/coercer/decimal/to_integer_spec.rb
require 'spec_helper' describe Coercer::Decimal, '.to_integer' do subject { object.to_integer(big_decimal) } let(:object) { described_class.new } let(:big_decimal) { BigDecimal('1.0') } it { is_expected.to be_kind_of(Integer) } it { is_expected.to eql(1) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/decimal/to_decimal_spec.rb
spec/unit/coercible/coercer/decimal/to_decimal_spec.rb
require 'spec_helper' describe Coercer::Decimal, '.to_decimal' do subject { described_class.new.to_decimal(value) } let(:value) { BigDecimal('1.0') } it { is_expected.to be(value) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/decimal/to_string_spec.rb
spec/unit/coercible/coercer/decimal/to_string_spec.rb
require 'spec_helper' describe Coercer::Decimal, '.to_string' do subject { object.to_string(big_decimal) } let(:object) { described_class.new } let(:big_decimal) { BigDecimal('1.0') } it { is_expected.to be_instance_of(String) } it { is_expected.to eql('1.0') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/decimal/to_float_spec.rb
spec/unit/coercible/coercer/decimal/to_float_spec.rb
require 'spec_helper' describe Coercer::Decimal, '.to_float' do subject { object.to_float(big_decimal) } let(:object) { described_class.new } let(:big_decimal) { BigDecimal('1.0') } it { is_expected.to be_instance_of(Float) } it { is_expected.to eql(1.0) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date_time/coerced_predicate_spec.rb
spec/unit/coercible/coercer/date_time/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::DateTime, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { DateTime.new } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date_time/to_datetime_spec.rb
spec/unit/coercible/coercer/date_time/to_datetime_spec.rb
require 'spec_helper' describe Coercer::DateTime, '.to_datetime' do subject { object.to_datetime(date_time) } let(:object) { described_class.new } let(:date_time) { DateTime.new(2012, 1, 1) } it { is_expected.to equal(date_time) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date_time/to_time_spec.rb
spec/unit/coercible/coercer/date_time/to_time_spec.rb
require 'spec_helper' describe Coercer::DateTime, '.to_time' do subject { object.to_time(date_time) } let(:object) { described_class.new } let(:date_time) { DateTime.new(2011, 1, 1) } context 'when DateTime does not support #to_time' do if RUBY_VERSION < '1.9' before do expect(date_time).not_to respond_to(:to_time) end end it { is_expected.to be_instance_of(Time) } it { is_expected.to eql(Time.local(2011, 1, 1)) } end context 'when DateTime supports #to_time' do let(:time) { Time.local(2011, 1, 1) } before do allow(date_time).to receive(:to_time).and_return(time) end it { is_expected.to equal(time) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date_time/to_date_spec.rb
spec/unit/coercible/coercer/date_time/to_date_spec.rb
require 'spec_helper' describe Coercer::DateTime, '.to_date' do subject { object.to_date(date_time) } let(:object) { described_class.new } let(:date_time) { DateTime.new(2011, 1, 1) } context 'when DateTime does not support #to_date' do if RUBY_VERSION < '1.9' before do expect(date_time).not_to respond_to(:to_date) end end it { is_expected.to be_instance_of(Date) } it { is_expected.to eql(Date.new(2011, 1, 1)) } end context 'when DateTime supports #to_date' do let(:date) { Date.new(2011, 1, 1) } before do allow(date_time).to receive(:to_date).and_return(date) end it { is_expected.to equal(date) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/date_time/to_string_spec.rb
spec/unit/coercible/coercer/date_time/to_string_spec.rb
require 'spec_helper' describe Coercer::DateTime, '.to_string' do subject { object.to_string(date_time) } let(:object) { described_class.new } let(:date_time) { DateTime.new(2011, 1, 1, 0, 0, 0, 0) } it { is_expected.to be_instance_of(String) } it { is_expected.to eql('2011-01-01T00:00:00+00:00') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/numeric/to_integer_spec.rb
spec/unit/coercible/coercer/numeric/to_integer_spec.rb
require 'spec_helper' describe Coercer::Numeric, '.to_integer' do subject { object.to_integer(numeric) } let(:object) { described_class.new } let(:numeric) { Rational(2, 2) } it { is_expected.to eql(1) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/numeric/to_decimal_spec.rb
spec/unit/coercible/coercer/numeric/to_decimal_spec.rb
require 'spec_helper' describe Coercer::Numeric, '.to_decimal' do subject { object.to_decimal(value) } let(:object) { described_class.new } context "with an object responding to #to_d" do let(:value) { Rational(2, 2) } it { is_expected.to eql(BigDecimal('1.0')) } end context "with an object not responding to #to_d" do let(:value) { Class.new { def to_s; '1'; end }.new } it { is_expected.to eql(BigDecimal('1.0')) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/numeric/to_string_spec.rb
spec/unit/coercible/coercer/numeric/to_string_spec.rb
require 'spec_helper' describe Coercer::Numeric, '.to_string' do subject { object.to_string(numeric) } let(:object) { described_class.new } let(:numeric) { Rational(2, 2) } let(:coerced_value) { RUBY_VERSION < '1.9' ? '1' : '1/1' } it { is_expected.to eql(coerced_value) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/numeric/to_float_spec.rb
spec/unit/coercible/coercer/numeric/to_float_spec.rb
require 'spec_helper' describe Coercer::Numeric, '.to_float' do subject { object.to_float(numeric) } let(:object) { described_class.new } let(:numeric) { Rational(2, 2) } it { is_expected.to eql(1.0) } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/symbol/coerced_predicate_spec.rb
spec/unit/coercible/coercer/symbol/coerced_predicate_spec.rb
require 'spec_helper' describe Coercer::Symbol, '#coerced?' do let(:object) { described_class.new } it_behaves_like 'Coercible::Coercer#coerced?' do let(:primitive_value) { :symbol } let(:non_primitive_value) { 'other' } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/unit/coercible/coercer/symbol/to_string_spec.rb
spec/unit/coercible/coercer/symbol/to_string_spec.rb
require 'spec_helper' describe Coercer::Symbol, '.to_string' do subject { object.to_string(symbol) } let(:object) { described_class.new } let(:symbol) { :piotr } it { is_expected.to be_instance_of(String) } it { is_expected.to eql('piotr') } end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/shared/unit/coerced_predicate.rb
spec/shared/unit/coerced_predicate.rb
shared_examples_for 'Coercible::Coercer#coerced?' do context "with a primitive value" do subject { object.coerced?(primitive_value) } it { is_expected.to be(true) } end context "with a non-primitive value" do subject { object.coerced?(non_primitive_value) } it { is_expected.to be(false) } end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/spec/shared/unit/configurable.rb
spec/shared/unit/configurable.rb
shared_examples_for 'Configurable' do describe '.config_name' do subject { described_class.config_name } it { is_expected.to be_instance_of(Symbol) } end describe '.config_keys' do subject { described_class.config_keys } it { is_expected.to be_instance_of(Array) } it { is_expected.not_to be_empty } end describe '.config' do subject { described_class.config } it { is_expected.to be_instance_of(Coercible::Configuration) } it 'responds to configuration keys' do described_class.config_keys.each do |key| expect(subject).to respond_to(key) expect(subject).to respond_to("#{key}=") end end end end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/lib/coercible.rb
lib/coercible.rb
module Coercible EXTRA_CONST_ARGS = (RUBY_VERSION < '1.9' ? [] : [ false ]).freeze UnsupportedCoercion = Class.new(StandardError) # Test for rubinius platform # # @return [true] # if running under rubinius # # @return [false] # otherwise # # @api private def self.rbx? @is_rbx ||= defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx' end end require 'date' require 'time' require 'bigdecimal' require 'bigdecimal/util' require 'set' require 'descendants_tracker' require 'support/options' require 'support/type_lookup' require 'coercible/version' require 'coercible/configuration' require 'coercible/coercer' require 'coercible/coercer/configurable' require 'coercible/coercer/object' require 'coercible/coercer/numeric' require 'coercible/coercer/float' require 'coercible/coercer/integer' require 'coercible/coercer/decimal' require 'coercible/coercer/string' require 'coercible/coercer/symbol' require 'coercible/coercer/time_coercions' require 'coercible/coercer/date' require 'coercible/coercer/date_time' require 'coercible/coercer/time' require 'coercible/coercer/false_class' require 'coercible/coercer/true_class' require 'coercible/coercer/array' require 'coercible/coercer/hash'
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/lib/coercible/version.rb
lib/coercible/version.rb
module Coercible VERSION = "1.0.1" end
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/lib/coercible/configuration.rb
lib/coercible/configuration.rb
module Coercible # Configuration object for global and per coercer type settings # class Configuration # Build a configuration instance # # @param [Array] list of accessor keys # # @return [Configuration] # # @api private def self.build(keys, &block) config = new keys.each do |key| config.instance_eval <<-RUBY def #{key} @#{key} end def #{key}=(value) @#{key} = value end RUBY end yield(config) if block_given? config end end # class Configuration end # module Coercible
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/lib/coercible/coercer.rb
lib/coercible/coercer.rb
module Coercible # Coercer object # # # @example # # coercer = Coercible::Coercer.new # # coercer[String].to_boolean('yes') # => true # coercer[Integer].to_string(1) # => '1' # # @api public class Coercer # Return coercer instances # # @return [Array<Coercer::Object>] # # @api private attr_reader :coercers # Returns global configuration for coercers # # @return [Configuration] # # @api private attr_reader :config # Build a new coercer # # @example # # Coercible::Coercer.new { |config| # set configuration } # # @yieldparam [Configuration] # # @return [Coercer] # # @api public def self.new(&block) configuration = Configuration.build(config_keys) configurable_coercers.each do |coercer| configuration.send("#{coercer.config_name}=", coercer.config) end yield(configuration) if block_given? super(configuration) end # Return configuration keys for Coercer instance # # @return [Array<Symbol>] # # @api private def self.config_keys configurable_coercers.map(&:config_name) end private_class_method :config_keys # Return coercer classes that are configurable # # @return [Array<Class>] # # @api private def self.configurable_coercers(&block) Coercer::Object.descendants.select { |descendant| descendant.respond_to?(:config) } end private_class_method :configurable_coercers # Initialize a new coercer instance # # @param [Hash] coercers # # @param [Configuration] config # # @return [undefined] # # @api private def initialize(config, coercers = {}) @coercers = coercers @config = config end # Access a specific coercer object for the given type # # @example # # coercer[String] # => string coercer # coercer[Integer] # => integer coercer # # @param [Class] type # # @return [Coercer::Object] # # @api public def [](klass) coercers[klass] || initialize_coercer(klass) end private # Initialize a new coercer instance for the given type # # If a coercer class supports configuration it will receive it from the # global configuration object # # @return [Coercer::Object] # # @api private def initialize_coercer(klass) coercers[klass] = begin coercer = Coercer::Object.determine_type(klass) || Coercer::Object args = [ self ] args << config_for(coercer) if coercer.respond_to?(:config_name) coercer.new(*args) end end # Find configuration for the given coercer type # # @return [Configuration] # # @api private def config_for(coercer) config.send(coercer.config_name) end end # class Coercer end # module Coercible
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/lib/coercible/coercer/true_class.rb
lib/coercible/coercer/true_class.rb
module Coercible class Coercer # Coerce true values class TrueClass < Object primitive ::TrueClass # Coerce given value to String # # @example # coercer[TrueClass].to_string(true) # => "true" # # @param [TrueClass] value # # @return [String] # # @api public def to_string(value) value.to_s end end # class TrueClass end # class Coercer end # module Coercible
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/lib/coercible/coercer/decimal.rb
lib/coercible/coercer/decimal.rb
module Coercible class Coercer # Coerce BigDecimal values class Decimal < Numeric primitive ::BigDecimal FLOAT_FORMAT = 'F'.freeze # Coerce given value to String # # @example # coercer[BigDecimal].to_string(BigDecimal('1.0')) # => "1.0" # # @param [BigDecimal] value # # @return [String] # # @api public def to_string(value) value.to_s(FLOAT_FORMAT) end # Passthrough the value # # @example # Coercible::Coercion::BigDecimal.to_decimal(BigDecimal('1.0')) # => BigDecimal('1.0') # # @param [BigDecimal] value # # @return [Fixnum] # # @api public def to_decimal(value) value end end # class BigDecimal end # class Coercer end # module Coercible
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false
solnic/coercible
https://github.com/solnic/coercible/blob/c076869838531abb5783280da108aa3cbddbd61a/lib/coercible/coercer/array.rb
lib/coercible/coercer/array.rb
module Coercible class Coercer # Coerce Array values class Array < Object primitive ::Array TIME_SEGMENTS = [ :year, :month, :day, :hour, :min, :sec ].freeze # Creates a Set instance from an Array # # @param [Array] value # # @return [Array] # # @api private def to_set(value) value.to_set end end # class Array end # class Coercer end # module Coercible
ruby
MIT
c076869838531abb5783280da108aa3cbddbd61a
2026-01-04T17:44:40.075543Z
false