Skip to content
Browse files

make single value not as array

  • Loading branch information...
1 parent d5dd262 commit dde9411565ade1323032935c2d2d06a4f9a3833d afly committed Feb 2, 2012
Showing with 15 additions and 4 deletions.
  1. +15 −4 lib/bitmask_attributes/definition.rb
View
19 lib/bitmask_attributes/definition.rb
@@ -48,6 +48,12 @@ def override_getter_on(model)
model.class_eval %(
def #{attribute}
@#{attribute} ||= BitmaskAttributes::ValueProxy.new(self, :#{attribute}, &self.class.bitmask_definitions[:#{attribute}].extension)
+ @#{attribute}.size == 1 ? @#{attribute}.first : @#{attribute}
+ end
+
+ def #{attribute}_array
+ self.#{attribute}
+ @#{attribute}
end
)
end
@@ -56,7 +62,7 @@ def override_setter_on(model)
model.class_eval %(
def #{attribute}=(raw_value)
values = raw_value.kind_of?(Array) ? raw_value : [raw_value]
- self.#{attribute}.replace(values.reject(&:blank?))
+ self.#{attribute}_array.replace(values.reject(&:blank?))
end
)
end
@@ -67,6 +73,11 @@ def create_attribute_methods_on(model)
def self.values_for_#{attribute} # def self.values_for_numbers
#{values} # [:one, :two, :three]
end # end
+ def self.values_with_bitmask_for_#{attribute}
+ #{values}.map do |value|
+ [value, bitmasks[:#{attribute}][value] ]
+ end # [[:one,1], [:two,2], [:three,4]]
+ end
)
end
@@ -95,10 +106,10 @@ def #{attribute}_for_#{value}?
def #{attribute}?(*values)
if !values.blank?
values.all? do |value|
- self.#{attribute}.include?(value)
+ self.#{attribute}_array.include?(value)
end
else
- self.#{attribute}.present?
+ self.#{attribute}_array.present?
end
end
)
@@ -151,4 +162,4 @@ def create_scopes_on(model)
end
end
end
-end
+end

0 comments on commit dde9411

Please sign in to comment.
Something went wrong with that request. Please try again.