Sha256: c82b7699a1d8a244ee8acce90ce7f7892d7a4667fb1460c1f11bd748ab00f062

Contents?: true

Size: 972 Bytes

Versions: 16

Compression:

Stored size: 972 Bytes

Contents

# Collection of methods for converting to
#   google's boolean integers from ruby's booleans
module BooleanHelpers
  # Convert each boolean in the hash to integer
  #   if it is a boolean field
  # @param hash [Hash]
  # @return [Hash]
  def convert_booleans(hash)
    hash.each_pair.with_object({}, &method(:convert_boolean))
  end

  # Method to convert a single field from bool to int
  # @param hash [#[]=] the collector object
  def convert_boolean((k,v), hash)
    hash[k] = boolean_field?(k) ? integer_for(v) : v
  end

  # Is this key one of the boolean fields
  # @param key [Symbol] field key
  # @return [Boolean]
  def boolean_field?(key)
    boolean_fields.include?(key)
  end

  # Convert a value to appropriate int
  # @param value [nil, true, false, Integer]
  # @return [nil, Integer]
  def integer_for(value)
    case value
    when Integer
      value
    when TrueClass
      1
    when FalseClass
      0
    when NilClass
      nil
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
staccato-0.5.3 lib/staccato/boolean_helpers.rb
staccato-0.5.2 lib/staccato/boolean_helpers.rb
staccato-0.5.1 lib/staccato/boolean_helpers.rb
staccato-0.5.0 lib/staccato/boolean_helpers.rb
staccato-0.4.7 lib/staccato/boolean_helpers.rb
staccato-0.4.6 lib/staccato/boolean_helpers.rb
staccato-0.4.5 lib/staccato/boolean_helpers.rb
staccato-0.4.4 lib/staccato/boolean_helpers.rb
staccato-0.4.3 lib/staccato/boolean_helpers.rb
staccato-0.4.2 lib/staccato/boolean_helpers.rb
staccato-0.4.1 lib/staccato/boolean_helpers.rb
staccato-0.4.0 lib/staccato/boolean_helpers.rb
staccato-0.3.1 lib/staccato/boolean_helpers.rb
staccato-0.3.0 lib/staccato/boolean_helpers.rb
staccato-0.2.1 lib/staccato/boolean_helpers.rb
staccato-0.2.0 lib/staccato/boolean_helpers.rb