Class: EventbriteSDK::Resource::Field
- Inherits:
-
Object
- Object
- EventbriteSDK::Resource::Field
- 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
-
#initial_value ⇒ Object
readonly
Returns the value of attribute initial_value.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #apply(attrs, existing_changes) ⇒ Object
- #bury(hash = {}) ⇒ Object
- #changes(attrs, changes) ⇒ Object
-
#initialize(key, value, prefix: nil, schema: NullSchemaDefinition.new) ⇒ Field
constructor
A new instance of Field.
- #keys ⇒ Object
- #writeable? ⇒ Boolean
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_value ⇒ Object (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 |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/eventbrite_sdk/resource/field.rb', line 7 def key @key end |
#value ⇒ Object (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 |
#keys ⇒ Object
22 23 24 |
# File 'lib/eventbrite_sdk/resource/field.rb', line 22 def keys key.split('.') end |
#writeable? ⇒ Boolean
26 27 28 |
# File 'lib/eventbrite_sdk/resource/field.rb', line 26 def writeable? schema.writeable?(key) end |