Class: Anima::Attribute

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/anima/attribute.rb

Overview

An attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Attribute

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.

Initialize attribute

Parameters:

  • name (Symbol)


15
16
17
# File 'lib/anima/attribute.rb', line 15

def initialize(name)
  @name, @instance_variable_name = name, :"@#{name}"
end

Instance Attribute Details

#nameSymbol (readonly)

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.

Return attribute name

Returns:

  • (Symbol)


25
26
27
# File 'lib/anima/attribute.rb', line 25

def name
  @name
end

Instance Method Details

#get(object) ⇒ Object

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.

Get attribute value from object

Parameters:

  • object (Object)

Returns:

  • (Object)


52
53
54
# File 'lib/anima/attribute.rb', line 52

def get(object)
  object.public_send(name)
end

#load(object, attributes) ⇒ self

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.

Load attribute

Parameters:

  • object (Object)
  • attributes (Hash)

Returns:

  • (self)


36
37
38
39
40
41
42
# File 'lib/anima/attribute.rb', line 36

def load(object, attributes)
  value = attributes.fetch(name) do |attribute_name|
    fail Error::Missing.new(object, attribute_name)
  end

  set(object, value)
end

#set(object, value) ⇒ self

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.

Set attribute value in object

Parameters:

  • object (Object)
  • value (Object)

Returns:

  • (self)


65
66
67
68
69
# File 'lib/anima/attribute.rb', line 65

def set(object, value)
  object.instance_variable_set(@instance_variable_name, value)

  self
end