Class: JsonApiFilter::ValueParser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ ValueParser

Returns a new instance of ValueParser.

Parameters:

  • value (String, Hash, HashWithIndifferentAccess)


7
8
9
# File 'lib/json_api_filter/value_parser.rb', line 7

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/json_api_filter/value_parser.rb', line 4

def value
  @value
end

Class Method Details

.parse(value) ⇒ Array

Transform string of coma separated values to Array

Examples:

"1,2,3" #=> [1,2,3]

Parameters:

  • value (String, Hash, HashWithIndifferentAccess)

Returns:

  • (Array)


18
19
20
# File 'lib/json_api_filter/value_parser.rb', line 18

def self.parse(value)
  new(value).indifferent_parse
end

Instance Method Details

#indifferent_parseObject



22
23
24
25
# File 'lib/json_api_filter/value_parser.rb', line 22

def indifferent_parse
  return parse if value.is_a? String
  parse_hash if value.is_a? HashWithIndifferentAccess
end

#parseObject



27
28
29
30
# File 'lib/json_api_filter/value_parser.rb', line 27

def parse
  return [value] unless value.include?(",")
  value.split(',')
end

#parse_hashObject



32
33
34
# File 'lib/json_api_filter/value_parser.rb', line 32

def parse_hash
  value.map{|k,v| {k => self.class.parse(v)}}.reduce(&:merge)
end