Class: Brita::ValueParser

Inherits:
Object
  • Object
show all
Defined in:
lib/brita/value_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = options.fetch(:supports_boolean, false)
  @supports_ranges = options.fetch(:supports_ranges, false)
  @supports_json = options.fetch(:supports_json, false)
end

Instance Attribute Details

#supports_booleanObject

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_jsonObject

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_rangesObject

Returns the value of attribute supports_ranges.



3
4
5
# File 'lib/brita/value_parser.rb', line 3

def supports_ranges
  @supports_ranges
end

#valueObject

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_jsonObject



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

#parseObject



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