Class: FormStalker::Data::Base

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/form_stalker/data/base.rb

Direct Known Subclasses

CheckLogic, Form, FormField, FormFieldsLogic

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Base

Returns a new instance of Base.



37
38
39
40
41
42
43
44
45
# File 'lib/form_stalker/data/base.rb', line 37

def initialize(attributes = nil)
  attributes ||= {}

  instance_variable_set('@table', attributes)

  @parser = Parser.new(self, self.class.type_cast_schema, attributes)

  super(@parser.parse_attributes)
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



35
36
37
# File 'lib/form_stalker/data/base.rb', line 35

def parser
  @parser
end

Class Method Details

.inherited(base) ⇒ Object



14
15
16
17
18
# File 'lib/form_stalker/data/base.rb', line 14

def self.inherited(base)
  base.type_cast_schema.merge! type_cast_schema.clone

  super
end

.schema(options = nil) ⇒ Object



6
7
8
# File 'lib/form_stalker/data/base.rb', line 6

def self.schema(options = nil)
  type_cast_schema.merge! options
end

.tap_into(response) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/form_stalker/data/base.rb', line 20

def self.tap_into(response)
  return response unless response.ok?

  response.data =
    if response.data.is_a?(Array)
      response.data.map { |subset| new(subset) }
    else
      new(response.data)
    end

  response
end

.type_cast_schemaObject



10
11
12
# File 'lib/form_stalker/data/base.rb', line 10

def self.type_cast_schema
  @type_cast_schema ||= { id: :integer }
end

Instance Method Details

#attributes_before_type_castObject



47
48
49
# File 'lib/form_stalker/data/base.rb', line 47

def attributes_before_type_cast
  parser.attributes_before_type_cast
end