Sha256: eb13faa26055018a18e40ae3a11fb7d7c5d3eb3b7818d4809cc9f39a4cf5a819

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require 'metabolical/meta_datum'
require 'metabolical/scopes'

module Metabolical
  class Error < StandardError; end

  def self.included(klass)
    klass.extend ClassMethods
  end

  module ClassMethods
    def metabolize!
      if self.respond_to?(:named_scope)
        include Metabolical::AR3Scopes
      else
        include Metabolical::AR4Scopes
      end

      class_eval do
        has_many :metas, :as => :metabolized, :class_name => 'Metabolical::MetaDatum' do
          def [](key)
            owner = (self.respond_to?(:proxy_association) ? self.proxy_association.owner : self.proxy_owner)
            (owner.metas.loaded? && owner.metas.detect { |m| m.key == key }) || find_by(key: key) || owner.metas.load.build(:key => key)
          end

          def []=(key, data)
            owner = (self.respond_to?(:proxy_association) ? self.proxy_association.owner : self.proxy_owner)
            meta = self[key]
            meta.data = data
            meta.save unless owner.new_record?
            meta
          end
        end
      end

    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metabolical-1.2.2 lib/metabolical.rb