Class: DataMapper::Types::Flag
- Inherits:
-
Type
- Object
- Type
- DataMapper::Types::Flag
- Defined in:
- lib/dm-types/flag.rb
Class Method Summary collapse
- .[](*flags) ⇒ Object
- .dump(value, property) ⇒ Object
- .flag_map ⇒ Object
- .flag_map=(value) ⇒ Object
- .inherited(target) ⇒ Object
- .load(value, property) ⇒ Object
- .new(*flags) ⇒ Object
- .typecast(value, property) ⇒ Object
Class Method Details
.[](*flags) ⇒ Object
29 30 31 |
# File 'lib/dm-types/flag.rb', line 29 def self.[](*flags) new(*flags) end |
.dump(value, property) ⇒ Object
49 50 51 52 53 |
# File 'lib/dm-types/flag.rb', line 49 def self.dump(value, property) return if value.nil? flags = Array(value).map { |flag| flag.to_sym }.flatten flag_map.invert.values_at(*flags).compact.inject(0) { |sum, i| sum += 1 << i } end |
.flag_map ⇒ Object
10 11 12 |
# File 'lib/dm-types/flag.rb', line 10 def self.flag_map @flag_map end |
.flag_map=(value) ⇒ Object
14 15 16 |
# File 'lib/dm-types/flag.rb', line 14 def self.flag_map=(value) @flag_map = value end |
.inherited(target) ⇒ Object
6 7 8 |
# File 'lib/dm-types/flag.rb', line 6 def self.inherited(target) target.instance_variable_set('@primitive', self.primitive) end |
.load(value, property) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dm-types/flag.rb', line 33 def self.load(value, property) 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 |
.new(*flags) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dm-types/flag.rb', line 18 def self.new(*flags) type = Class.new(Flag) type.flag_map = {} flags.each_with_index do |flag, i| type.flag_map[i] = flag end type end |
.typecast(value, property) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/dm-types/flag.rb', line 55 def self.typecast(value, property) case value when nil then nil when Array then value.map {|v| v.to_sym} else value.to_sym end end |