File: attribute.rb

package info (click to toggle)
ruby-anima 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 76 kB
  • sloc: ruby: 94; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 1,065 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Anima
  # An attribute
  class Attribute
    include Adamantium::Flat, Equalizer.new(:name)

    # Initialize attribute
    #
    # @param [Symbol] name
    def initialize(name)
      @name, @instance_variable_name = name, :"@#{name}"
    end

    # Return attribute name
    #
    # @return [Symbol]
    attr_reader :name

    # Return instance variable name
    #
    # @return [Symbol]
    attr_reader :instance_variable_name

    # Load attribute
    #
    # @param [Object] object
    # @param [Hash] attributes
    #
    # @return [self]
    def load(object, attributes)
      set(object, attributes.fetch(name))
    end

    # Get attribute value from object
    #
    # @param [Object] object
    #
    # @return [Object]
    def get(object)
      object.public_send(name)
    end

    # Set attribute value in object
    #
    # @param [Object] object
    # @param [Object] value
    #
    # @return [self]
    def set(object, value)
      object.instance_variable_set(instance_variable_name, value)

      self
    end
  end # Attribute
end # Anima