Module: ActiveRemote::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Included in:
Base
Defined in:
lib/active_remote/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

Performs equality checking on the result of attributes and its type.

Examples:

Compare for equality.

model == other


15
16
17
18
19
# File 'lib/active_remote/attributes.rb', line 15

def ==(other)
  return false unless other.instance_of?(self.class)

  attributes == other.attributes
end

#attribute(name) ⇒ Object

Read an attribute from the attributes hash



65
66
67
# File 'lib/active_remote/attributes.rb', line 65

def attribute(name)
  @attributes[name]
end

#attribute=(name, value) ⇒ Object

Write an attribute to the attributes hash



71
72
73
# File 'lib/active_remote/attributes.rb', line 71

def attribute=(name, value)
  @attributes[name] = self.class.attributes[name].from_user(value)
end

#attribute_method?(attr_name) ⇒ Boolean



75
76
77
78
79
# File 'lib/active_remote/attributes.rb', line 75

def attribute_method?(attr_name)
  # Check if @attributes is defined because dangerous_attribute? method
  # can check allocate.respond_to? before actaully calling initialize
  defined?(@attributes) && @attributes.key?(attr_name)
end

#attributesObject

Returns a copy of our attributes hash



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

def attributes
  @attributes.dup
end

#inspectObject

Returns the class name plus its attributes

Examples:

Inspect the model.

person.inspect


31
32
33
34
35
# File 'lib/active_remote/attributes.rb', line 31

def inspect
  attribute_descriptions = attributes.sort.map { |key, value| "#{key}: #{value.inspect}" }.join(", ")
  separator = " " unless attribute_descriptions.empty?
  "#<#{self.class.name}#{separator}#{attribute_descriptions}>"
end

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

Read attribute from the attributes hash



39
40
41
42
43
44
45
46
47
# File 'lib/active_remote/attributes.rb', line 39

def read_attribute(name)
  name = name.to_s

  if respond_to?(name)
    attribute(name)
  else
    raise ::ActiveRemote::UnknownAttributeError, "unknown attribute: #{name}"
  end
end

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

Update an attribute in the attributes hash



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

def write_attribute(name, value)
  name = name.to_s

  if respond_to?("#{name}=")
    __send__("attribute=", name, value)
  else
    raise ::ActiveRemote::UnknownAttributeError, "unknown attribute: #{name}"
  end
end