7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/unimatrix/blueprintable.rb', line 7
def self.included( klass )
@module_members << klass
class << klass
cattr_accessor :blueprints
def find_blueprint( class_type_name )
self.blueprints.detect do | blueprint |
blueprint.resource_type_name == class_type_name
end
end
def build( attributes = {}, associations = {} )
attributes = attributes.transform_keys( &:to_s )
blueprint = find_blueprint( attributes[ 'type_name' ] ) rescue nil
klass = build_typed_class( attributes, blueprint )
klass.new( attributes, associations ) rescue nil
end
def build_typed_class( attributes, blueprint )
klass = self
if !blueprint.nil?
module_name = Unimatrix.const_get( self.name.split( '::' )[1].underscore.camelize )
entity_class_name = attributes[ 'type_name' ].camelize + attributes[ 'realm_uuid' ]
unless module_name.const_defined?( entity_class_name )
klass = module_name.const_set( entity_class_name, Class.new( self ) )
default_attributes = [ "type_name", "relationships", "realm_uuid" ]
if blueprint.respond_to?( 'blueprint_attributes' )
permitted_attributes = blueprint.blueprint_attributes.map( &:name ) + default_attributes
klass.instance_eval do
permitted_attributes.each do | field_name |
field field_name.to_sym
end
end
end
else
klass = module_name.const_get( entity_class_name )
end
end
klass
end
end
end
|