Module: CouchbaseOrm::AttributesDynamic

Extended by:
ActiveSupport::Concern
Defined in:
lib/couchbase-orm/attributes/dynamic.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used for allowing accessor methods for dynamic attributes.

Examples:

Call through method_missing.

document.method_missing(:test)

Parameters:

  • name (String | Symbol)

    The name of the method.

  • *args (Object...)

    The arguments to the method.

Returns:

  • (Object)

    The result of the method call.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/couchbase-orm/attributes/dynamic.rb', line 116

def method_missing(name, *args)
  attr = name.to_s
  return super unless attr.reader != 'id' && attributes.key?(attr.reader)

  getter = attr.reader
  if attr.writer?
    define_dynamic_writer(getter)
    @attributes.write_from_user(getter, args.first)
    args.first
  else
    define_dynamic_reader(getter)
    @attributes[getter].value
  end
end

Instance Method Details

#respond_to?(name, include_private = false) ⇒ true | false

Override respond_to? so it responds properly for dynamic attributes.

Examples:

Does this object respond to the method?

person.respond_to?(:title)

Parameters:

  • name (Array)

    The name of the method.

  • include_private (true | false) (defaults to: false)

Returns:

  • (true | false)

    True if it does, false if not.



17
18
19
# File 'lib/couchbase-orm/attributes/dynamic.rb', line 17

def respond_to?(name, include_private = false)
  super || attributes&.key?(name.to_s.reader)
end