Sha256: ba2513ef0157816634196f93c6723b9f3255714897d901525d68649d9a83bd6a

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require "active_model"

module Matic
  extend ActiveSupport::Concern
  include ActiveModel::Dirty

  module ClassMethods
    def collection_name
      self.name.tableize
    end

    def field(attr_name)
      # Plagiarized from ActiveModel
      attribute_method_matchers.each do |matcher|
        unless instance_method_already_implemented?(matcher.method_name(attr_name))
          generate_method = "define_method_#{matcher.prefix}attribute#{matcher.suffix}"

          if respond_to?(generate_method)
            send(generate_method, attr_name)
          else
            method_name = matcher.method_name(attr_name)

            generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
              if method_defined?(:#{method_name})
                undef :#{method_name}
              end
              def #{method_name}(*args)
                send(:#{matcher.method_missing_target}, '#{attr_name}', *args)
              end
            STR
          end
        end
      end

      define_method(attr_name) do
        self[attr_name.to_s]
      end

      define_method("#{attr_name}=") do |val|
        eval("#{attr_name}_will_change!") unless val == self[attr_name.to_s]
        self[attr_name.to_s] = val
      end
    end
  end

  def insert(opts={})
    if super
      @previously_changed = changes
      @changed_attributes.clear
    end
  end

  def insert!(opts={})
    insert(opts.merge(:safe => true))
  end

  def update(opts={}, update_doc=@doc)
    if super
      @previously_changed = changes
      @changed_attributes.clear
    end
  end

  def update!(opts={}, update_doc=@doc)
    update(opts.merge(:safe => true), update_doc)
  end

  def save
    is_new ? insert : update
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
matic-0.1.0 lib/matic.rb