Class: JetSet::Mapping

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

Overview

Represents JetSet Mapping.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Mapping

Initializes the mapping using Ruby block. Parameters:

+&block+:: should contain "entity" definitions see method +entity+.


11
12
13
14
# File 'lib/jet_set/mapping.rb', line 11

def initialize(&block)
  @entity_mappings = {}
  instance_eval(&block)
end

Instance Attribute Details

#entity_mappingsObject (readonly)

Returns the value of attribute entity_mappings.



6
7
8
# File 'lib/jet_set/mapping.rb', line 6

def entity_mappings
  @entity_mappings
end

Instance Method Details

#entity(type, &block) ⇒ Object

Defines an entity mapping Parameters:

+type+:: an entity class
+&block+:: should contain mapping definitions of the entity attributes. See +JetSet::EntityMapping+ class.

Returns an instance of EntityMapping



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jet_set/mapping.rb', line 21

def entity(type, &block)
  unless type.is_a? Class
    raise MapperError, 'Mapping definition of an entity should begin from a type declaration which should be a Class.'
  end

  name = type.name.underscore.to_sym
  if @entity_mappings.has_key?(name)
    raise MapperError, "Mapping definition for entity of type #{type} is already registered."
  end

  @entity_mappings[name] = EntityMapping.new(type, &block)
end

#get(name) ⇒ Object

Returns an entity mapping by its name. Parameters:

+name+:: string


37
38
39
40
41
42
43
# File 'lib/jet_set/mapping.rb', line 37

def get(name)
  unless @entity_mappings.has_key?(name)
    raise MapperError, "Entity \"#{name}\" is not defined in the mapping."
  end

  @entity_mappings[name]
end