Class: Unimatrix::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/unimatrix/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(content = {}) {|_self| ... } ⇒ Parser

Returns a new instance of Parser.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
# File 'lib/unimatrix/parser.rb', line 5

def initialize( content = {} )
  @content = content
  yield self if block_given?
end

Instance Method Details

#associationsObject



32
33
34
35
36
# File 'lib/unimatrix/parser.rb', line 32

def associations
  @content.include?( '$associations' ) ?
    @content[ '$associations' ] :
    nil
end

#find_resource_class_by_type_name(attribute_type_name, option_type_name) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/unimatrix/parser.rb', line 124

def find_resource_class_by_type_name( attribute_type_name, option_type_name )
  resource_class = Resource.find_by_type_name( attribute_type_name )
  unless resource_class.present?
    resource_class = Resource.find_by_type_name( option_type_name )
  end
  resource_class
end

#keyObject



22
23
24
# File 'lib/unimatrix/parser.rb', line 22

def key
  'id'
end

#keysObject



26
27
28
29
30
# File 'lib/unimatrix/parser.rb', line 26

def keys
  @content.include?( '$this' ) ?
    @content[ '$this' ][ 'ids' ] :
    nil
end

#nameObject



10
11
12
13
14
# File 'lib/unimatrix/parser.rb', line 10

def name
  @content.include?( '$this' ) ?
    @content[ '$this' ][ 'name' ] :
    nil
end

#parse_resource(name, attributes) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/unimatrix/parser.rb', line 49

def parse_resource( name, attributes )

  @resources_mutex ||= Hash.new { | hash, key | hash[ key ] = [] }
  resource_key = attributes[ key ]

  # Lock the resource index for this name/key combination
  # This prevents objects that are associated with
  # themselves from causing a stack overflow
  return nil if @resources_mutex[ name ].include?( resource_key )
  @resources_mutex[ name ].push( resource_key )

  resource = nil

  if attributes.present?

    resource_type_name = attributes[ :type_name ] || self.type_name
    resource_class =
      Resource.find_by_type_name( resource_type_name ) ||
      Resource.find_by_type_name( self.type_name )

    if resource_class.present?
      relations = name == self.name ?
        self.parse_associations( attributes ) : []
      resource = resource_class.build( attributes, relations )
    end

  end

  @resources_mutex[ name ].delete( object_key )

  resource

end

#resource_associations_by(name, key) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/unimatrix/parser.rb', line 132

def resource_associations_by( name, key )
  result = Hash.new { | hash, key | hash[ key ] = [] }
  associations = self.associations
  if associations && associations.include?( name )
    association = associations[ name ].detect do | association |
      association[ 'id' ] == key
    end
    if association.present?
      association.each do | key, value |
        unless key == 'id'
          type_name = value[ 'type_name' ]
          result[ key ] = ( value[ 'ids' ] || [] ).map do | associated_id |
            self.resource_by(
              key,
              associated_id,
              { 'type_name' => type_name }
            )
          end
          result[ key ].compact!
        end
      end
    end
  end
  result
end

#resource_attribute_indexObject



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/unimatrix/parser.rb', line 158

def resource_attribute_index
  @resource_attribute_index ||= begin
    index = Hash.new { | hash, key | hash[ key ] = {} }
    @content.each do | key, resources_attributes |
      unless key[0] == '$'
        resources_attributes.each do | resource_attributes |
          index[ key ][ resource_attributes[ 'id' ] ] = resource_attributes
        end
      end
    end
    index
  end
end

#resource_by(name, key, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/unimatrix/parser.rb', line 83

def resource_by( name, key, options = {} )

  @resources_index ||= Hash.new { | hash, key | hash[ key ] = {} }
  @resource_index_mutex ||= Hash.new { | hash, key | hash[ key ] = [] }

  @resources_index[ name ][ key ] ||= begin

    # lock the resource index for this name/key combination
    # note: this prevents Unimatrix objects that are associated with
    #       themselves from causing a stack overflow
    return nil if @resource_index_mutex[ name ].include?( key )
    @resource_index_mutex[ name ].push( key )

    result = nil
    resource_attributes = resource_attribute_index[ name ][ key ]
    
      if resource_attributes.present?
        
        parse_nested_attributes( resource_attributes )
        
        resource_class = find_resource_class_by_type_name(
          resource_attributes[ 'type_name' ],
          options[ 'type_name' ]
        )
        if resource_class.present?
          result = resource_class.build(
            resource_attributes,
            self.resource_associations_by( name, key )
          )
        end
      end

    # unlock the resource index for this name/key combination
    @resource_index_mutex[ name ].delete( key )

    result

  end

end

#resourcesObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/unimatrix/parser.rb', line 38

def resources
  result = nil

  unless self.name.blank?
    result = self.keys.map do | key |
      self.resource_by( name, key, { 'type_name' => self.type_name } )
    end
  end
  result
end

#type_nameObject



16
17
18
19
20
# File 'lib/unimatrix/parser.rb', line 16

def type_name
  @content.include?( '$this' ) ?
    @content[ '$this' ][ 'type_name' ] :
    nil
end