Sha256: 235fed888181518333d19b0c3878e163e728aa11f9091497769e4df49bbf8274

Contents?: true

Size: 898 Bytes

Versions: 5

Compression:

Stored size: 898 Bytes

Contents

# frozen_string_literal: true

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

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

      def #{accessor}?
        !( self.#{origin}.to_i & #{mask} ).zero?
      end

      def #{accessor}=(value)
        booleans = self.#{origin}.to_i

        result = self.act_with_booleans.to_boolean(value)
        if result
          booleans |= #{mask}
        else
          booleans &= ~#{mask}
        end
        self.#{origin} = booleans

        result
      end
    ), __FILE__, __LINE__ - 22
  end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
act_with_booleans-0.0.5 lib/act_with_booleans/define.rb
act_with_booleans-0.0.4 lib/act_with_booleans/define.rb
act_with_booleans-0.0.3 lib/act_with_booleans/define.rb
act_with_booleans-0.0.2 lib/act_with_booleans/define.rb
act_with_booleans-0.0.1 lib/act_with_booleans/define.rb