Class: DataMapper::Types::Flag

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb

Class Method Summary collapse

Class Method Details

.[](*flags) ⇒ Object



27
28
29
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb', line 27

def self.[](*flags)
  new(*flags)
end

.dump(value, property) ⇒ Object



46
47
48
49
50
51
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb', line 46

def self.dump(value, property)
  return if value.nil?
  flags = value.is_a?(Array) ? value : [value]
  flags.map!{ |f| f.to_sym }
  flag_map.invert.values_at(*flags.flatten).compact.inject(0) { |sum, i| sum + i }
end

.flag_mapObject



8
9
10
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb', line 8

def self.flag_map
  @flag_map
end

.flag_map=(value) ⇒ Object



12
13
14
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb', line 12

def self.flag_map=(value)
  @flag_map = value
end

.inherited(target) ⇒ Object



4
5
6
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb', line 4

def self.inherited(target)
  target.instance_variable_set("@primitive", self.primitive)
end

.load(value, property) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb', line 31

def self.load(value, property)
  begin
    matches = []

    0.upto((Math.log(value) / Math.log(2)).ceil) do |i|
      pow = 2 ** i
      matches << flag_map[pow] if value & pow == pow
    end

    matches.compact
  rescue TypeError, Errno::EDOM
    []
  end
end

.new(*flags) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb', line 16

def self.new(*flags)
  type = Class.new(Flag)
  type.flag_map = {}

  flags.each_with_index do |flag, i|
    type.flag_map[2 ** i] = flag
  end

  type
end

.typecast(value, property) ⇒ Object



53
54
55
56
57
58
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/flag.rb', line 53

def self.typecast(value, property)
  case value
  when Array then value.map {|v| v.to_sym}
  else value.to_sym
  end
end