Module: Lotus::Entity::ClassMethods

Defined in:
lib/lotus/entity.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#allowed_attribute_name?(name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if attr_reader define the given attribute

Returns:

  • (Boolean)

Since:

  • 0.5.1



167
168
169
# File 'lib/lotus/entity.rb', line 167

def allowed_attribute_name?(name)
  !instance_methods.include?(name)
end

#attributes(*attrs) ⇒ 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 'lotus/model'

class User
  include Lotus::Entity
  attributes :name, :age
end
User.attributes => #<Set: {:id, :name, :age}>

Given params is array of attributes

require 'lotus/model'

class User
  include Lotus::Entity
  attributes [:name, :age]
end
User.attributes => #<Set: {:id, :name, :age}>

Extend entity

require 'lotus/model'

class User
  include Lotus::Entity
  attributes :name
end

class DeletedUser < User
  include Lotus::Entity
  attributes :deleted_at
end

User.attributes => #<Set: {:id, :name}>
DeletedUser.attributes => #<Set: {:id, :name, :deleted_at}>

Parameters:

  • attrs (Array<Symbol>)

    a set of arbitrary attribute names

See Also:

Since:

  • 0.2.0



142
143
144
145
146
147
148
149
150
151
# File 'lib/lotus/entity.rb', line 142

def attributes(*attrs)
  return @attributes ||= Set.new unless attrs.any?

  Lotus::Utils::Kernel.Array(attrs).each do |attr|
    if allowed_attribute_name?(attr)
      define_attr_accessor(attr)
      self.attributes << attr
    end
  end
end

#define_attr_accessor(attr) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define setter/getter methods for attributes.

Parameters:

  • attr (Symbol)

    an attribute name

Since:

  • 0.3.1



159
160
161
# File 'lib/lotus/entity.rb', line 159

def define_attr_accessor(attr)
  attr_accessor(attr)
end