Class: DataMapper::Property::Flag
- Inherits:
-
Integer
- Object
- Integer
- DataMapper::Property::Flag
- Includes:
- Flags
- Defined in:
- lib/dm-types/flag.rb
Instance Method Summary collapse
- #dump(value) ⇒ Object
-
#initialize(model, name, options = {}) ⇒ Flag
constructor
A new instance of Flag.
- #load(value) ⇒ Object
- #typecast(value) ⇒ Object
Methods included from Flags
Constructor Details
#initialize(model, name, options = {}) ⇒ Flag
Returns a new instance of Flag.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dm-types/flag.rb', line 10 def initialize(model, name, = {}) super @flag_map = {} flags = .fetch(:flags, self.class.flags) flags.each_with_index do |flag, i| flag_map[i] = flag end end |
Instance Method Details
#dump(value) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dm-types/flag.rb', line 37 def dump(value) unless value.nil? flags = Array(value).map { |flag| flag.to_sym } flags.uniq! flag = 0 flag_map.invert.values_at(*flags).each do |i| next if i.nil? flag += (1 << i) end flag end end |
#load(value) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dm-types/flag.rb', line 21 def load(value) return [] if value.nil? || value <= 0 begin matches = [] 0.upto(flag_map.size - 1) do |i| matches << flag_map[i] if value[i] == 1 end matches.compact rescue TypeError, Errno::EDOM [] end end |
#typecast(value) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/dm-types/flag.rb', line 53 def typecast(value) case value when nil then nil when ::Array then value.map { |v| v.to_sym } else [value.to_sym] end end |