Sha256: aa6fd61583ebe5025fc6604666ab4a24292059ad9f707cdc722393d1aa3f665a

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

module BitmaskAttributes
  class ValueProxy < Array
      
    def initialize(record, attribute, &extension)
      @record = record
      @attribute = attribute
      find_mapping
      instance_eval(&extension) if extension
      super(extract_values)
    end
    
    # =========================
    # = OVERRIDE TO SERIALIZE =
    # =========================
    
    %w(push << delete replace reject! select!).each do |override|
      class_eval(<<-EOEVAL)
        def #{override}(*args)
          (super).tap do
            updated!
          end
        end
      EOEVAL
    end
    
    def to_i
      inject(0) { |memo, value| memo | @mapping[value] }
    end
  
    private
    
      def validate!
        each do |value|
          if @mapping.key? value
            true
          else
            raise ArgumentError, "Unsupported value for `#{@attribute}': #{value.inspect}"
          end
        end
      end
    
      def updated!
        validate!
        uniq!
        serialize!
      end
    
      def serialize!
        @record.send(:write_attribute, @attribute, to_i)
      end
  
      def extract_values
        stored = [@record.send(:read_attribute, @attribute) || 0, 0].max
        @mapping.inject([]) do |values, (value, bitmask)|
          values.tap do
            values << value.to_sym if (stored & bitmask > 0)
          end
        end        
      end
    
      def find_mapping
        unless (@mapping = @record.class.bitmasks[@attribute])
          raise ArgumentError, "Could not find mapping for bitmask attribute :#{@attribute}"
        end
      end
      
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bitmask_attributes-0.2.2 lib/bitmask_attributes/value_proxy.rb
bitmask_attributes-0.2.1 lib/bitmask_attributes/value_proxy.rb
bitmask_attributes-0.1.1 lib/bitmask_attributes/value_proxy.rb
bitmask_attributes-0.1.0 lib/bitmask_attributes/value_proxy.rb