Module: Epiphy::Entity::ClassMethods

Defined in:
lib/epiphy/entity.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#attributesObject

Since:

  • 0.1.0



117
118
119
# File 'lib/epiphy/entity.rb', line 117

def attributes
  @attributes
end

#attributes=(*attributes) ⇒ Object

(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.

Examples:

require 'epiphy/model'

class User
  include Epiphy::Entity
  self.attributes = :name
end

Parameters:

  • attributes (Array<Symbol>)

    a set of arbitrary attribute names

See Also:

Since:

  • 0.1.0



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/epiphy/entity.rb', line 105

def attributes=(*attributes)
  @attributes = Lotus::Utils::Kernel.Array(attributes.unshift(:id))

  class_eval %{
    def initialize(attributes = {})
  #{ @attributes.map {|a| "@#{a}" }.join(', ') }, = *attributes.values_at(#{ @attributes.map {|a| ":#{a}"}.join(', ') })
    end
  }

  attr_accessor *@attributes
end