lib/hari/entity.rb in hari-0.0.4 vs lib/hari/entity.rb in hari-0.0.5
- old
+ new
@@ -1,50 +1,40 @@
-require 'hari/entity/property'
-require 'hari/entity/repository'
-require 'hari/entity/serialization'
-
module Hari
class Entity
extend ActiveModel::Naming
extend ActiveModel::Callbacks
- include ActiveModel::Validations
autoload :Property, 'hari/entity/property'
- autoload :Repository, 'hari/entity/property'
- autoload :Serialization, 'hari/entity/property'
+ autoload :Repository, 'hari/entity/repository'
+ autoload :Serialization, 'hari/entity/serialization'
- extend Property::Builder
include Repository
include Serialization
define_model_callbacks :create, :update, :destroy, :save
property :id
property :created_at, type: Time
property :updated_at, type: Time
def initialize(attrs = {})
+ update_attributes attrs, save: false
+ end
+
+ def update_attributes(attrs = {}, options = {})
return if attrs.blank?
attrs = attrs.with_indifferent_access
self.class.properties.each do |prop|
- send("#{prop.name}=", attrs[prop.name]) if attrs[prop.name]
+ write_attribute prop.name, attrs[prop.name] if attrs.key?(prop.name)
end
- end
- def attributes
- self.class.properties.inject({}) do |buffer, prop|
- buffer.merge prop.name => send(prop.name)
- end
+ save if options.fetch(:save, true)
end
- alias :attribute :send
- alias :read_attribute :send
- alias :has_attribute? :respond_to?
-
- def write_attribute(name, value)
- send "#{name}=", value
+ def update_attribute(attribute, value)
+ update_attributes attribute => value
end
def ==(other)
other.is_a?(Hari::Entity) && id == other.id
end