Created
July 12, 2017 12:39
-
-
Save chrise86/edd53739e417a04954aa5918616fb7b6 to your computer and use it in GitHub Desktop.
How to implement a Boolean-Class for Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# DO NOT EXPECT THIS WILL RUN AS SHOWN HERE! | |
# This is an extract from https://github.com/iboard/yarb | |
# The specs will work only in the full environment of the application. | |
# | |
# Anyhow, this example should show you how to define a Boolean-value | |
# for ruby. | |
# | |
# Because HTML-Forms will post checkbox-values as "0"/"1" rather than false/true | |
# this class will handle 0,"0",false as FalseValue, and 1,"1",true as TrueValue | |
# | |
module Store | |
# Ruby has no Boolean class and html-forms will | |
# submit "0"/"1" instead of false/true | |
# Boolean will handle 0 and 1 and will return | |
# a TrueClass or FalseClass | |
class Boolean | |
# @param [true|false|Integer] value | |
# @return [TrueClass|FalseClass] "1", 1, true will return true | |
def self.new value | |
case value | |
when String | |
value == '1' | |
when Integer | |
value == 1 | |
else | |
value | |
end | |
end | |
end | |
end | |
### Usage example ################### | |
# @param [Class-Constant] _type | |
# @param [Object] _value | |
# @return [Object] | |
def initialize_default _type, _value | |
_type.new( _value ) | |
rescue | |
_value | |
end | |
i = initialize_default Integer, 42 | |
i # => 42 | |
b_false = initialize_default Boolean, "0" | |
b_false # => false | |
b_false = initialize_default Boolean, 0 | |
b_false # => false | |
b_false = initialize_default Boolean, false | |
b_false # => false | |
b_false = initialize_default Boolean, nil | |
b_false # => nil | |
# same for "1", 1, true | |
### Rspec ########################### | |
require_relative '../spec_helper' | |
describe Store do | |
describe Store::AttributeDefinition do | |
class MyClass | |
include Store | |
key_method :id | |
attribute :id | |
attribute :string_field | |
attribute :another_string | |
attribute :string_with_default, default: "string" | |
attribute :integer_with_default, type: Integer, default: 0 | |
attribute :nil_boolean, type: Boolean | |
attribute :default_true, type: Boolean, default: true | |
attribute :set_boolean, type: Boolean | |
attribute :str_boolean_true, type: Boolean, default: "1" | |
attribute :str_boolean_false, type: Boolean, default: "0" | |
attribute :int_boolean_true, type: Boolean, default: 1 | |
attribute :int_boolean_false, type: Boolean, default: 0 | |
end | |
let(:object) { | |
MyClass.new( id: 0, | |
another_string: "A String", | |
set_boolean: true, | |
) | |
} | |
it "default string is nil" do | |
expect( object.string_field ).to be_nil | |
end | |
it "returns string when set" do | |
expect( object.another_string ).to eq("A String") | |
end | |
it "returns the default if given" do | |
expect( object.string_with_default ).to eq("string") | |
end | |
it "returns an Integer with default" do | |
expect( object.integer_with_default ).to eq(0) | |
end | |
it "returns a Boolean w/o default as nil" do | |
expect( object.nil_boolean ).to be_nil | |
end | |
it "returns a Boolean with default true as true" do | |
expect( object.default_true ).to be_true | |
end | |
it "returns a Boolean as initialized at new" do | |
expect( object.set_boolean ).to be_true | |
end | |
it "returns a boolean initialized with 1 as true" do | |
expect( object.int_boolean_true ).to be_true | |
end | |
it "returns a boolean initialized with 0 as false" do | |
expect( object.int_boolean_false).to be_false | |
end | |
it "returns a boolean initialized with '1' as true" do | |
expect( object.str_boolean_true ).to be_true | |
end | |
it "returns a boolean initialized with '0' as false" do | |
expect( object.str_boolean_false).to be_false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment