File: attribute.rb

package info (click to toggle)
ruby-unparser 0.6.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 936 kB
  • sloc: ruby: 7,691; sh: 6; makefile: 4
file content (59 lines) | stat: -rw-r--r-- 1,223 bytes parent folder | download
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
55
56
57
58
59
# frozen_string_literal: true

module Unparser
  class Anima
    # An attribute
    class Attribute
      include Adamantium, Equalizer.new(:name)

      # Initialize attribute
      #
      # @param [Symbol] name
      def initialize(name)
        @name = name
        @instance_variable_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
end # Unparser