Class: JetSet::EntityMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/jet_set/entity_mapping.rb

Overview

Entity mapping is an element of JetSet mapping definition, see JetSet::Mapping. Should be instantiated by method entity of JetSet::Mapping instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, &block) ⇒ EntityMapping

Initializes the mapping using Ruby block. Parameters:

+type+:: an entity class
+&block+:: should contain attributes definitions see methods +field+, +collection+, +reference+.


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jet_set/entity_mapping.rb', line 14

def initialize(type, &block)
  @type = type
  @references = {}
  @collections = {}
  @dependencies = [] # stores only strong dependencies
  @relations = [] # stores all dependencies
  @fields = ['id']

  if block_given?
    instance_eval(&block)
  end
end

Instance Attribute Details

#collectionsObject (readonly)

Returns the value of attribute collections.



8
9
10
# File 'lib/jet_set/entity_mapping.rb', line 8

def collections
  @collections
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



8
9
10
# File 'lib/jet_set/entity_mapping.rb', line 8

def dependencies
  @dependencies
end

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/jet_set/entity_mapping.rb', line 8

def fields
  @fields
end

#referencesObject (readonly)

Returns the value of attribute references.



8
9
10
# File 'lib/jet_set/entity_mapping.rb', line 8

def references
  @references
end

#relationsObject (readonly)

Returns the value of attribute relations.



8
9
10
# File 'lib/jet_set/entity_mapping.rb', line 8

def relations
  @relations
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/jet_set/entity_mapping.rb', line 8

def type
  @type
end

Instance Method Details

#collection(name, params = {}) ⇒ Object

Defines an attribute-collection of a complex type - another entity defined in the mapping. Parameters:

+name+:: attribute name
+params+::
  +type+:: class of the entity
  +using+:: (optional) a name of many-to-many association table if needed.


57
58
59
60
61
62
63
# File 'lib/jet_set/entity_mapping.rb', line 57

def collection(name, params = {})
  unless params.has_key? :type
    raise MapperError, "Collection \"#{name}\" should have a type. Example:\n collection '#{name}', type: User\n"
  end

  @collections[name] = Collection.new(name, params[:type], params[:using])
end

#field(name) ⇒ Object

Defines an attribute of a simple type (String, Integer, etc) Parameters:

+name+:: attribute name


30
31
32
# File 'lib/jet_set/entity_mapping.rb', line 30

def field(name)
  @fields << name.to_s
end

#reference(name, params = {}) ⇒ Object

Defines an attribute of a complex type - another entity defined in the mapping. Parameters:

+name+:: attribute name
+params+::
  +type+:: class of the entity
  +weak+:: (optional) a flag for making a reference to an entity which is not directly
           associated for skipping persistence steps for it


41
42
43
44
45
46
47
48
49
# File 'lib/jet_set/entity_mapping.rb', line 41

def reference(name, params = {})
  unless params.has_key? :type
    raise MapperError, "Reference \"#{name}\" should have a type. Example:\n reference '#{name}', type: User\n"
  end

  @references[name] = Reference.new(name, params[:type], params[:weak])
  @dependencies << params[:type] unless params[:weak]
  @relations << params[:type]
end