Module: Gillbus::Fields

Overview

Gives a bunch of class methods to parse gillbus responses hash and define accessors

Warning:

resulting definitions won't work properly in subclass

Usage:

class MyClass
  extend Gillbus::Fields
  field :foo, :date
end

MyClass.parse(hash)

Instance Method Summary collapse

Instance Method Details

#parse(doc, instance: new, parent: nil, options: {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gillbus/helpers/fields.rb', line 24

def parse(doc, instance: new, parent: nil, options: {})
  instance ||= new
  parser_class.new(
    doc: doc,
    instance: instance,
    fields: field_definitions,
    parent: parent,
    options: options,
  ).parse
  instance
end

#parser(&definition) ⇒ Object



36
37
38
# File 'lib/gillbus/helpers/fields.rb', line 36

def parser(&definition)
  @parser_class = Class.new(parser_class, &definition)
end

#parser_classObject



16
17
18
19
20
21
22
# File 'lib/gillbus/helpers/fields.rb', line 16

def parser_class
  if defined? @parser_class
    @parser_class || Gillbus::Parser
  else
    Gillbus::Parser
  end
end