Class: Converter
- Inherits:
-
Object
- Object
- Converter
- Defined in:
- lib/undecided/converter.rb
Overview
Convert data to required type
Class Method Summary collapse
-
.hash_values_to_bool(values) ⇒ Object
Convert every value from hash to a boolean value.
- .process_values(values) ⇒ Object
-
.replacing_variables(rule, values) ⇒ Object
Replaces variables from rules with values.
-
.to_bool(value) ⇒ Object
Transform value to boolean.
-
.to_symbol(value) ⇒ Object
Convert array, hash’s keys or solo values to symbol.
Class Method Details
.hash_values_to_bool(values) ⇒ Object
Convert every value from hash to a boolean value
21 22 23 24 |
# File 'lib/undecided/converter.rb', line 21 def self.hash_values_to_bool(values) raise 'Only hash values' unless values.is_a? Hash values.map { |k, v| [k, to_bool(v)] }.to_h end |
.process_values(values) ⇒ Object
3 4 5 6 |
# File 'lib/undecided/converter.rb', line 3 def self.process_values(values) values = to_symbol(values) hash_values_to_bool(values) end |
.replacing_variables(rule, values) ⇒ Object
Replaces variables from rules with values
39 40 41 42 43 44 |
# File 'lib/undecided/converter.rb', line 39 def self.replacing_variables(rule, values) process_values(values).each do |k, v| rule.gsub! k.to_s, v.to_s end rule end |
.to_bool(value) ⇒ Object
Transform value to boolean
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/undecided/converter.rb', line 27 def self.to_bool(value) # if boolean return value return value if Undecided::Evaluator.bool?(value) # transform integer value to boolean !value.to_i.zero? rescue => e puts e. raise "#{value} Is not a correct value, " \ 'insert (1 or 0) or (true or false)' end |
.to_symbol(value) ⇒ Object
Convert array, hash’s keys or solo values to symbol
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/undecided/converter.rb', line 9 def self.to_symbol(value) if value.is_a?(Array) value.map(&:to_sym) elsif value.is_a? Hash value.map { |k, v| [k.to_sym, v] }.to_h else # for String and Number solo values value.to_s.to_sym end end |