@@ -4,13 +4,15 @@ class Entity < SimpleDelegator
4
4
extend Extensions ::ApiClient
5
5
6
6
class << self
7
- attr_accessor :entity_attributes
7
+ def entity_attributes
8
+ @entity_attributes ||= [ ]
9
+ end
8
10
9
- def attr_entity ( *attrs )
10
- @ entity_attributes = attrs
11
+ def attr_entity ( *attrs , & block )
12
+ entity_attributes . concat attrs
11
13
12
- entity_attributes . each do |attr |
13
- define_entity_attribute_getter ( attr )
14
+ attrs . each do |attr |
15
+ define_entity_attribute_getter ( attr , & block )
14
16
define_entity_attribute_setter ( attr )
15
17
end
16
18
end
@@ -43,7 +45,7 @@ def convert_to_entity(item, entity_type = self)
43
45
44
46
def define_entity_attribute_getter ( attr )
45
47
define_method attr . to_s do
46
- entity [ attr ]
48
+ block_given? ? yield ( entity [ attr ] ) : entity [ attr ]
47
49
end
48
50
end
49
51
@@ -56,7 +58,7 @@ def define_entity_attribute_setter(attr)
56
58
57
59
attr_reader :entity , :entity_status
58
60
59
- def initialize ( entity , status = true )
61
+ def initialize ( entity , entity_status = true )
60
62
raise EntityError , "#{ entity } must be Hash" unless entity . is_a? ( Hash )
61
63
@entity = Hashie ::Mash . new ( extract_attributes ( entity ) )
62
64
@entity_status = entity_status
@@ -73,8 +75,8 @@ def failure?
73
75
74
76
private
75
77
76
- def extract_attributes ( entity_attributes )
77
- entity_attributes . select { |key , value | self . class . entity_attributes . include? ( key . to_sym ) }
78
+ def extract_attributes ( attributes )
79
+ attributes . select { |key , _value | self . class . entity_attributes . include? ( key . to_sym ) }
78
80
end
79
81
end
80
82
0 commit comments