Class: ActWithFlags::Admin

Inherits:
Object
  • Object
show all
Defined in:
lib/act_with_flags/admin.rb,
lib/act_with_flags/clear.rb,
lib/act_with_flags/flags.rb,
lib/act_with_flags/print.rb,
lib/act_with_flags/utils.rb,
lib/act_with_flags/define.rb

Defined Under Namespace

Classes: Location

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Admin

Returns a new instance of Admin.



6
7
8
9
10
11
12
13
14
# File 'lib/act_with_flags/admin.rb', line 6

def initialize(model)
  @locations = {}
  @clears = {}
  @ranges = {}
  @model = model
  @boolean_hash = {}
  [true, "true", 1, "1"].each { |x| @boolean_hash[x] = true }
  [false, "false", 0, "0"].each { |x| @boolean_hash[x] = false }
end

Instance Attribute Details

#clearsObject (readonly)

Returns the value of attribute clears.



4
5
6
# File 'lib/act_with_flags/clear.rb', line 4

def clears
  @clears
end

#locationsObject (readonly)

Returns the value of attribute locations.



6
7
8
# File 'lib/act_with_flags/flags.rb', line 6

def locations
  @locations
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/act_with_flags/admin.rb', line 4

def model
  @model
end

#rangesObject (readonly)

Returns the value of attribute ranges.



4
5
6
# File 'lib/act_with_flags/admin.rb', line 4

def ranges
  @ranges
end

Instance Method Details

#add_accessors(accessor, origin, mask) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/act_with_flags/define.rb', line 4

def add_accessors(accessor, origin, mask)
  unless model.method_defined?(:act_with_flags)
    model.class_eval %(
      def act_with_flags
        #{model}.act_with_flags
      end
    ), __FILE__, __LINE__ - 4
  end

  model.class_eval %(
    def #{accessor}
      #{accessor}?
    end

    def #{accessor}?
      raise "Uninitialized '#{model}.#{origin}'" if #{origin}.nil?
      if #{origin}.is_a?(String)
        flags = self.#{origin}.to_i
        !( flags & #{mask} ).zero?
      else
        !( self.#{origin} & #{mask} ).zero?
      end
    end

    def #{accessor}=(value)
      raise "Uninitialized '#{model}.#{origin}'" if #{origin}.nil?
      is_a_string = #{origin}.is_a?(String)
      flags = self.#{origin}.to_i
      flags ||= 0

      result = self.act_with_flags.to_boolean(value)
      if result
        flags |= #{mask}
      else
        flags &= ~#{mask}
      end
      self.#{origin} = is_a_string ? flags.to_s : flags

      result
    end
  ), __FILE__, __LINE__ - 31
end

#add_flag(name, pos, origin) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/act_with_flags/utils.rb', line 4

def add_flag(name, pos, origin)
  range = ranges[origin]
  accessor = name.to_sym
  validate_accessor accessor, "#{accessor}?", "#{accessor}="

  pos = check_pos(model, origin, pos)
  msg = "Invalid position <#{pos}>"
  raise(ArgumentError, msg) unless pos.is_a?(Integer)
  raise(ArgumentError, msg) unless pos >= 0
  loc = Location.new(model, origin, pos)
  add_to_locations accessor, loc

  validate_position(range, pos)

  mask = mask(accessor)
  add_accessors(accessor, origin, mask)
end

#add_mask_et_all(origin) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/act_with_flags/utils.rb', line 22

def add_mask_et_all(origin)
  model.class_eval %(
    def flags_mask(*names)
      self.class.act_with_flags.mask(*names)
    end

    def flags_any?(*names)
      mask = self.class.act_with_flags.mask(*names)
      !( self.#{origin} & mask ).zero?
    end

    def flags_all?(*names)
      mask = self.class.act_with_flags.mask(*names)
      ( self.#{origin} & mask ) == mask
    end

    def flags_none?(*names)
      mask = self.class.act_with_flags.mask(*names)
      ( self.#{origin} & mask ).zero?
    end
  ), __FILE__, __LINE__ - 19
end

#clear_at_save(*flags) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/act_with_flags/clear.rb', line 6

def clear_at_save(*flags)
  flags.each { |name| add_to_clear_mask(name) }
  clears.each { |orig, mask|
    before_save(orig, mask)
  }
  @clears = {}
end

#location(name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/act_with_flags/flags.rb', line 28

def location(name)
  location = @locations[name]
  return location if location

  parent = model.superclass.act_with_flags
  return parent.location(name) if parent

  raise "unknown flag '#{model}##{name}'"
end

#mask(*flags) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/act_with_flags/flags.rb', line 8

def mask(*flags)
  return 0 if flags.empty?

  res = mask2d(*flags)
  raise "Mixing origins fails: #{flags}" unless res.length == 1

  res.values.first
end

#mask2d(*flags) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/act_with_flags/flags.rb', line 17

def mask2d(*flags)
  res = {}
  flags.each { |flag|
    model, orig, pos = location(flag).values
    idx = "#{model}##{orig}"
    mask = res[idx] || 0
    res[idx] = mask | (1 << pos)
  }
  res
end

#remove_accessor(accessor) ⇒ Object



47
48
49
# File 'lib/act_with_flags/define.rb', line 47

def remove_accessor(accessor)
  my_undef model, accessor, "#{accessor}?", "#{accessor}="
end

#resetObject



45
46
47
48
49
50
51
# File 'lib/act_with_flags/utils.rb', line 45

def reset
  names = @locations.keys.sort
  names.each { |name|
    remove_accessor name
  }
  reset_model model
end

#reset_model(model) ⇒ Object



16
17
18
# File 'lib/act_with_flags/admin.rb', line 16

def reset_model(model)
  initialize model
end

#to_boolean(value) ⇒ Object



20
21
22
23
24
25
# File 'lib/act_with_flags/admin.rb', line 20

def to_boolean(value)
  res = @boolean_hash[value]
  return res unless res.nil?

  raise "invalid boolean <#{value}>"
end

#to_sObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/act_with_flags/print.rb', line 4

def to_s
  res = []
  res << title("Variables")
  res << variables(:boolean_hash)

  res << blk("Flags sorted alfabetically") { |key, loc|
    "#{key} #{loc}"
  }
  res << blk("Flags and mask; sorted alfabetically") { |key, loc|
    "#{key}  #{sprintf("0x%08X", mask(key))}"
  }
  res << blk("FLAG assignment; sorted alfabetically") { |key, loc|
    "FLAG_#{key.upcase} = #{sprintf("0x%08X", mask(key))}"
  }

  res << title("@ranges")
  res << @ranges
  res << title("@locations")
  res << @locations
  res.flatten.join("\n")
end

#validate_position(range, position) ⇒ Object

Raises:

  • (RangeError)


53
54
55
56
57
58
59
# File 'lib/act_with_flags/utils.rb', line 53

def validate_position(range, position)
  return if range.nil?
  return if range.cover?(position)

  msg = "Position #{position} out of range #{range}"
  raise RangeError, msg
end