Module: BooleanHelpers
- Included in:
- Staccato::Hit
- Defined in:
- lib/staccato/boolean_helpers.rb
Overview
Collection of methods for converting to
google's boolean integers from ruby's booleans
Instance Method Summary collapse
-
#boolean_field?(key) ⇒ Boolean
Is this key one of the boolean fields.
-
#convert_boolean(k,v, hash) ⇒ Object
Method to convert a single field from bool to int.
-
#convert_booleans(hash) ⇒ Hash
Convert each boolean in the hash to integer if it is a boolean field.
-
#integer_for(value) ⇒ nil, Integer
Convert a value to appropriate int.
Instance Method Details
#boolean_field?(key) ⇒ Boolean
Is this key one of the boolean fields
21 22 23 |
# File 'lib/staccato/boolean_helpers.rb', line 21 def boolean_field?(key) boolean_fields.include?(key) end |
#convert_boolean(k,v, hash) ⇒ Object
Method to convert a single field from bool to int
14 15 16 |
# File 'lib/staccato/boolean_helpers.rb', line 14 def convert_boolean((k,v), hash) hash[k] = boolean_field?(k) ? integer_for(v) : v end |
#convert_booleans(hash) ⇒ Hash
Convert each boolean in the hash to integer
if it is a boolean field
8 9 10 |
# File 'lib/staccato/boolean_helpers.rb', line 8 def convert_booleans(hash) hash.each_pair.with_object({}, &method(:convert_boolean)) end |
#integer_for(value) ⇒ nil, Integer
Convert a value to appropriate int
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/staccato/boolean_helpers.rb', line 28 def integer_for(value) case value when Integer value when TrueClass 1 when FalseClass 0 when NilClass nil end end |