Module: Flipper::Typecast
- Defined in:
- lib/flipper/typecast.rb
Constant Summary collapse
- TruthMap =
{ true => true, 1 => true, 'true' => true, '1' => true, }.freeze
Class Method Summary collapse
- .features_hash(source) ⇒ Object
-
.to_boolean(value) ⇒ Object
Internal: Convert value to a boolean.
-
.to_float(value) ⇒ Object
Internal: Convert value to a float.
-
.to_integer(value) ⇒ Object
Internal: Convert value to an integer.
-
.to_number(value) ⇒ Object
Internal: Convert value to a number.
-
.to_set(value) ⇒ Object
Internal: Convert value to a set.
Class Method Details
.features_hash(source) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/flipper/typecast.rb', line 74 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 |
.to_boolean(value) ⇒ Object
Internal: Convert value to a boolean.
Returns true or false.
15 16 17 |
# File 'lib/flipper/typecast.rb', line 15 def self.to_boolean(value) !!TruthMap[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.
33 34 35 36 37 |
# File 'lib/flipper/typecast.rb', line 33 def self.to_float(value) value.to_f rescue NoMethodError raise ArgumentError, "#{value.inspect} cannot be converted to a float" 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.
23 24 25 26 27 |
# File 'lib/flipper/typecast.rb', line 23 def self.to_integer(value) value.to_i rescue NoMethodError raise ArgumentError, "#{value.inspect} cannot be converted to an integer" 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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/flipper/typecast.rb', line 43 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.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/flipper/typecast.rb', line 63 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 |