mixture 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +9 -0
  5. data/.travis.yml +6 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +74 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +43 -0
  11. data/Rakefile +8 -0
  12. data/lib/mixture/attribute.rb +31 -0
  13. data/lib/mixture/attribute_list.rb +29 -0
  14. data/lib/mixture/coerce/array.rb +15 -0
  15. data/lib/mixture/coerce/base.rb +64 -0
  16. data/lib/mixture/coerce/date.rb +20 -0
  17. data/lib/mixture/coerce/datetime.rb +20 -0
  18. data/lib/mixture/coerce/float.rb +19 -0
  19. data/lib/mixture/coerce/hash.rb +14 -0
  20. data/lib/mixture/coerce/integer.rb +19 -0
  21. data/lib/mixture/coerce/nil.rb +20 -0
  22. data/lib/mixture/coerce/object.rb +34 -0
  23. data/lib/mixture/coerce/rational.rb +19 -0
  24. data/lib/mixture/coerce/set.rb +14 -0
  25. data/lib/mixture/coerce/string.rb +19 -0
  26. data/lib/mixture/coerce/symbol.rb +14 -0
  27. data/lib/mixture/coerce/time.rb +19 -0
  28. data/lib/mixture/coerce.rb +70 -0
  29. data/lib/mixture/errors.rb +14 -0
  30. data/lib/mixture/extensions/attributable.rb +64 -0
  31. data/lib/mixture/extensions/coercable.rb +31 -0
  32. data/lib/mixture/extensions/hashable.rb +37 -0
  33. data/lib/mixture/extensions/validatable.rb +52 -0
  34. data/lib/mixture/extensions.rb +30 -0
  35. data/lib/mixture/model.rb +13 -0
  36. data/lib/mixture/type.rb +142 -0
  37. data/lib/mixture/validate/base.rb +21 -0
  38. data/lib/mixture/validate/match.rb +17 -0
  39. data/lib/mixture/validate/presence.rb +17 -0
  40. data/lib/mixture/validate.rb +35 -0
  41. data/lib/mixture/version.rb +9 -0
  42. data/lib/mixture.rb +29 -0
  43. data/mixture.gemspec +30 -0
  44. metadata +156 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2be9a2d68accc0bae55d6a86edac6d0d0d0fd034
