Class: DataMapper::Types::Flag

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

Class Method Summary collapse

Class Method Details

.[](*flags) ⇒ Object



24
25
26
# File 'lib/dm-types/flag.rb', line 24

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

.dump(value, property) ⇒ Object



43
44
45
46
47
48
# File 'lib/dm-types/flag.rb', line 43

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



5
6
7
# File 'lib/dm-types/flag.rb', line 5

def self.flag_map
  @flag_map
end

.flag_map=(value) ⇒ Object



9
10
11
# File 'lib/dm-types/flag.rb', line 9

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

.load(value, property) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dm-types/flag.rb', line 28

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



13
14
15
16
17
18
19
20
21
22
# File 'lib/dm-types/flag.rb', line 13

def self.new(*flags)
  type = Flag.clone  # cannot dup a Class
  type.flag_map = {}

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

  type
end