Skip to content
Browse files

fix index error

  • Loading branch information...
1 parent 86a0410 commit fffb4e1b1355b745f6f40dae00a58b0af33bcbd1 afly committed Apr 7, 2012
Showing with 8 additions and 6 deletions.
  1. +8 −6 lib/bitmask_attributes/definition_for_index.rb
View
14 lib/bitmask_attributes/definition_for_index.rb
@@ -47,7 +47,7 @@ def override(model)
def override_getter_on(model)
model.class_eval %(
def #{attribute}
- @#{attribute} ||= self.class.bitmask_definitions[:#{attribute}].values[( self[:#{attribute}] || -1)]
+ @#{attribute} ||= self.class.bitmask_definitions[:#{attribute}].values[( self[:#{attribute}] || -9999)]
end
)
end
@@ -56,11 +56,11 @@ def override_setter_on(model)
model.class_eval %(
def #{attribute}=(raw_value)
if raw_value.kind_of?(Integer) then
- self[:#{attribute}] = raw_value.between?(0,self.class.bitmask_definitions[:#{attribute}].values.size - 1) ? raw_value : -1
+ self[:#{attribute}] = raw_value.between?(0,self.class.bitmask_definitions[:#{attribute}].values.size - 1) ? raw_value : -9999
else
- self[:#{attribute}] = self.class.bitmask_definitions[:#{attribute}].values.find_index(raw_value) || -1
+ self[:#{attribute}] = self.class.bitmask_definitions[:#{attribute}].values.find_index(raw_value) || -9999
end
-
+ @#{attribute} = self.class.bitmask_definitions[:#{attribute}].values[self[:#{attribute}] ]
end
)
end
@@ -120,7 +120,8 @@ def create_scopes_on(model)
scope :with_#{attribute},
proc { |value|
if value
- mask = value.kind_of?(Integer) ? value : self.bitmask_definitions[:#{attribute}].values.find_index(value)
+ mask = value.match(/^\d+$/)
+ ? value.to_i : self.bitmask_definitions[:#{attribute}].values.find_index(value)
where('#{attribute} = ?', mask )
else
where("#{attribute} >= 0 ")
@@ -130,7 +131,8 @@ def create_scopes_on(model)
scope :without_#{attribute},
proc { |value|
if value
- mask = value.kind_of?(Integer) ? value : self.bitmask_definitions[:#{attribute}].values.find_index(value)
+ mask = value.match(/^\d+$/)
+ ? value.to_i : self.bitmask_definitions[:#{attribute}].values.find_index(value)
where('#{attribute} <> ?', mask )
else
where("#{attribute} IS NULL OR #{attribute} < 0 ")

0 comments on commit fffb4e1

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