Class: Brita::ValueParser
- Inherits:
-
Object
- Object
- Brita::ValueParser
- Defined in:
- lib/brita/value_parser.rb
Instance Attribute Summary collapse
-
#supports_boolean ⇒ Object
Returns the value of attribute supports_boolean.
-
#supports_json ⇒ Object
Returns the value of attribute supports_json.
-
#supports_ranges ⇒ Object
Returns the value of attribute supports_ranges.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #array_from_json ⇒ Object
-
#initialize(value:, options: {}) ⇒ ValueParser
constructor
A new instance of ValueParser.
- #parse ⇒ Object
Constructor Details
#initialize(value:, options: {}) ⇒ ValueParser
Returns a new instance of ValueParser.
4 5 6 7 8 9 |
# File 'lib/brita/value_parser.rb', line 4 def initialize(value:, options: {}) @value = value @supports_boolean = .fetch(:supports_boolean, false) @supports_ranges = .fetch(:supports_ranges, false) @supports_json = .fetch(:supports_json, false) end |
Instance Attribute Details
#supports_boolean ⇒ Object
Returns the value of attribute supports_boolean.
3 4 5 |
# File 'lib/brita/value_parser.rb', line 3 def supports_boolean @supports_boolean end |
#supports_json ⇒ Object
Returns the value of attribute supports_json.
3 4 5 |
# File 'lib/brita/value_parser.rb', line 3 def supports_json @supports_json end |
#supports_ranges ⇒ Object
Returns the value of attribute supports_ranges.
3 4 5 |
# File 'lib/brita/value_parser.rb', line 3 def supports_ranges @supports_ranges end |
#value ⇒ Object
Returns the value of attribute value.
3 4 5 |
# File 'lib/brita/value_parser.rb', line 3 def value @value end |
Instance Method Details
#array_from_json ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/brita/value_parser.rb', line 24 def array_from_json result = JSON.parse(value) if result.is_a?(Array) result else value end rescue JSON::ParserError value end |
#parse ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/brita/value_parser.rb', line 11 def parse @_result ||= if parse_as_range? range_value elsif parse_as_boolean? boolean_value elsif parse_as_json? array_from_json else value end end |