Module: Arcade::Support::Model

Included in:
Database, Query, Vertex, Vertex, Array, Hash
Defined in:
lib/arcade/support/model.rb

Instance Method Summary collapse

Instance Method Details

#_allocate_model(response = nil, auto = Config.autoload) ⇒ Object

used by array#allocate_model



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/arcade/support/model.rb', line 19

def _allocate_model response=nil, auto = Config.autoload

  if response.is_a? Hash
    # save rid to a safe place
    temp_rid = response.delete( :"@rid"  ) || response.delete( :rid ) || "#0:0"
    type     = response.delete( :"@type" ) || response.delete( :type )  || nil
    cat      = response.delete( :"@cat"  ) || response.delete( :cat )  || "d"

    return response if type.nil?
    # extract type infos  and convert to database-name
    namespace, type_name = type.camelcase_and_namespace
    namespace = self.namespace if namespace.nil?
    # autoconvert rid's in attributes to model-records  (exclude edges!)
    if auto && !(cat.to_s =='e')
      response.transform_values do  |x|
        case x
        when String
          x.rid? ?  x.load_rid : x                   # follow links
        when Array
          a =[]
          x.map do | y |
            # Thread.new do  ## using thread or fiber decreases the performance significantly.
              if y.is_a?(Hash) && y.include?(:@type)   # if embedded documents are present, load them
                _allocate_model(y,false)
              elsif y.rid?                             # if links are present, load the object
                y.load_rid(false) # do not autoload further records, prevents from recursive locking
              else
                y
              end
           # end
          end
        when Hash
          if x.include?(:@type)
            #x.allocate_model(false)
            _allocate_model(x,false)
          else
            x.transform_values!{|z|  z.rid? ?  z.load_rid(false) : z }
          end
        else
          x
        end
      end
    end
    # choose the appropriate class
    klass=  Dry::Core::ClassBuilder.new(  name: type_name, parent: nil, namespace: namespace ).call
    #
  begin
    # create a new object of that  class with the appropriate attributes
    new = klass.new  **response.merge( rid: temp_rid  )
  rescue ::ArgumentError => e
    raise  "Allocation of class #{klass.to_s} failed"
  end
    # map additional attributes to `input`
    v =  response.except  *new.attributes.keys
    v.empty? ?  new  :  new.new( values: v.except( :"@in", :"@out" ) )
 elsif response.is_a? Array
   puts "Allocate_model..detected array"
   ## recursive behavior, just in case
   response.map{ | y | _allocate_model y, auto }
 elsif response.rid?
   # Autoload rid's
   response.load_rid
 #  auto ? response.load_rid : response
 else
   response
  end
end

#resolve_edge_name(*edge_names) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/arcade/support/model.rb', line 5

def resolve_edge_name *edge_names
  edge_names.map do | edge_name |
    case  edge_name
    when nil
      ""
    when Class
      "'" + edge_name.database_name + "'"
    when String
      "'" + edge_name + "'"
    end
  end.join(',')
end