Module: TinyDyno::Attributes

Extended by:
ActiveSupport::Concern
Includes:
Readonly
Included in:
DocumentComposition
Defined in:
lib/tiny_dyno/attributes.rb,
lib/tiny_dyno/attributes/readonly.rb

Defined Under Namespace

Modules: Readonly

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Readonly

#attribute_writable?

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



11
12
13
# File 'lib/tiny_dyno/attributes.rb', line 11

def attributes
  @attributes
end

Instance Method Details

#attribute_missing?(name) ⇒ true, false

Determine if the attribute is missing from the document, due to loading it from the database with missing fields.

Examples:

Is the attribute missing?

document.attribute_missing?("test")

Parameters:

  • name (String)

    The name of the attribute.

Returns:

  • (true, false)

    If the attribute is missing.

Since:

  • 4.0.0



132
133
134
# File 'lib/tiny_dyno/attributes.rb', line 132

def attribute_missing?(name)
  return (!self.fields.keys.include?(name))
end

#process_attribute(name, value) ⇒ Object

If the attribute is dynamic, add a field for it with a type of object and then either way set the value.

Examples:

Process the attribute.

document.process_attribute(name, value)

Parameters:

  • name (Symbol)

    The name of the field.

  • value (Object)

    The value of the field.

Raises:

Since:

  • 2.0.0.rc.7



97
98
99
100
101
# File 'lib/tiny_dyno/attributes.rb', line 97

def process_attribute(name, value)
  responds = respond_to?("#{name}=", true)
  raise TinyDyno::Errors::UnknownAttribute.new(self.class, name) unless responds
  send("#{name}=", value)
end

#process_attributes(attrs = nil) ⇒ Object

Process the provided attributes casting them to their proper values if a field exists for them on the document. This will be limited to only the attributes provided in the suppied Hash so that no extra nil values get put into the document’s attributes.

Examples:

Process the attributes.

person.process_attributes(:title => "sir", :age => 40)

Parameters:

  • attrs (Hash) (defaults to: nil)

    The attributes to set.

Since:

  • 2.0.0.rc.7



76
77
78
79
80
81
82
83
84
85
# File 'lib/tiny_dyno/attributes.rb', line 76

def process_attributes(attrs = nil)
  attrs ||= {}
  if !attrs.empty?
    attrs.each_pair do |key, value|
      process_attribute(key, value)
    end
  end
  # context?
  # yield self if block_given?
end

#read_attribute(name) ⇒ Object Also known as: []

Read a value from the document attributes. If the value does not exist it will return nil.

Examples:

Read an attribute.

person.read_attribute(:title)

Read an attribute (alternate syntax.)

person[:title]

Parameters:

  • name (String, Symbol)

    The name of the attribute to get.

Returns:

  • (Object)

    The value of the attribute.

Since:

  • 1.0.0



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

def read_attribute(name)
  normalized = database_field_name(name.to_s)
  if attribute_missing?(normalized)
    raise ActiveModel::MissingAttributeError, "Missing attribute: '#{name}'."
  end
  attributes[normalized]
end

#typed_value_for(key, value) ⇒ Object

Return the typecasted value for a field. Based on the field option, type which is mandatory

Examples:

Get the value typecasted.

person.typed_value_for(:title, :sir)

Parameters:

  • key (String, Symbol)

    The field name.

  • value (Object)

    The uncast value.

Returns:

  • (Object)

    The cast value.

Raises:

  • (MissingAttributeError)

Since:

  • 1.0.0



116
117
118
119
# File 'lib/tiny_dyno/attributes.rb', line 116

def typed_value_for(key, value)
  raise MissingAttributeError if fields[key].nil?
  TinyDyno::Adapter.simple_attribute(field_type: self.fields[key].options[:type], value: value)
end

#write_attribute(name, value) ⇒ Object Also known as: []=

Write a single attribute to the document attribute hash. This will also fire the before and after update callbacks, and perform any necessary typecasting. called from within ActiveModel

Examples:

Write the attribute.

person.write_attribute(:title, "Mr.")

Write the attribute (alternate syntax.)

person[:title] = "Mr."

Parameters:

  • name (String, Symbol)

    The name of the attribute to update.

  • value (Object)

    The value to set for the attribute.

Since:

  • 1.0.0



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tiny_dyno/attributes.rb', line 52

def write_attribute(name, value)
  access = database_field_name(name.to_s)
  typed_value = typed_value_for(access, value)
  if attribute_writable?(access)
    unless attributes[access] == typed_value|| attribute_changed?(access)
      attribute_will_change!(access)
    end
    attributes[access] = typed_value
    typed_value
  end
end