Parent

Included Modules

DataMapper::Property::Flag

Public Class Methods

new(model, name, options = {}) click to toggle source
# File lib/dm-types/flag.rb, line 10
def initialize(model, name, options = {})
  super

  @flag_map = {}

  flags = options.fetch(:flags, self.class.flags)
  flags.each_with_index do |flag, i|
    flag_map[i] = flag
  end
end

Public Instance Methods

dump(value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.