Class: Gillbus::Parser

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

Constant Summary collapse

NULL_CONST =
'null'.freeze
TRUE_CONST =
'true'.freeze
YES_CONST =
'Y'.freeze
DATE_FORMAT_CONST =
'%d.%m.%Y'.freeze
TIME_FORMAT_REGEXP =
/^ ( \d\d:\d\d ) (?: :\d\d )? $/x.freeze
DEFAULT_TIMEZONE =
'Europe/Kiev'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parser.



18
19
20
21
22
23
24
# File 'lib/gillbus/helpers/parser.rb', line 18

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.



13
14
15
# File 'lib/gillbus/helpers/parser.rb', line 13

def doc
  @doc
end

#fieldsObject

Returns the value of attribute fields.



15
16
17
# File 'lib/gillbus/helpers/parser.rb', line 15

def fields
  @fields
end

#instanceObject

Returns the value of attribute instance.



14
15
16
# File 'lib/gillbus/helpers/parser.rb', line 14

def instance
  @instance
end

#parentObject

Returns the value of attribute parent.



16
17
18
# File 'lib/gillbus/helpers/parser.rb', line 16

def parent
  @parent
end

Instance Method Details

#parseObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gillbus/helpers/parser.rb', line 26

def parse
  fields.each do |name:, key:, type:, root:|
    value =
      if type == :datetime_combined
        datetime_combined(key)
      else
        raw_value = fetch_value(key: key, root: root)
        make_one_or_many(type, raw_value)
      end
    instance.send "#{name}=", value unless value.nil?
  end
  instance
end