Skip to content

Commit

Permalink
implemented offset option for enum attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeremyanin committed Oct 26, 2011
1 parent 0656957 commit c0b96b5
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/magic_numbers.rb
Expand Up @@ -24,14 +24,14 @@ def magic_number_values(name)

def magic_number_for(name, value)
options = self.magic_number_options(name)
values = options[:values]
values, offset = options[:values], options[:offset] || 0

value = value.is_a?(Array) ? value.map(&:to_s) : value.to_s

if options[:type] == :bitfield
(values & value).map { |v| 2**values.index(v) }.sum
else
values.index(value) || options[:default]
(values.index(value) + offset) || options[:default]
end
end

Expand All @@ -42,7 +42,7 @@ def magic_number_options(name)
protected

def magic_number_attribute(name, options)
options.assert_valid_keys(:values, :type, :default, :column)
options.assert_valid_keys(:values, :type, :default, :column, :offset)
options[:values].map!(&:to_s)

options[:column] = name unless options[:column].present?
Expand All @@ -65,13 +65,13 @@ module InstanceMethods

def magic_number_read(name)
options = self.class.magic_number_options(name)
values = options[:values]
values, offset = options[:values], options[:offset] || 0
mask_value = self[options[:column]]

if options[:type] == :bitfield
result = values.reject { |r| ((mask_value || 0) & 2**values.index(r)).zero? }
else
(mask_value && values[mask_value].try(:to_sym))
(mask_value && values[mask_value - offset].try(:to_sym))
end
end

Expand Down

0 comments on commit c0b96b5

Please sign in to comment.