Sha256: 2e9d5908bb0cfb6ae9c8a917fa0d17dce35bd4bee17af85d07d668cf2a8796de

Contents?: true

Size: 1.2 KB

Versions: 8

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
act_with_flags-3.1.9 lib/act_with_flags/define.rb
act_with_flags-3.1.6 lib/act_with_flags/define.rb
act_with_flags-3.1.5 lib/act_with_flags/define.rb
act_with_flags-3.1.3 lib/act_with_flags/define.rb
act_with_flags-3.1.2 lib/act_with_flags/define.rb
act_with_flags-3.1.1 lib/act_with_flags/define.rb
act_with_flags-3.1.0 lib/act_with_flags/define.rb
act_with_flags-3.0.1 lib/act_with_flags/define.rb