Class: Gillbus::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gillbus/helpers/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance:, doc:, fields:, parent:, options: {}) ⇒ Parser

Returns a new instance of Parser.



11
12
13
14
15
16
17
# File 'lib/gillbus/helpers/parser.rb', line 11

def initialize(instance:, doc:, fields:, parent:, options: {})
  @instance = instance
  @doc = doc
  @fields = fields
  @parent = parent
  @options = options
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



6
7
8
# File 'lib/gillbus/helpers/parser.rb', line 6

def doc
  @doc
end

#fieldsObject

Returns the value of attribute fields.



8
9
10
# File 'lib/gillbus/helpers/parser.rb', line 8

def fields
  @fields
end

#instanceObject

Returns the value of attribute instance.



7
8
9
# File 'lib/gillbus/helpers/parser.rb', line 7

def instance
  @instance
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/gillbus/helpers/parser.rb', line 9

def parent
  @parent
end

Instance Method Details

#parseObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gillbus/helpers/parser.rb', line 19

def parse
  fields.each do |name:, key:, type:, root:|
    raw_value =
      if key.is_a?(Regexp)
        doc.select { |k| k =~ key }
      elsif root
        doc[root] && doc[root][key]
      else
        doc[key]
      end
    value = make_one_or_many(type, raw_value)
    instance.send "#{name}=", value unless value.nil?
  end
  instance
end