Skip to content

Commit

Permalink
Fix an issue with a missing boolean coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
teliosdev committed Sep 10, 2015
1 parent 1041b21 commit 11bfde4
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/mixture.rb
Expand Up @@ -27,6 +27,16 @@ module Mixture
# @return [Proc{(Object) => Object}]
Itself = proc { |value| value }

# A proc that returns true.
#
# @return [Proc{() => true}]
Prove = proc { true }

# A proc that returns false.
#
# @return [Proc{() => false}]
Refute = proc { false }

# Finalizes all of the Mixture modules.
#
# @return [void]
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/coerce/array.rb
Expand Up @@ -7,6 +7,7 @@ class Array < Base
type Types::Array

coerce_to(Types::Object, Itself)
coerce_to(Types::Boolean, :any?)

coerce_to(Types::Array) do |value, type|
member = type.options.fetch(:members).first
Expand Down
14 changes: 14 additions & 0 deletions lib/mixture/coerce/boolean.rb
@@ -0,0 +1,14 @@
# encoding: utf-8

module Mixture
module Coerce
# Handles coercion of the boolean classes.
class Boolean < Base
type Types::Array

coerce_to(Types::Object, Itself)
coerce_to(Types::Boolean, Itself)
coerce_to(Types::Integer) { |value| value ? 1 : 0 }
end
end
end
1 change: 1 addition & 0 deletions lib/mixture/coerce/float.rb
Expand Up @@ -11,6 +11,7 @@ class Float < Base
coerce_to(Types::Float, Itself)
coerce_to(Types::Integer, :to_i)
coerce_to(Types::Rational, :to_r)
coerce_to(Types::Boolean) { |value| !value.zero? }
coerce_to(Types::Time) { |value| ::Time.at(value) }
coerce_to(Types::Date) { |value| ::Time.at(value).to_date }
coerce_to(Types::DateTime) { |value| ::Time.at(value).to_datetime }
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/coerce/hash.rb
Expand Up @@ -7,6 +7,7 @@ class Hash < Base
type Types::Hash

coerce_to(Types::Object, Itself)
coerce_to(Types::Boolean, :any?)

coerce_to(Types::Hash) do |value, type|
members = type.options.fetch(:members)
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/coerce/integer.rb
Expand Up @@ -11,6 +11,7 @@ class Integer < Base
coerce_to(Types::Float, :to_f)
coerce_to(Types::Rational, :to_r)
coerce_to(Types::Integer, Itself)
coerce_to(Types::Boolean) { |value| !value.zero? }
coerce_to(Types::Time) { |value| ::Time.at(value) }
coerce_to(Types::Date) { |value| ::Time.at(value).to_date }
coerce_to(Types::DateTime) { |value| ::Time.at(value).to_datetime }
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/coerce/nil.rb
Expand Up @@ -8,6 +8,7 @@ class Nil < Base

coerce_to(Types::Object, Itself)
coerce_to(Types::Nil, Itself)
coerce_to(Types::Boolean, Refute)
coerce_to(Types::String) { "" }
coerce_to(Types::Array) { [] }
coerce_to(Types::Float) { 0.0 }
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/coerce/object.rb
Expand Up @@ -22,6 +22,7 @@ class Object < Base
end

coerce_to(Types::Object, Itself)
coerce_to(Types::Boolean, Prove)
coerce_to(Types::Array, TryMethods[:to_a, :to_ary, :to_array])
coerce_to(Types::Date, TryMethods[:to_date])
coerce_to(Types::DateTime, TryMethods[:to_datetime])
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/coerce/rational.rb
Expand Up @@ -8,6 +8,7 @@ class Rational < Base

coerce_to(Types::Object, Itself)
coerce_to(Types::Rational, Itself)
coerce_to(Types::Boolean) { |value| !value.zero? }
coerce_to(Types::Time) { |value| ::Time.at(value) }
coerce_to(Types::Date) { |value| ::Time.at(value).to_date }
coerce_to(Types::DateTime) { |value| ::Time.at(value).to_datetime }
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/coerce/set.rb
Expand Up @@ -7,6 +7,7 @@ class Set < Base
type Types::Set

coerce_to(Types::Object, Itself)
coerce_to(Types::Boolean, :any?)

coerce_to(Types::Set) do |value, type|
member = type.options.fetch(:members).first
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/coerce/string.rb
Expand Up @@ -11,6 +11,7 @@ class String < Base
coerce_to(Types::Symbol, :to_sym)
coerce_to(Types::Integer, :to_i)
coerce_to(Types::Float, :to_f)
coerce_to(Types::Boolean) { |value| !value.empty? }
coerce_to(Types::Time) { |value| ::Time.parse(value) }
coerce_to(Types::Date) { |value| ::Date.parse(value) }
coerce_to(Types::DateTime) { |value| ::DateTime.parse(value) }
Expand Down
1 change: 1 addition & 0 deletions lib/mixture/types/boolean.rb
Expand Up @@ -10,6 +10,7 @@ module Types
class Boolean < Object
register
options[:primitive] = nil
options[:method] = :to_boolean
as :bool, :boolean, true, false

constraints.clear
Expand Down
2 changes: 1 addition & 1 deletion lib/mixture/version.rb
Expand Up @@ -5,5 +5,5 @@ module Mixture
# The current version of Mixture.
#
# @return [String]
VERSION = "0.5.1"
VERSION = "0.6.0"
end
9 changes: 9 additions & 0 deletions spec/mixture/coerce/nil_spec.rb
@@ -0,0 +1,9 @@
RSpec.describe Mixture::Coerce::Nil do
subject { described_class.instance }

it "performs coercions" do
expect(subject.to_integer.call(nil)).to be 0
expect(subject.to_boolean.call(nil)).to be false
expect(subject.to_string.call(nil)).to eq ""
end
end
1 change: 1 addition & 0 deletions spec/mixture/coerce/object_spec.rb
Expand Up @@ -7,6 +7,7 @@
it "attempts coercion" do
expect(subject.to_integer).to be_a Proc
expect(subject.to_integer.call(value)).to be_an Integer
expect(subject.to_boolean.call(value)).to be true
end

it "tries multiple methods" do
Expand Down

0 comments on commit 11bfde4

Please sign in to comment.