Sha256: e6e2ff3d18e23950bad73a4b873a964201af4a933bea53a103a781772e8f6aaa
Contents?: true
Size: 1.81 KB
Versions: 2
Compression:
Stored size: 1.81 KB
Contents
require 'set' module Flipper module Typecast TruthMap = { true => true, 1 => true, 'true' => true, '1' => true, }.freeze # Internal: Convert value to a boolean. # # Returns true or false. def self.to_boolean(value) !!TruthMap[value] end # Internal: Convert value to an integer. # # Returns an Integer representation of the value. # Raises ArgumentError if conversion is not possible. def self.to_integer(value) value.to_i rescue NoMethodError raise ArgumentError, "#{value.inspect} cannot be converted to an integer" end # Internal: Convert value to a float. # # Returns a Float representation of the value. # Raises ArgumentError if conversion is not possible. def self.to_float(value) value.to_f rescue NoMethodError raise ArgumentError, "#{value.inspect} cannot be converted to a float" end # Internal: Convert value to a percentage. # # Returns a Integer or Float representation of the value. # Raises ArgumentError if conversion is not possible. def self.to_percentage(value) result_to_f = value.to_f result_to_i = result_to_f.to_i result_to_f == result_to_i ? result_to_i : result_to_f rescue NoMethodError raise ArgumentError, "#{value.inspect} cannot be converted to a percentage" end # Internal: Convert value to a set. # # Returns a Set representation of the value. # Raises ArgumentError if conversion is not possible. 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 end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
flipper-0.26.2 | lib/flipper/typecast.rb |
flipper-0.26.1 | lib/flipper/typecast.rb |