Class: LazyGraph::Builder

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/lazy_graph/builder.rb,
lib/lazy_graph/builder/dsl.rb

Defined Under Namespace

Modules: DSL

Constant Summary collapse

BUILD_CACHE_CONFIG =

Cache up to a fixed number of graphs, context and queries

{
  # Store up to 1000 graphs
  graph: {size: 1000, cache: {}},
  # Store up to 5000 configs
  context: {size: 5000, cache: {}},
  # Store up to 5000 queries
  query: {size: 5000, cache: {}}
}.compare_by_identity.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#additional_properties, #any_of, #array, #date, #decimal, #default, #dependencies, #depends_on, #items, #matches, #object, #object_conditional, #one_of, #primitive, #required, #rule_from_first_of, #rule_from_when, #set_pattern_property, #set_property, #time, #timestamp, #yields

Constructor Details

#initialize(schema: { type: 'object', properties: {} }) ⇒ Builder

Returns a new instance of Builder.



25
# File 'lib/lazy_graph/builder.rb', line 25

def initialize(schema: { type: 'object', properties: {} }) = @schema = schema

Class Attribute Details

.helper_modulesObject (readonly)

Returns the value of attribute helper_modules.



52
53
54
# File 'lib/lazy_graph/builder.rb', line 52

def helper_modules
  @helper_modules
end

Instance Attribute Details

#schemaObject

This class is responsible for piece-wise building of rules, as a combined schema definition.



23
24
25
# File 'lib/lazy_graph/builder.rb', line 23

def schema
  @schema
end

Class Method Details

.cache_as(type, key) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/lazy_graph/builder.rb', line 127

def self.cache_as(type, key)
  cache, max_size = BUILD_CACHE_CONFIG[type].values_at(:cache, :size)
  key = key.hash
  cache[key] = cache[key] ? cache.delete(key) : yield
ensure
  cache.delete(cache.keys.first) while cache.size > max_size
end

.clear_helper_modules!Object



63
64
65
# File 'lib/lazy_graph/builder.rb', line 63

def self.clear_helper_modules!
  @helper_modules = nil
end

.clear_rules_modules!Object



59
60
61
# File 'lib/lazy_graph/builder.rb', line 59

def self.clear_rules_modules!
  @rules_modules = nil
end

.entity(name, &blk) ⇒ Object

Helper for defining a new entity in the schema (just a shorthand for defining a new method for now)



41
42
43
44
45
46
47
48
49
# File 'lib/lazy_graph/builder.rb', line 41

def self.entity(name, &blk)
  module_body_func_name = :"_#{name}"
  define_method(module_body_func_name, &blk)
  define_method(name) do |**args, &inner_blk|
    send(module_body_func_name, **args)
    inner_blk&.call
    self
  end
end

.eval!(modules:, context:, query:, debug: false, validate: false) ⇒ Object



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
123
124
125
# File 'lib/lazy_graph/builder.rb', line 90

def self.eval!(modules:, context:, query:, debug: false, validate: false)
  builder = cache_as(:graph, [modules, debug, validate].hash) do
    invalid_modules = modules.reject { |k, _v| rules_modules[:properties].key?(k.to_sym) }
    return format_error_response('Invalid Modules', invalid_modules.keys.join(',')) unless invalid_modules.empty?

    error = validate_modules(modules)

    return format_error_response('Invalid Module Option', error) unless error.empty?

    builder = build_modules(modules)
    return builder if builder.is_a?(Hash)

    builder
  end

  context_result = cache_as(:context, [builder, context]) do
    evaluate_context(builder, context, debug: debug, validate: validate)
  end

  return context_result if context_result.is_a?(Hash) && context_result[:type] == :error

  query_result = cache_as(:query, [context_result, query]){ context_result.query(*(query || '')) }

  {
    type: :success,
    result: query_result
  }
rescue SystemStackError => e
  LazyGraph.logger.error(e.message)
  LazyGraph.logger.error(e.backtrace.join("\n"))
  {
    type: :error,
    message: 'Recursive Query Detected',
    detail: "Problem query path: #{query}"
  }
end

.register_helper_module(mod) ⇒ Object



55
56
57
# File 'lib/lazy_graph/builder.rb', line 55

def self.register_helper_module(mod)
  (@helper_modules ||= []) << mod
end

.rules_module(name, schema = { type: 'object', properties: {} }, &blk) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/lazy_graph/builder.rb', line 30

def self.rules_module(name, schema = { type: 'object', properties: {} }, &blk)
  rules_modules[:properties][name.to_sym] = { type: :object, properties: schema }
  module_body_func_name = :"_#{name}"
  define_method(module_body_func_name, &blk)
  define_method(name) do |**args, &inner_blk|
    send(module_body_func_name, **args, &inner_blk)
    self
  end
end

.rules_modulesObject



67
68
69
70
71
72
73
# File 'lib/lazy_graph/builder.rb', line 67

def self.rules_modules
  @rules_modules ||= {
    type: :object,
    properties: {},
    additionalProperties: false
  }
end

.usageObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lazy_graph/builder.rb', line 75

def self.usage
  {
    modules_options: rules_modules[:properties].map do |k, v|
      {
        name: k.to_s,
        properties: v[:properties],
        required: v[:required]
      }
    end,
    context_sample_schema: rules_modules[:properties].keys.reduce(new) do |acc, (k, _v)|
      acc.send(k, **{})
    end.schema
  }
end

Instance Method Details

#build!(debug: false, validate: false) ⇒ Object



26
# File 'lib/lazy_graph/builder.rb', line 26

def build!(debug: false, validate: false) = @schema.to_lazy_graph(debug: debug, validate: validate, helpers: self.class.helper_modules)

#context(value, debug: false, validate: false) ⇒ Object



27
# File 'lib/lazy_graph/builder.rb', line 27

def context(value, debug: false, validate: false) = build!(debug: debug, validate: validate).context(value)

#eval!(context, *value, debug: false, validate: false) ⇒ Object



28
# File 'lib/lazy_graph/builder.rb', line 28

def eval!(context, *value, debug: false, validate: false) = context(context, validate: validate, debug: debug).query(*value)