Class: Sequent::Core::Helpers::StringToValueParsers
- Inherits:
-
Object
- Object
- Sequent::Core::Helpers::StringToValueParsers
- Defined in:
- lib/sequent/core/helpers/string_to_value_parsers.rb
Constant Summary collapse
- PARSERS =
{ ::Symbol => ->(value) { Symbol.deserialize_from_json(value) }, ::String => ->(value) { value&.to_s }, ::Integer => ->(value) { parse_to_integer(value) }, ::BigDecimal => ->(value) { parse_to_bigdecimal(value) }, ::Float => ->(value) { parse_to_float(value) }, ::Boolean => ->(value) { parse_to_bool(value) }, ::Date => ->(value) { parse_to_date(value) }, ::DateTime => ->(value) { parse_to_date_time(value) }, ::Time => ->(value) { parse_to_time(value) }, ::Sequent::Core::Helpers::ArrayWithType => ->(values, type_in_array) { parse_array(values, type_in_array) }, ::Sequent::Core::Helpers::Secret => ->(value) { Sequent::Core::Helpers::Secret.new(value).encrypt }, }.freeze
Class Method Summary collapse
- .for(klass) ⇒ Object
- .parse_array(values, type_in_array) ⇒ Object
- .parse_to_bigdecimal(value) ⇒ Object
- .parse_to_bool(value) ⇒ Object
- .parse_to_date(value) ⇒ Object
- .parse_to_date_time(value) ⇒ Object
- .parse_to_float(value) ⇒ Object
- .parse_to_integer(value) ⇒ Object
- .parse_to_time(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(klass) ⇒ StringToValueParsers
constructor
A new instance of StringToValueParsers.
- #parse_from_string(value) ⇒ Object
Constructor Details
#initialize(klass) ⇒ StringToValueParsers
Returns a new instance of StringToValueParsers.
76 77 78 79 80 81 82 83 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 76 def initialize(klass) if klass.is_a? Sequent::Core::Helpers::ArrayWithType @array_with_type = klass @klass = klass.class else @klass = klass end end |
Class Method Details
.for(klass) ⇒ Object
72 73 74 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 72 def self.for(klass) new(klass) end |
.parse_array(values, type_in_array) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 60 def self.parse_array(values, type_in_array) fail "invalid value for array(): \"#{values}\"" unless values.is_a?(Array) values.map do |item| if item.respond_to?(:parse_attrs_to_correct_types) item.parse_attrs_to_correct_types else Sequent::Core::Helpers::StringToValueParsers.for(type_in_array).parse_from_string(item) end end end |
.parse_to_bigdecimal(value) ⇒ Object
30 31 32 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 30 def self.parse_to_bigdecimal(value) BigDecimal(value) unless value.blank? end |
.parse_to_bool(value) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 38 def self.parse_to_bool(value) if value.blank? && !(value.is_a?(TrueClass) || value.is_a?(FalseClass)) nil else (value.is_a?(TrueClass) || value == 'true') end end |
.parse_to_date(value) ⇒ Object
46 47 48 49 50 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 46 def self.parse_to_date(value) return if value.blank? value.is_a?(Date) ? value : Date.iso8601(value.dup) end |
.parse_to_date_time(value) ⇒ Object
52 53 54 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 52 def self.parse_to_date_time(value) value.is_a?(DateTime) ? value : DateTime.deserialize_from_json(value) end |
.parse_to_float(value) ⇒ Object
34 35 36 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 34 def self.parse_to_float(value) Float(value) unless value.blank? end |
.parse_to_integer(value) ⇒ Object
24 25 26 27 28 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 24 def self.parse_to_integer(value) return value if value.is_a?(Integer) Integer(value, 10) unless value.blank? end |
.parse_to_time(value) ⇒ Object
56 57 58 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 56 def self.parse_to_time(value) value.is_a?(Time) ? value : Time.deserialize_from_json(value) end |
Instance Method Details
#parse_from_string(value) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/sequent/core/helpers/string_to_value_parsers.rb', line 85 def parse_from_string(value) parser = PARSERS.fetch(@klass) { |key| fail "Unsupported value type: #{key}" } if @array_with_type parser.call(value, @array_with_type.item_type) else parser.call(value) end end |