Module: Grumlin::Repository

Included in:
Benchmark::Repository
Defined in:
lib/grumlin/repository.rb

Defined Under Namespace

Modules: InstanceMethods Classes: ErrorHandlingStrategy

Constant Summary collapse

RETURN_MODES =
{
  list: :toList,
  none: :iterate,
  single: :next,
  traversal: :nil
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#middlewares {|@middlewares| ... } ⇒ Object

Yields:



39
40
41
42
43
44
45
46
# File 'lib/grumlin/repository.rb', line 39

def middlewares
  @middlewares ||= Grumlin::Middlewares::Builder.new do |b|
    b.use(Grumlin.default_middlewares)
  end

  yield(@middlewares) if block_given?
  @middlewares
end

Class Method Details

.extended(base) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/grumlin/repository.rb', line 11

def self.extended(base)
  super
  base.extend(Grumlin::Shortcuts)
  base.include(Grumlin::Repository::InstanceMethods)

  base.shortcuts_from(Grumlin::Shortcuts::Properties)
  base.shortcuts_from(Grumlin::Shortcuts::Upserts)
end

.newObject



20
21
22
23
24
# File 'lib/grumlin/repository.rb', line 20

def self.new
  @repository ||= Class.new do # rubocop:disable Naming/MemoizedInstanceVariableName
    extend Grumlin::Repository
  end.new
end

Instance Method Details

#default_edge_properties(&block) ⇒ Object



82
83
84
85
86
# File 'lib/grumlin/repository.rb', line 82

def default_edge_properties(&block)
  shortcut :addE, override: true do |*args|
    super(*args).props(**block.call(*args)) # rubocop:disable Performance/RedundantBlockCall
  end
end

#default_vertex_properties(&block) ⇒ Object



76
77
78
79
80
# File 'lib/grumlin/repository.rb', line 76

def default_vertex_properties(&block)
  shortcut :addV, override: true do |*args|
    super(*args).props(**block.call(*args)) # rubocop:disable Performance/RedundantBlockCall
  end
end

#inherited(subclass) ⇒ Object



26
27
28
29
30
31
# File 'lib/grumlin/repository.rb', line 26

def inherited(subclass)
  super
  subclass.middlewares = Grumlin::Middlewares::Builder.new do |b|
    b.use(middlewares)
  end
end

#query(name, return_mode: :list, postprocess_with: nil, &query_block) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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
# File 'lib/grumlin/repository.rb', line 48

def query(name, return_mode: :list, postprocess_with: nil, &query_block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return_mode = validate_return_mode!(return_mode)
  postprocess_with = validate_postprocess_with!(postprocess_with)

  define_method name do |*args, query_params: {}, **params, &block|
    t = instance_exec(*args, **params, &query_block)
    return t if t.nil? || (t.respond_to?(:empty?) && t.empty?)

    unless t.is_a?(Grumlin::Step)
      raise Grumlin::WrongQueryResult,
            "queries must return #{Grumlin::Step}, nil or an empty collection. Given: #{t.class}"
    end

    return block.call(t) unless block.nil?

    return t.profile.next if query_params[:profile] == true

    return_mode = self.class.validate_return_mode!(query_params[:return_mode] || return_mode)

    return t if return_mode == :traversal

    t.public_send(RETURN_MODES[return_mode]).tap do |result|
      return postprocess_with.call(result) if postprocess_with.respond_to?(:call)
      return send(postprocess_with, result) unless postprocess_with.nil?
    end
  end
end

#read_only!Object



33
34
35
36
37
# File 'lib/grumlin/repository.rb', line 33

def read_only!
  middlewares do |b|
    b.insert_after Grumlin::Middlewares::ApplyShortcuts, Grumlin::Middlewares::FindMutatingSteps
  end
end

#validate_postprocess_with!(postprocess_with) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
# File 'lib/grumlin/repository.rb', line 94

def validate_postprocess_with!(postprocess_with)
  if postprocess_with.nil? || postprocess_with.is_a?(Symbol) ||
     postprocess_with.is_a?(String) || postprocess_with.respond_to?(:call)
    return postprocess_with
  end

  raise ArgumentError,
        "postprocess_with must be a String, Symbol or a callable object, given: #{postprocess_with.class}"
end

#validate_return_mode!(return_mode) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
# File 'lib/grumlin/repository.rb', line 88

def validate_return_mode!(return_mode)
  return return_mode if RETURN_MODES.include?(return_mode)

  raise ArgumentError, "unsupported return mode #{return_mode}. Supported modes: #{RETURN_MODES.keys}"
end