Sha256: 23d449c9ea539c92486139196cee3cec074ae72e2d2407e8334286b64af30cf4

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Hari
  class Entity
    extend  ActiveModel::Naming
    extend  ActiveModel::Callbacks

    autoload :Property,      'hari/entity/property'
    autoload :Repository,    'hari/entity/repository'
    autoload :Serialization, 'hari/entity/serialization'

    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|
        write_attribute prop.name, attrs[prop.name] if attrs.key?(prop.name)
      end

      save if options.fetch(:save, true)
    end

    def update_attribute(attribute, value)
      update_attributes attribute => value
    end

    def ==(other)
      other.is_a?(Hari::Entity) && id == other.id
    end

    def new?
      id.nil?
    end

    alias :new_record? :new?

    def persisted?
      not new?
    end

    def destroyed?
      @destroyed
    end

    def generate_id
      '_e' + ::Time.now.strftime('%Y%m%d%H%M%S') + SecureRandom.hex(3)
    end

    def to_s
      attrs = attributes
      attrs.delete 'id'

      "<#{self.class} id='#{id}' attributes=#{attrs}>"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hari-0.0.5 lib/hari/entity.rb