Class: Arcade::Match

Inherits:
Object
  • Object
show all
Includes:
Support::Sql
Defined in:
lib/arcade/match.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Sql

#compose_where, #generate_sql_list

Constructor Details

#initialize(**args) ⇒ Match

This is a wrapper for the match statement

Initialize:  a= Arcade::Match.new type: Arcade::DatabaseType, where: { property: 'value' }, as: :alias
Complete:    b = a.out( Arcade::EdgeType ).node( while: true, as: item )[ .in.node ... ]
Inspect      b.to_s
Query DB     b.execute [.allocate_model]
                       [.analyse_result]
Customize the return values:
             b.to_s{ "customized return statement" }
             b.execute{ "customized return statment" }

Conditions on edges:
      m =  Match.new type: Arcade::DatabaseType
      m.inE via: Arcade::Edge  || via: [Arcade::Edge1, Arcade::Edge2], where: { var: 3..6 }  
       --->  inE('edge(1)'){ where: (var between 3 and 6 )}.outV('edge(2)')

Address a single database record:
      m = Match.new( vertex: { Arcade::Vertex-Instance } )
          .....
          .node( vertex: { Arcade::Vertex-Instance } )     # instead of where statement           

Example

 symbol_or_title =  symbol.present? ? { :symbol => symbol } : { :title => title }
 where = { :right => right }

 a= Arcade::Match.new( type: self.class, where: symbol_or_title )
                 .out( Arcade::HasContract )
                 .node( as: :c, where: where )
 a.execute do "c.last_trading_day as expiry,         # c is returned form the query [as: :c]                                               
               count(c) as contracts,
               min(c.strike) as s_min,
               max(c.strike) as s_max
               group by c.last_trading_day order by c.last_trading_day"
           end


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/arcade/match.rb', line 46

def initialize  **args
  type = args.delete :type
  vertex =  args.delete :vertex
  rid =  args.delete :rid
  raise "MATCH#new:: parameter rid is not supported. Use `vertex: Arcade::Vertex.instance` instead." if rid.present?
  @args = args
  @as = []

  @stack = if vertex.is_a?( Arcade::Vertex ) && args.empty?
     [ "MATCH { type: #{vertex.class.database_name }, rid: #{vertex.rid} }" ]
           elsif  vertex.is_a?( Arcade::Vertex ) 
     [ "MATCH { type: #{vertex.class.database_name }, rid: #{vertex.rid}, #{ assigned_parameters } }" ]
           elsif  type.is_a?( Class) && type.ancestors.include?(Arcade::Vertex) && args.empty?
     [ "MATCH { type: #{type.database_name} }" ]
           elsif  type.is_a?( Class) && type.ancestors.include?(Arcade::Vertex)
     [ "MATCH { type: #{type.database_name}, #{ assigned_parameters } }" ]
           else
     raise "Match:: Either type  (Arcade::Vertex-Class) or vertex (Arcade::Vertex-Object) is required as parameter"
           end

  return self
end

Class Method Details

.define_base_edge(*direction) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/arcade/match.rb', line 95

def self.define_base_edge *direction
  direction.each do | e_d |
    define_method e_d do | edge = "" |
      raise "edge must be a Database-class" unless edge.is_a?(Class) || edge.empty?
      @stack << ".#{ e_d  }(#{edge.is_a?(Class) ? edge.database_name.to_or : ''})"
      self
    end
  end
end

.define_inner_edge(start_dir, final_dir) ⇒ Object

add conditions on edges to the match statement



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/arcade/match.rb', line 107

def self.define_inner_edge start_dir, final_dir
  define_method start_dir do | edge = "", **a |
    raise "edge must be a Database-class"  unless edge.is_a?(Class) || edge.empty?

    print_edges = -> (e){ e.is_a?(Class) ? e.database_name.to_or : ''}
    edges = [ a.delete(:via) ].flatten
    edges << edge unless edge == ''
    n = if a.empty?
          ""
        else
          @args = a
          "{ #{ assigned_parameters } }"
        end
    @stack << ".#{ start_dir }(#{print_edges.call(edges.first)})#{ n }.#{ final_dir }(#{print_edges.call(edges.last)})"
    return self
  end

end

Instance Method Details

#execute(&b) ⇒ Object

Execute the @stack generally followed by ‘select_result` to convert json-hashes to arcade objects

The optional block modifies the result-statement i.e TG::TimeGraph.grid( 2023, 2..9 ).out(HasPosition)

.node( as: :contract  )
.execute { "contract.symbol"  }
.select_result

gets all associated contracts connected to the month-grid



89
90
91
# File 'lib/arcade/match.rb', line 89

def execute &b
  Arcade::Init.db.query( to_s( &b ) )
end

#node(**args) ⇒ Object

general declation of a node (ie. vertex)



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/arcade/match.rb', line 133

def node **args
  vertex =  args.delete :vertex
  rid =  args.delete :rid
  raise "MATCH#node:: parameter rid is not supported. Use `vertex: Arcade::Vertex.instance` instead." if rid.present?
  @args = args
  @stack << if args.empty?
   vertex.present? ? "{ type: #{vertex.class.database_name }, rid: #{vertex.rid} }" : "{}"
            else
   vertex.present? ? "{ type: #{vertex.class.database_name }, rid: #{vertex.rid}, #{assigned_parameters} }" :  "{ #{ assigned_parameters } }"
            end
  return self
end

#to_s(&b) ⇒ Object

Inspect the generated match statement



70
71
72
73
74
75
76
# File 'lib/arcade/match.rb', line 70

def to_s &b
  r = ""
  r = "DISTINCT " if @distinct
  r << @as.join(",")
  r= yield(r) if block_given?
  @stack.join("") + " RETURN #{r} "
end