@@ -7,6 +7,8 @@ module Attributes
7
7
with_options instance_writer : false , instance_reader : false do |serializer |
8
8
serializer . class_attribute :_attributes_data # @api private
9
9
self . _attributes_data ||= { }
10
+ serializer . class_attribute :_attributes_conditions # @api private
11
+ self . _attributes_conditions ||= { }
10
12
end
11
13
12
14
autoload :Attribute
@@ -16,6 +18,7 @@ module Attributes
16
18
def attributes ( requested_attrs = nil , reload = false )
17
19
@attributes = nil if reload
18
20
@attributes ||= self . class . _attributes_data . values . each_with_object ( { } ) do |attr , hash |
21
+ next unless attr . include? ( self )
19
22
next unless requested_attrs . nil? || requested_attrs . include? ( attr . key )
20
23
hash [ attr . key ] = attr . value ( self )
21
24
end
@@ -53,7 +56,16 @@ def attributes(*attrs)
53
56
# end
54
57
def attribute ( attr , options = { } , &block )
55
58
key = options . fetch ( :key , attr )
56
- _attributes_data [ attr ] = Attribute . new ( attr , key , block )
59
+ condition_type =
60
+ if options . key? ( :if )
61
+ :if
62
+ elsif options . key? ( :unless )
63
+ :unless
64
+ else
65
+ :none
66
+ end
67
+ condition = options [ condition_type ]
68
+ _attributes_data [ attr ] = Attribute . new ( attr , key , block , condition_type , condition )
57
69
end
58
70
59
71
# @api private
0 commit comments