4
+ data.tar.gz: 28db784d61898c2f4bfc5ed546f5644724ea627f
5
+ SHA512:
6
+ metadata.gz: 88764ff3ef98d101f7acc87221cb715a9544a47bc330dc679a07bf9ec0696c1eb8bd1d792ccb7f9e602418289d40416195cd26b62b06011ac4cd0370bc46a3c3
7
+ data.tar.gz: 9fcfa02187da824b8d21eb726526230f178f5c48ec9eb2f638d3214bd08a29d22f5aeb740a7be9278ab4ffc780c1161ae4b1eb8a76d6afd0c1a8ab1da0602e18
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.gems
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ Style/StringLiterals:
2
+ EnforcedStyle: double_quotes
3
+ Style/Encoding:
4
+ Enabled: true
5
+ AutoCorrectEncodingComment: "# encoding: utf-8\n"
6
+ Style/RegexpLiteral:
7
+ EnforcedStyle: mixed
8
+ Metrics/MethodLength:
9
+ Max: 15
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ - 2.1.0
5
+ - 1.9.3
6
+ - rbx
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mixture.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mixture (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.0)
10
+ coveralls (0.8.2)
11
+ json (~> 1.8)
12
+ rest-client (>= 1.6.8, < 2)
13
+ simplecov (~> 0.10.0)
14
+ term-ansicolor (~> 1.3)
15
+ thor (~> 0.19.1)
16
+ diff-lcs (1.2.5)
17
+ docile (1.1.5)
18
+ domain_name (0.5.24)
19
+ unf (>= 0.0.5, < 1.0.0)
20
+ http-cookie (1.0.2)
21
+ domain_name (~> 0.5)
22
+ json (1.8.3)
23
+ method_source (0.8.2)
24
+ mime-types (2.6.1)
25
+ netrc (0.10.3)
26
+ pry (0.10.1)
27
+ coderay (~> 1.1.0)
28
+ method_source (~> 0.8.1)
29
+ slop (~> 3.4)
30
+ rake (10.4.2)
31
+ rest-client (1.8.0)
32
+ http-cookie (>= 1.0.2, < 2.0)
33
+ mime-types (>= 1.16, < 3.0)
34
+ netrc (~> 0.7)
35
+ rspec (3.3.0)
36
+ rspec-core (~> 3.3.0)
37
+ rspec-expectations (~> 3.3.0)
38
+ rspec-mocks (~> 3.3.0)
39
+ rspec-core (3.3.1)
40
+ rspec-support (~> 3.3.0)
41
+ rspec-expectations (3.3.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.3.0)
44
+ rspec-mocks (3.3.1)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.3.0)
47
+ rspec-support (3.3.0)
48
+ simplecov (0.10.0)
49
+ docile (~> 1.1.0)
50
+ json (~> 1.8)
51
+ simplecov-html (~> 0.10.0)
52
+ simplecov-html (0.10.0)
53
+ slop (3.6.0)
54
+ term-ansicolor (1.3.2)
55
+ tins (~> 1.0)
56
+ thor (0.19.1)
57
+ tins (1.5.4)
58
+ unf (0.1.4)
59
+ unf_ext
60
+ unf_ext (0.0.7.1)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ bundler
67
+ coveralls
68
+ mixture!
69
+ pry
70
+ rake
71
+ rspec
72
+
73
+ BUNDLED WITH
74
+ 1.10.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jeremy Rodi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Mixture
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'mixture'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mixture
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ To install this gem onto your local machine, run
26
+ `bundle exec rake install`. To release a new version, update the
27
+ version number in `version.rb`, and then run
28
+ `bundle exec rake release`, which will create a git tag for the
29
+ version, push git commits and tags, and push the `.gem` file to
30
+ [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at
35
+ https://github.com/medcat/mixture. This project is intended to be a
36
+ safe, welcoming space for collaboration, and contributors are expected
37
+ to adhere to the [Contributor Covenant](contributor-covenant.org) code
38
+ of conduct.
39
+
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ # An attribute for a mixture object.
5
+ class Attribute
6
+ attr_reader :name
7
+ attr_reader :options
8
+
9
+ def initialize(name, list, options = {})
10
+ @name = name
11
+ @list = list
12
+ @options = options
13
+ end
14
+
15
+ def update(value)
16
+ @list.callbacks[:update].inject(value) { |a, e| e.call(self, a) }
17
+ end
18
+
19
+ def ivar
20
+ @_ivar ||= :"@#{@name}"
21
+ end
22
+
23
+ def getter
24
+ @name
25
+ end
26
+
27
+ def setter
28
+ @_setter ||= :"#{@name}="
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require "forwardable"
4
+
5
+ module Mixture
6
+ # A list of attributes.
7
+ class AttributeList
8
+ extend Forwardable
9
+ # If it looks like a duck...
10
+ include Enumerable
11
+ # If it quacks like a duck...
12
+ include Comparable
13
+ # Then it must be a duck.
14
+ delegate [:fetch, :[], :[]=, :key?, :each, :<=>] => :@list
15
+
16
+ attr_reader :options
17
+ attr_reader :callbacks
18
+
19
+ def initialize
20
+ @list = {}
21
+ @options = {}
22
+ @callbacks = Hash.new { |h, k| h[k] = [] }
23
+ end
24
+
25
+ def create(name, options = {})
26
+ @list[name] = Attribute.new(name, self, options)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Array class.
6
+ class Array < Base
7
+ type Type::Array
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::Array, Itself)
11
+ coerce_to(Type::Set, :to_set)
12
+ coerce_to(Type::Hash) { |value| ::Hash[value] }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+
3
+ require "singleton"
4
+
5
+ module Mixture
6
+ module Coerce
7
+ # The base for coercion actions. Each action defines the "from"
8
+ # type, and the instance handles the "to".
9
+ class Base
10
+ include Singleton
11
+ # @!method type
12
+ # Returns the type this instance corresponds to.
13
+ #
14
+ # @return [Mixture::Type]
15
+ #
16
+ # @!method type(value)
17
+ # Sets the type this instance corresponds to.
18
+ #
19
+ # @param [Mixture::Type]
20
+ # @return [void]
21
+ def self.type(value = Undefined)
22
+ if value == Undefined
23
+ @_type
24
+ else
25
+ @_type = value
26
+ end
27
+ end
28
+
29
+ def self.coercions
30
+ @_coercions ||= {}
31
+ end
32
+
33
+ def self.coerce_to(to, value = Undefined, &block)
34
+ fail ArgumentError, "Expected Mixture::Type, got #{to.class}" unless
35
+ to.is_a?(Mixture::Type)
36
+
37
+ body = case
38
+ when value != Undefined
39
+ value.to_proc
40
+ when block_given?
41
+ block
42
+ else
43
+ fail ArgumentError, "Expected a block"
44
+ end
45
+
46
+ coercions[to] = to.method_name
47
+ define_method(to.method_name) { body }
48
+ end
49
+
50
+ def self.to(type)
51
+ instance.to(type)
52
+ end
53
+
54
+ def to(type)
55
+ method_name = self.class.coercions.fetch(type) do
56
+ fail CoercionError,
57
+ "Undefined coercion of #{self.class.type} => #{type}"
58
+ end
59
+
60
+ public_send(method_name)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Date class.
6
+ class Date < Base
7
+ type Type::Date
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::String, :to_s)
11
+ coerce_to(Type::Time, :to_time)
12
+ coerce_to(Type::Date, :to_date)
13
+ coerce_to(Type::DateTime, :to_datetime)
14
+ coerce_to(Type::Array) { |value| value.to_time.to_a }
15
+ coerce_to(Type::Float) { |value| value.to_time.to_f }
16
+ coerce_to(Type::Integer) { |value| value.to_time.to_i }
17
+ coerce_to(Type::Rational) { |value| value.to_time.to_r }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the DateTime class.
6
+ class DateTime < Base
7
+ type Type::DateTime
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::String, :to_s)
11
+ coerce_to(Type::Time, :to_time)
12
+ coerce_to(Type::Date, :to_date)
13
+ coerce_to(Type::DateTime, :to_datetime)
14
+ coerce_to(Type::Array) { |value| value.to_time.to_a }
15
+ coerce_to(Type::Float) { |value| value.to_time.to_f }
16
+ coerce_to(Type::Integer) { |value| value.to_time.to_i }
17
+ coerce_to(Type::Rational) { |value| value.to_time.to_r }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Float class.
6
+ class Float < Base
7
+ type Type::Float
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::String, :to_s)
11
+ coerce_to(Type::Float, Itself)
12
+ coerce_to(Type::Integer, :to_i)
13
+ coerce_to(Type::Rational, :to_r)
14
+ coerce_to(Type::Time) { |value| ::Time.at(value) }
15
+ coerce_to(Type::Date) { |value| ::Time.at(value).to_date }
16
+ coerce_to(Type::DateTime) { |value| ::Time.at(value).to_datetime }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Hash class.
6
+ class Hash < Base
7
+ type Type::Hash
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::Hash, Itself)
11
+ coerce_to(Type::Array, :to_a)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Integer class.
6
+ class Integer < Base
7
+ type Type::Integer
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::String, :to_s)
11
+ coerce_to(Type::Float, :to_f)
12
+ coerce_to(Type::Rational, :to_r)
13
+ coerce_to(Type::Integer, Itself)
14
+ coerce_to(Type::Time) { |value| ::Time.at(value) }
15
+ coerce_to(Type::Date) { |value| ::Time.at(value).to_date }
16
+ coerce_to(Type::DateTime) { |value| ::Time.at(value).to_datetime }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Nil class.
6
+ class Nil < Base
7
+ type Type::Nil
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::String) { "" }
11
+ coerce_to(Type::Array) { [] }
12
+ coerce_to(Type::Float) { 0.0 }
13
+ coerce_to(Type::Hash) { {} }
14
+ coerce_to(Type::Integer) { 0 }
15
+ coerce_to(Type::Rational) { Rational(0, 1) }
16
+ coerce_to(Type::Set) { Set.new }
17
+ coerce_to(Type::Symbol) { :"" }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Object class.
6
+ class Object < Base
7
+ type Type::Object
8
+
9
+ TryMethods = proc do |*methods|
10
+ proc do |value|
11
+ method = methods.find { |m| value.respond_to?(m) }
12
+ if method
13
+ value.public_send(method)
14
+ else
15
+ fail CoercionError, "Could not coerce #{value.class}"
16
+ end
17
+ end
18
+ end
19
+
20
+ coerce_to(Type::Array, TryMethods[:to_a, :to_ary, :to_array])
21
+ coerce_to(Type::Date, TryMethods[:to_date])
22
+ coerce_to(Type::DateTime, TryMethods[:to_datetime])
23
+ coerce_to(Type::Float, TryMethods[:to_f, :to_float])
24
+ coerce_to(Type::Hash, TryMethods[:to_h, :to_hash])
25
+ coerce_to(Type::Integer, TryMethods[:to_i, :to_integer])
26
+ coerce_to(Type::Object, :dup)
27
+ coerce_to(Type::Rational, TryMethods[:to_r, :to_rational])
28
+ coerce_to(Type::Set, TryMethods[:to_set])
29
+ coerce_to(Type::String, TryMethods[:to_s, :to_str, :to_string])
30
+ coerce_to(Type::Symbol, TryMethods[:to_sym, :intern, :to_symbol])
31
+ coerce_to(Type::Time, TryMethods[:to_time])
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Rational class.
6
+ class Rational < Base
7
+ type Type::Rational
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::Rational, Itself)
11
+ coerce_to(Type::Time) { |value| ::Time.at(value) }
12
+ coerce_to(Type::Date) { |value| ::Time.at(value).to_date }
13
+ coerce_to(Type::DateTime) { |value| ::Time.at(value).to_datetime }
14
+ coerce_to(Type::Float, :to_f)
15
+ coerce_to(Type::Integer, :to_i)
16
+ coerce_to(Type::String, :to_s)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Set class.
6
+ class Set < Base
7
+ type Type::Set
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::Set, :dup)
11
+ coerce_to(Type::Array, :to_a)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the String class.
6
+ class String < Base
7
+ type Type::String
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::String, :dup)
11
+ coerce_to(Type::Symbol, :to_sym)
12
+ coerce_to(Type::Integer, :to_i)
13
+ coerce_to(Type::Float, :to_f)
14
+ coerce_to(Type::Time) { |value| ::Time.parse(value) }
15
+ coerce_to(Type::Date) { |value| ::Date.parse(value) }
16
+ coerce_to(Type::DateTime) { |value| ::DateTime.parse(value) }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Numeric class.
6
+ class Symbol < Base
7
+ type Type::Symbol
8
+
9
+ coerce_to(Type::Object, Itself)
10
+ coerce_to(Type::String, :to_s)
11
+ coerce_to(Type::Symbol, Itself)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module Mixture
4
+ module Coerce
5
+ # Handles coercion of the Time class.
6
+ class Time < Base
7
+ type Type::Time
8
+
9
+ coerce_to(Type::String, :to_s)
10
+ coerce_to(Type::Integer, :to_i)
11
+ coerce_to(Type::Float, :to_f)
12
+ coerce_to(Type::Rational, :to_r)
13
+ coerce_to(Type::Array, :to_a)
14
+ coerce_to(Type::Time, Itself)
15
+ coerce_to(Type::Date, :to_date)
16
+ coerce_to(Type::DateTime, :to_datetime)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+
3
+ require "mixture/coerce/base"
4
+ require "mixture/coerce/array"
5
+ require "mixture/coerce/date"
6
+ require "mixture/coerce/datetime"
7
+ require "mixture/coerce/float"
8
+ require "mixture/coerce/hash"
9
+ require "mixture/coerce/integer"
10
+ require "mixture/coerce/nil"
11
+ require "mixture/coerce/object"
12
+ require "mixture/coerce/rational"
13
+ require "mixture/coerce/set"
14
+ require "mixture/coerce/string"
15
+ require "mixture/coerce/symbol"
16
+ require "mixture/coerce/time"
17
+
18
+ module Mixture
19
+ # Handles coercion of objects.
20
+ module Coerce
21
+ # Registers a coercion with the module. This uses the {COERCERS}
22
+ # constant.
23
+ #
24
+ # @param coercion [Mixture::Coerce::Base] The coercer to register.
25
+ # @return [void]
26
+ def self.register(coercion)
27
+ coercers[coercion.type] = coercion
28
+ end
29
+
30
+ # A hash of the coercers that currently exist. This maps their
31
+ # types to their classes.
32
+ #
33
+ # @return [Hash{Mixture::Type => Mixture::Coerce::Base}]
34
+ def self.coercers
35
+ @_coercers ||= {}
36
+ end
37
+
38
+ # Returns a block that takes one argument: the value.
39
+ #
40
+ # @param from [Mixture::Type]
41
+ # The type to coerce from.
42
+ # @param to [Mixture::Type]
43
+ # The type to coerce to.
44
+ # @return [Proc{(Object) => Object}]
45
+ def self.coerce(from, to)
46
+ coercers
47
+ .fetch(from) { fail CoercionError, "No coercer for #{from}" }
48
+ .to(to)
49
+ end
50
+
51
+ # Registers the default coercions.
52
+ def self.load
53
+ register Array
54
+ register Date
55
+ register DateTime
56
+ register Float
57
+ register Hash
58
+ register Integer
59
+ register Nil
60
+ register Object
61
+ register Rational
62
+ register Set
63
+ register String
64
+ register Symbol
65
+ register Time
66
+ end
67
+
68
+ load
69
+ end
70
+ end