Sha256: b7dd50af07af975afcc43092e79634cf31d5259c92050ebd0ae4a137e8201260

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

class ActWithFlags::Admin
  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

  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

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

  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

  private

  def validate_accessor(*names)
    names.each { |acc|
      raise "redefining #{acc} rejected" if model.method_defined?(acc)
    }
  end

  def my_undef(*names)
    names.each { |name|
      model.class_eval %(
        begin
          undef #{name}
        rescue
        end
      ), __FILE__, __LINE__ - 5
    }
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
act_with_flags-3.1.9 lib/act_with_flags/utils.rb
act_with_flags-3.1.6 lib/act_with_flags/utils.rb
act_with_flags-3.1.5 lib/act_with_flags/utils.rb
act_with_flags-3.1.3 lib/act_with_flags/utils.rb
act_with_flags-3.1.2 lib/act_with_flags/utils.rb
act_with_flags-3.1.1 lib/act_with_flags/utils.rb