Class: RGen::Instantiator::AbstractInstantiator

Inherits:
Object
  • Object
show all
Defined in:
lib/rgen/instantiator/abstract_instantiator.rb

Direct Known Subclasses

NodebasedXMLInstantiator

Defined Under Namespace

Classes: ResolverDescription

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ AbstractInstantiator

Returns a new instance of AbstractInstantiator.



13
14
15
# File 'lib/rgen/instantiator/abstract_instantiator.rb', line 13

def initialize(env)
	@env = env
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object (private)

:nodoc:



43
44
45
46
47
48
49
# File 'lib/rgen/instantiator/abstract_instantiator.rb', line 43

def method_missing(m, *args) #:nodoc:
	if @current_object
		@current_object.send(m)
	else
		super
	end
end

Class Attribute Details

.resolver_descsObject

Returns the value of attribute resolver_descs.



10
11
12
# File 'lib/rgen/instantiator/abstract_instantiator.rb', line 10

def resolver_descs
  @resolver_descs
end

Class Method Details

.resolve(attribute, desc = nil, &block) ⇒ Object

Specifies that attribute should be resolved. If :class is specified, resolve attribute only for objects of type class. The block must return the value to which the attribute should be assigned. The object for which the attribute is to be resolved will be accessible in the current context within the block.



23
24
25
26
27
# File 'lib/rgen/instantiator/abstract_instantiator.rb', line 23

def self.resolve(attribute, desc=nil, &block)
	from = (desc.is_a?(Hash) && desc[:class])
	self.resolver_descs ||= []
	self.resolver_descs << ResolverDescription.new(from, attribute, block)
end

.resolve_by_id(attribute, desc) ⇒ Object

Resolves attribute to a model element which has attribute :id set to the value currently in attribute :src

Raises:

  • (StandardError)


32
33
34
35
36
37
38
39
# File 'lib/rgen/instantiator/abstract_instantiator.rb', line 32

def self.resolve_by_id(attribute, desc)
	id_attr = (desc.is_a?(Hash) && desc[:id])
	src_attr = (desc.is_a?(Hash) && desc[:src])
	raise StandardError.new("No id attribute given.") unless id_attr
	resolve(attribute) do
		@env.find(id_attr => @current_object.send(src_attr)).first
	end
end