class Atheneum::Storage

Attributes

strategy[R]

Public Class Methods

generate(strategy_name, attributes, options) click to toggle source
# File lib/atheneum/storage.rb, line 38
def self.generate strategy_name, attributes, options
  strategy = Strategy.find strategy_name
  storage = new strategy.new(options)
  storage.for attributes
end
new(strategy) click to toggle source
# File lib/atheneum/storage.rb, line 4
def initialize(strategy)
  @strategy = strategy
end

Public Instance Methods

for(records) click to toggle source
# File lib/atheneum/storage.rb, line 10
def for(records)
  make_module(records, strategy)
end
make_module(records, strategy) click to toggle source
# File lib/atheneum/storage.rb, line 14
def make_module(records, strategy)
  Module.new do
    records.each do |record|
      define_method "#{record}=", ->(item){
        self.send "#{strategy.store_for(record)}=", strategy.pack(item)
      }

      define_method record, -> (){
        strategy.unpack(self.send("#{strategy.store_for(record)}"))
      }
    end

    if strategy.privatise?
      define_singleton_method :'included', ->(klass){
        records.each do |record|
          klass.send :private, strategy.store_for(record)
          klass.send :private, "#{strategy.store_for(record)}="
        end
      }
    end

  end
end