Module: Entitainer

Defined in:
lib/entitainer.rb

Overview

Build database entities. Define attributes and relation in a schema block

schema do
  attributes :attr1, :attr2, :attr3
  belongs_to :parent1, :parent2
  has_many :items1, :items2
end

An id attribute is always added automatically to the attribute list.

Also for belongs_to relation an attribute with the same name as the relation and _id suffix is added to the attribute list.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



18
19
20
# File 'lib/entitainer.rb', line 18

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#defined_attributesObject

List of attributes with an assigned value.



128
129
130
131
132
# File 'lib/entitainer.rb', line 128

def defined_attributes
  self.class.available_attributes.select do |attr|
    instance_variable_get("@#{attr}").some?
  end
end

#defined_attributes_with_valuesObject

A hash where keys are attribute symbols and values are their values. Only defined attributes are included.



136
137
138
139
140
141
142
143
# File 'lib/entitainer.rb', line 136

def defined_attributes_with_values
  {}.tap do |h|
    self.class.available_attributes.each do |attr|
      option = instance_variable_get("@#{attr}")
      h[attr.to_sym] = option.value unless option.none?
    end
  end
end