module ORMivore::Entity

Attributes

id[R]

Public Class Methods

included(base) click to toggle source
# File lib/ormivore/entity.rb, line 125
    def self.included(base)
      base.extend(ClassMethods)

      base.module_eval(<<-EOS)
        class Builder
          def initialize
            @attributes = {}
          end

          def id
            attributes[:id]
          end

          def adapter=(value)
            @adapter = value
          end

          # FactoryGirl integration point
          def save!
            @attributes = @adapter.create(attributes)
          end

          attr_reader :attributes
        end
      EOS
    end
new(attrs) click to toggle source
# File lib/ormivore/entity.rb, line 187
def initialize(attrs)
  coerced_attrs = attrs.symbolize_keys.tap { |h| self.class.coerce(h) }.freeze

  @base_attributes = {}.freeze
  @dirty_attributes = coerced_attrs

  self.class.validate_presence_of_proper_attributes(@base_attributes, @dirty_attributes)

  # TODO how to do custom validation?
  # validate
end

Public Instance Methods

apply(attrs) click to toggle source
# File lib/ormivore/entity.rb, line 162
def apply(attrs)
  self.dup.tap { |other|
    other.expand_changes(attrs)
  }
end
attributes() click to toggle source
# File lib/ormivore/entity.rb, line 154
def attributes
  all_attributes
end
changes() click to toggle source
# File lib/ormivore/entity.rb, line 158
def changes
  @dirty_attributes
end

Protected Instance Methods

expand_changes(attrs) click to toggle source

to be used only by change

# File lib/ormivore/entity.rb, line 171
def expand_changes(attrs)
  attrs = attrs.symbolize_keys.tap { |h| self.class.coerce(h) }
  @dirty_attributes = @dirty_attributes.merge(attrs).freeze # melt and freeze, huh
  @all_attributes = nil # it is not valid anymore

  self.class.validate_presence_of_proper_attributes(@base_attributes, @dirty_attributes)
end

Private Instance Methods

all_attributes() click to toggle source
# File lib/ormivore/entity.rb, line 181
def all_attributes
  # memory / performance tradeoff can be played with here by keeping
  # all_attributes around or generating it each time
  @all_attributes = @base_attributes.merge(@dirty_attributes)
end