Class: EventbriteSDK::Resource::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/eventbrite_sdk/resource/attributes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hydrated_attrs = {}, schema = NullSchemaDefinition.new) ⇒ Attributes

Returns a new instance of Attributes.



12
13
14
15
16
17
18
19
20
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 12

def initialize(hydrated_attrs = {}, schema = NullSchemaDefinition.new)
  @changes = {}
  @schema = schema

  # Build out initial hash based on schema's defined keys
  @attrs = schema.defined_keys.each_with_object({}) do |key, attrs|
    Field.new(key, nil).bury(attrs)
  end.merge(stringify_keys(hydrated_attrs))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args, &_block) ⇒ Object (private)



83
84
85
86
87
88
89
90
91
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 83

def method_missing(method_name, *_args, &_block)
  requested_key = method_name.to_s

  if attrs.key?(requested_key)
    handle_requested_attr(attrs[requested_key])
  else
    super
  end
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



4
5
6
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 4

def attrs
  @attrs
end

#changesObject (readonly)

Returns the value of attribute changes.



4
5
6
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 4

def changes
  @changes
end

Class Method Details

.build(attrs, schema) ⇒ Object



6
7
8
9
10
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 6

def self.build(attrs, schema)
  new({}, schema).tap do |instance|
    instance.assign_attributes(attrs)
  end
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 22

def [](key)
  public_send(key)
end

#assign_attributes(new_attrs) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 26

def assign_attributes(new_attrs)
  stringify_keys(new_attrs).each do |attribute_key, value|
    value = Field.new(attribute_key, value, schema: schema)
    changes.merge! value.apply(attrs, changes)
  end

  nil
end

#changed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 35

def changed?
  changes.any?
end

#inspectObject



47
48
49
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 47

def inspect
  "#<#{self.class}: #{JSON.pretty_generate(@attrs.to_h)}>"
end

#payload(prefix = nil) ⇒ Object

Provides changeset in a format that can be thrown at an endpoint

prefix: This is needed due to inconsistencies in the EB API

Sometimes there's a prefix, sometimes there's not,
sometimes it's singular, sometimes it's plural.
Once the API gets a bit more normalized we can remove this
altogether and infer a prefix based
on the class name of the resource


69
70
71
72
73
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 69

def payload(prefix = nil)
  changes.each_with_object({}) do |(attribute_key, (_, value)), payload|
    Field.new(attribute_key, value, prefix: prefix).bury(payload)
  end
end

#reset!Object



51
52
53
54
55
56
57
58
59
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 51

def reset!
  changes.each do |attribute_key, (old_value, _current_value)|
    Field.new(attribute_key, old_value).bury(attrs)
  end

  @changes = {}

  true
end

#to_hObject



39
40
41
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 39

def to_h
  attrs.to_h
end

#to_json(opts = {}) ⇒ Object



43
44
45
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 43

def to_json(opts = {})
  to_h.to_json(opts)
end

#valuesObject



75
76
77
# File 'lib/eventbrite_sdk/resource/attributes.rb', line 75

def values
  attrs.values
end