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: ENV.fetch('LAZY_GRAPH_GRAPH_CACHE_MAX_ENTRIES', 1000).to_i, cache: {} },
  # Store up to 5000 configs
  context: { size: ENV.fetch('LAZY_GRAPH_CONTEXT_CACHE_MAX_ENTRIES', 5000).to_i, cache: {} },
  # Store up to 5000 queries
  query: { size: ENV.fetch('LAZY_GRAPH_QUERY_CACHE_MAX_ENTRIES', 5000).to_i, 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_copy, #rule_from_first_of, #rule_from_when, #rule_location, #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.



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

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



147
148
149
150
151
152
153
# File 'lib/lazy_graph/builder.rb', line 147

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_caches!Object



79
80
81
82
83
# File 'lib/lazy_graph/builder.rb', line 79

def self.clear_caches!
  clear_rules_modules!
  clear_helper_modules!
  BUILD_CACHE_CONFIG.each_value { |v| v[:cache].clear }
end

.clear_helper_modules!Object



75
76
77
# File 'lib/lazy_graph/builder.rb', line 75

def self.clear_helper_modules!
  @helper_modules = nil
end

.clear_rules_modules!Object



71
72
73
# File 'lib/lazy_graph/builder.rb', line 71

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)



51
52
53
54
55
56
57
58
59
# File 'lib/lazy_graph/builder.rb', line 51

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: true) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/lazy_graph/builder.rb', line 108

def self.eval!(modules:, context:, query:, debug: false, validate: true)
  graph = cache_as(:graph, [modules, debug, validate]) 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.build!(debug: debug, validate: validate)
  end

  context_result = cache_as(:context, [graph, context]) do
    build_context(graph, context)
  end

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

  cache_as(:query, [context_result, query]) do
    HashUtils.strip_missing(
      {
        type: :success,
        result: context_result.resolve(*(query || ''))
      }
    )
  end
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



67
68
69
# File 'lib/lazy_graph/builder.rb', line 67

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

.register_helper_modules(*mods) ⇒ Object



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

def self.register_helper_modules(*mods) = mods.each(&method(:register_helper_module))

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



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

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|
    @path = @path.nil? ? "#{name}" : "#{@path}+#{name}" unless @path.to_s.include?(name.to_s)
    send(module_body_func_name, **args, &inner_blk)
    self
  end
end

.rules_modulesObject



85
86
87
88
89
90
91
# File 'lib/lazy_graph/builder.rb', line 85

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

.usageObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lazy_graph/builder.rb', line 93

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: true) ⇒ Object



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

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

#context(value, debug: false, validate: true) ⇒ Object Also known as: feed



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

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

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



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

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

#to_strObject



155
156
157
# File 'lib/lazy_graph/builder.rb', line 155

def to_str
  "LazyGraph(modules=#{@path})"
end