Sha256: 6b7c1ddf4ea677ad5eea50bd74c49c49b305bf041d0572578184b511e3c41bbd

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'bitmask_attributes/definition'
require 'bitmask_attributes/value_proxy'

module BitmaskAttributes
  extend ActiveSupport::Concern

  module ClassMethods
    def bitmask(attribute, options={}, &extension)
      unless options[:as] && options[:as].kind_of?(Array)
        raise ArgumentError, "Must provide an Array :as option"
      end

      if default = options[:default]
        after_initialize do
          send("#{attribute}=", default) unless send("#{attribute}?") || persisted?
        end
      end

      bitmask_definitions[attribute] = Definition.new(attribute, 
                                                      options[:as].to_a, 
                                                      options[:null].nil? || options[:null], 
                                                      options[:zero_value], 
                                                      &extension)

      bitmask_definitions[attribute].install_on(self)
    end

    def bitmask_definitions
      base_class.base_class_bitmask_definitions
    end

    def bitmasks
      base_class.base_class_bitmasks
    end

    protected

    def base_class_bitmask_definitions
      @bitmask_definitions ||= {}
    end

    def base_class_bitmasks
      @bitmasks ||= {}
    end
  end

  def reload(*)
    super
    self.class.bitmasks.keys.each{|attribute| self.send("reload_#{attribute}")}
    self
  end
end

ActiveRecord::Base.send :include, BitmaskAttributes

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bitmask_attributes-1.0.0 lib/bitmask_attributes.rb