Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: openxyz/bitmask_attributes
base: 67e7399565
...
head fork: openxyz/bitmask_attributes
compare: 86a0410b5e
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 13 additions and 6 deletions.
  1. +13 −6 lib/bitmask_attributes/definition_for_index.rb
View
19 lib/bitmask_attributes/definition_for_index.rb
@@ -9,7 +9,7 @@ def initialize(attribute, values=[], &extension)
end
def install_on(model)
- #validate_for model
+ validate_for model
#generate_bitmasks_on model
override model
#create_convenience_class_method_on model
@@ -47,25 +47,32 @@ def override(model)
def override_getter_on(model)
model.class_eval %(
def #{attribute}
- @#{attribute} ||= self.class.bitmask_definitions[:#{attribute}].values[( self[:#{attribute}] || 0)]
+ @#{attribute} ||= self.class.bitmask_definitions[:#{attribute}].values[( self[:#{attribute}] || -1)]
end
)
end
def override_setter_on(model)
model.class_eval %(
- def #{attribute}=(raw_value)
- self[:#{attribute}] = self.class.bitmask_definitions[:#{attribute}].values.find_index(raw_value) || 0
+ 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
+ else
+ self[:#{attribute}] = self.class.bitmask_definitions[:#{attribute}].values.find_index(raw_value) || -1
+ end
+
end
)
end
# Returns the defined values as an Array.
def create_attribute_methods_on(model)
+ #model.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
model.class_eval %(
def self.values_for_#{attribute}
self.bitmask_definitions[:#{attribute}].values
end
+
def self.values_with_bitmask_for_#{attribute}
ret = []
self.bitmask_definitions[:#{attribute}].values.each_with_index{|v,k| ret.push [v,k]}
@@ -113,7 +120,7 @@ def create_scopes_on(model)
scope :with_#{attribute},
proc { |value|
if value
- mask = self.bitmask_definitions[:#{attribute}].values.find_index(value)
+ mask = value.kind_of?(Integer) ? value : self.bitmask_definitions[:#{attribute}].values.find_index(value)
where('#{attribute} = ?', mask )
else
where("#{attribute} >= 0 ")
@@ -123,7 +130,7 @@ def create_scopes_on(model)
scope :without_#{attribute},
proc { |value|
if value
- mask = self.bitmask_definitions[:#{attribute}].values.find_index(value)
+ mask = value.kind_of?(Integer) ? value : self.bitmask_definitions[:#{attribute}].values.find_index(value)
where('#{attribute} <> ?', mask )
else
where("#{attribute} IS NULL OR #{attribute} < 0 ")

No commit comments for this range

Something went wrong with that request. Please try again.