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

Instance Method Details

#boolean_field?(key) ⇒ Boolean

Is this key one of the boolean fields

Parameters:

  • key (Symbol)

    field key

Returns:

  • (Boolean)


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

Parameters:

  • hash (#[]=)

    the collector object



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

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


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

Parameters:

  • value (nil, true, false, Integer)

Returns:

  • (nil, Integer)


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