Class: Flipper::Typecast

Inherits:
Object
  • Object
show all
Defined in:
lib/flipper/typecast.rb

Constant Summary collapse

TRUTH_MAP =
{
  true    => true,
  1       => true,
  'true'  => true,
  '1'     => true,
}.freeze

Class Method Summary collapse

Class Method Details

.features_hash(source) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/flipper/typecast.rb', line 76

def self.features_hash(source)
  normalized_source = {}
  (source || {}).each do |feature_key, gates|
    normalized_source[feature_key] ||= {}
    gates.each do |gate_key, value|
      normalized_value = case value
      when Array, Set
        value.to_set
      when Hash
        value
      else
        value ? value.to_s : value
      end
      normalized_source[feature_key][gate_key.to_sym] = normalized_value
    end
  end
  normalized_source
end

.from_gzip(source) ⇒ Object



107
108
109
# File 'lib/flipper/typecast.rb', line 107

def self.from_gzip(source)
  Serializers::Gzip.deserialize(source)
end

.from_json(source) ⇒ Object



99
100
101
# File 'lib/flipper/typecast.rb', line 99

def self.from_json(source)
  Serializers::Json.deserialize(source)
end

.to_boolean(value) ⇒ Object

Internal: Convert value to a boolean.

Returns true or false.



17
18
19
# File 'lib/flipper/typecast.rb', line 17

def self.to_boolean(value)
  !!TRUTH_MAP[value]
end

.to_float(value) ⇒ Object

Internal: Convert value to a float.

Returns a Float representation of the value. Raises ArgumentError if conversion is not possible.



35
36
37
38
39
# File 'lib/flipper/typecast.rb', line 35

def self.to_float(value)
  value.to_f
rescue NoMethodError
  raise ArgumentError, "#{value.inspect} cannot be converted to a float"
end

.to_gzip(source) ⇒ Object



103
104
105
# File 'lib/flipper/typecast.rb', line 103

def self.to_gzip(source)
  Serializers::Gzip.serialize(source)
end

.to_integer(value) ⇒ Object

Internal: Convert value to an integer.

Returns an Integer representation of the value. Raises ArgumentError if conversion is not possible.



25
26
27
28
29
# File 'lib/flipper/typecast.rb', line 25

def self.to_integer(value)
  value.to_i
rescue NoMethodError
  raise ArgumentError, "#{value.inspect} cannot be converted to an integer"
end

.to_json(source) ⇒ Object



95
96
97
# File 'lib/flipper/typecast.rb', line 95

def self.to_json(source)
  Serializers::Json.serialize(source)
end

.to_number(value) ⇒ Object

Internal: Convert value to a number.

Returns a Integer or Float representation of the value. Raises ArgumentError if conversion is not possible.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/flipper/typecast.rb', line 45

def self.to_number(value)
  case value
  when Numeric
    value
  when String
    value.include?('.') ? to_float(value) : to_integer(value)
  when NilClass
    0
  else
    value.to_f
  end
rescue NoMethodError
  raise ArgumentError, "#{value.inspect} cannot be converted to a number"
end

.to_set(value) ⇒ Object

Internal: Convert value to a set.

Returns a Set representation of the value. Raises ArgumentError if conversion is not possible.



65
66
67
68
69
70
71
72
73
74
# File 'lib/flipper/typecast.rb', line 65

def self.to_set(value)
  return value if value.is_a?(Set)
  return Set.new if value.nil? || value.empty?

  if value.respond_to?(:to_set)
    value.to_set
  else
    raise ArgumentError, "#{value.inspect} cannot be converted to a set"
  end
end