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
# 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



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

def attribute(name)
  @attributes[name]
end

#attribute=(name, value) ⇒ Object

Write an attribute to the attributes hash



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

def attribute=(name, value)
  @attributes[name] = value
end

#attribute_method?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

def attributes
  @attributes.dup
end

#inspectObject

Returns the class name plus its attributes

Examples:

Inspect the model.

person.inspect


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

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



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

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



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

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