Class: EventbriteSDK::Resource::Field

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

Constant Summary collapse

SIBLING_KEYMAP =
{ 'timezone' => 'utc', 'utc' => 'timezone' }.freeze
SIBLING_REGEX =
/\A(.+)\.(utc|timezone)\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, prefix: nil, schema: NullSchemaDefinition.new) ⇒ Field

Returns a new instance of Field.



9
10
11
12
13
14
# File 'lib/eventbrite_sdk/resource/field.rb', line 9

def initialize(key, value, prefix: nil, schema: NullSchemaDefinition.new)
  @key = (prefix && "#{prefix}.#{key}") || key
  @schema = schema
  @value = value
  @datetime = false
end

Instance Attribute Details

#initial_valueObject (readonly)

Returns the value of attribute initial_value.



7
8
9
# File 'lib/eventbrite_sdk/resource/field.rb', line 7

def initial_value
  @initial_value
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/eventbrite_sdk/resource/field.rb', line 7

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/eventbrite_sdk/resource/field.rb', line 7

def value
  @value
end

Instance Method Details

#apply(attrs, existing_changes) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/eventbrite_sdk/resource/field.rb', line 30

def apply(attrs, existing_changes)
  if writeable?
    changes(attrs, existing_changes).tap { bury(attrs) }
  else
    {}
  end
end

#bury(hash = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/eventbrite_sdk/resource/field.rb', line 38

def bury(hash = {})
  nested_keys = keys
  # Hand rolling #bury, hopefully we get it in the next release of Ruby.
  # UPDATE: we won't https://bugs.ruby-lang.org/issues/11747
  nested_keys.each_cons(2).reduce(hash) do |prev_attrs, (nkey, _)|
    prev_attrs[nkey] ||= {}
  end[nested_keys.last] = value

  hash
end

#changes(attrs, changes) ⇒ Object



16
17
18
19
20
# File 'lib/eventbrite_sdk/resource/field.rb', line 16

def changes(attrs, changes)
  %i[basic_changes rich_changes].reduce(changes) do |diff, method|
    send(method, attrs, diff)
  end
end

#keysObject



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

def keys
  key.split('.')
end

#writeable?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/eventbrite_sdk/resource/field.rb', line 26

def writeable?
  schema.writeable?(key)
end