module Epiphy::Entity::ClassMethods

Public Instance Methods

attributes() click to toggle source
# File lib/epiphy/entity.rb, line 119
def attributes
  @attributes
end
attributes=(*attributes) click to toggle source

(Re)defines getters, setters and initialization for the given attributes.

These attributes can match the database columns, but this isn’t a requirement. The mapper used by the relative repository will translate these names automatically.

An entity can work with attributes not configured in the mapper, but of course they will be ignored when the entity will be persisted.

Please notice that the required ‘id` attribute is automatically defined and can be omitted in the arguments.

@param attributes [Array<Symbol>] a set of arbitrary attribute names

@since 0.1.0

@see Epiphy::Repository @see Epiphy::Model::Mapper

@example

require 'epiphy/model'

class User
  include Epiphy::Entity
  self.attributes = :name
end
# File lib/epiphy/entity.rb, line 105
def attributes=(*attributes)
  @attributes = Lotus::Utils::Kernel.Array(attributes.unshift(:id))

  class_eval %{
    def initialize(attributes = {})
      attributes.keys.each do |key|
        attributes[(key.to_sym rescue key) || key] = attributes.delete(key)
      end
      #{ @attributes.map {|a| "@#{a}" }.join(', ') }, = *attributes.values_at(#{ @attributes.map {|a| ":#{a}"}.join(', ') })
    end
  }
  attr_accessor *@attributes
end