Module: Arcade::Support::Model

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

Instance Method Summary collapse

Instance Method Details

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

used by array#allocate_model



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
# File 'lib/support/model.rb', line 20

def _allocate_model response=nil, auto = Config.autoload


  if response.is_a? Hash
    # save rid to a safe place
    temp_rid = response.delete :"@rid"
    type           = response.delete :"@type"
    cat            = response.delete :"@cat"

    return response if type.nil?
    temp_rid = "#0:0" if temp_rid.nil?
    type = "d" if type.nil?
    # extract type infos  and convert to database-name
    n, type_name = type.camelcase_and_namespace
    n = self.namespace if n.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
        when Array
          x.map do| y |
            if y.is_a?(Hash) && y.include?(:@type)   # if embedded documents are present, load them
              y.allocate_model(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
        when Hash
          if x.include?(:@type)
            x.allocate_model(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:  n).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 }
 elsif response.rid?
   # Autoload rid's
   response.load_rid
 else
   response
  end
end

#resolve_edge_name(*edge_names) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/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