Sha256: 5428b30d7f22094c1ab205bf70bbdca1074e0a1649647999a1038dbb4bd77ab7

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'active_support'
require 'simple_flaggable_column/version'

module SimpleFlaggableColumn
  extend ActiveSupport::Concern

  def self.symbols_to_flags(symbols, symbols_flags)
    symbols.map{|s| symbols_flags[s]}.compact.reduce(:|) || 0
  end

  def self.flags_to_symbols(flags, symbols_flags)
    symbols_flags.each_pair.inject([]){|all, v| (flags & v[1] != 0) ? (all << v[0]) : all}
  end

  module ClassMethods
    def flag_column(name, symbols_flags)
      flags_symbols = symbols_flags.invert

      define_singleton_method :"#{name}_flags" do |*symbols|
        if symbols.empty?
          symbols_flags
        else
          SimpleFlaggableColumn.symbols_to_flags(symbols, symbols_flags)
        end
      end

      define_singleton_method :"flags_#{name}" do
        flags_symbols
      end

      define_method "#{name}=" do |symbols|
        write_attribute name, SimpleFlaggableColumn.symbols_to_flags(symbols, symbols_flags)
      end

      define_method name do
        SimpleFlaggableColumn.flags_to_symbols(read_attribute(name), symbols_flags)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_flaggable_column-0.0.1 lib/simple_flaggable_column.rb