Module: LazyGraph::Builder::DSL
- Included in:
- LazyGraph::Builder
- Defined in:
- lib/lazy_graph/builder/dsl.rb
Overview
This module defines the DSL for building Lazy Graph JSON schemas. Supported helpers
-
object :name, **opts, &blk
-
object_conditional :name, **opts, &blk ⌙ matches &blk
-
array :name, **opts, &blk ⌙ items &blk
-
<primitive> :name, **opts, &blk
-
date :name, **opts, &blk
-
decimal :name, **opts, &blk
-
timestamp :name, **opts, &blk
-
time :name, **opts, &blk
Instance Method Summary collapse
- #additional_properties(value) ⇒ Object
- #any_of(any_of) ⇒ Object
- #array(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, type: :object, extend: nil, **opts, &block) ⇒ Object
- #date(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) ⇒ Object
- #decimal(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) ⇒ Object
- #default(value) ⇒ Object
- #dependencies(dependencies) ⇒ Object
- #depends_on(*dependencies) ⇒ Object
- #items(&blk) ⇒ Object
- #matches(name, invisible: true, **when_clause, &blk) ⇒ Object
- #object(name = nil, required: false, pattern_property: false, rule: nil, copy: nil, default: nil, description: nil, extend: nil, **opts, &blk) ⇒ Object
- #object_conditional(name = nil, required: false, pattern_property: false, rule: nil, copy: nil, default: nil, description: nil, extend: nil, conditions: nil, **opts, &blk) ⇒ Object
- #one_of(one_of) ⇒ Object
- #primitive(name = nil, type, required: false, pattern_property: false, default: nil, description: nil, enum: nil, rule: nil, copy: nil, **additional_options, &blk) ⇒ Object
- #required(*keys) ⇒ Object
- #rule_from_copy(copy) ⇒ Object
- #rule_from_first_of(prop_list, conditions = nil) ⇒ Object
- #rule_from_when(when_clause) ⇒ Object
- #rule_location ⇒ Object
- #set_pattern_property(pattern, value) ⇒ Object
- #set_property(key, value) ⇒ Object
- #time(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) ⇒ Object
- #timestamp(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) ⇒ Object
- #yields(other) ⇒ Object
Instance Method Details
permalink #additional_properties(value) ⇒ Object
[View source]
19 20 21 22 |
# File 'lib/lazy_graph/builder/dsl.rb', line 19 def additional_properties(value) schema[:additionalProperties] = value self end |
permalink #any_of(any_of) ⇒ Object
[View source]
279 280 281 |
# File 'lib/lazy_graph/builder/dsl.rb', line 279 def any_of(any_of) schema[:anyOf] = (schema[:any_of] || []).concat(any_of).uniq end |
permalink #array(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, type: :object, extend: nil, **opts, &block) ⇒ Object
[View source]
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/lazy_graph/builder/dsl.rb', line 283 def array(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, type: :object, extend: nil, **opts, &block) rule ||= rule_from_copy(copy) new_array = { type: :array, **(!default.nil? ? { default: default } : {}), **(description ? { description: description } : {}), **(rule ? { rule: rule, rule_location: rule_location } : {}), **opts, items: { properties: {} }.tap do |items| yields(items) do send(type, :items, extend: extend, &block) end end[:properties][:items] } required(name) if required && default.nil? && rule.nil? pattern_property ? set_pattern_property(name, new_array) : set_property(name, new_array) end |
permalink #date(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) ⇒ Object
[View source]
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/lazy_graph/builder/dsl.rb', line 235 def date(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) rule ||= rule_from_copy(copy) new_date = { anyOf: [ { type: :string, # Matches ISO 8601 date format pattern: '^\\d{4}-\\d{2}-\\d{2}$' } ], type: :date, # Custom extended type **(!default.nil? ? { default: default } : {}), **(description ? { description: description } : {}), **(rule ? { rule: rule, rule_location: rule_location } : {}), validate_presence: required, **opts } yields(new_date, &blk) required(name) if required && default.nil? && rule.nil? pattern_property ? set_pattern_property(name, new_date) : set_property(name, new_date) end |
permalink #decimal(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) ⇒ Object
[View source]
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/lazy_graph/builder/dsl.rb', line 156 def decimal(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) rule ||= rule_from_copy(copy) # Define the decimal schema supporting multiple formats new_decimal = { anyOf: [ { type: :string, # Matches valid decimals with optional exponentials pattern: '^-?(\\d+\\.\\d+|\\d+|\\d+e[+-]?\\d+|\\d+\\.\\d+e[+-]?\\d+)$' }, # Allows both float and int { type: :number # }, { type: :integer } ], type: :decimal, **(!default.nil? ? { default: default } : {}), **(description ? { description: description } : {}), **(rule ? { rule: rule, rule_location: rule_location } : {}), validate_presence: required, **opts } yields(new_decimal, &blk) required(name) if required && default.nil? && rule.nil? pattern_property ? set_pattern_property(name, new_decimal) : set_property(name, new_decimal) end |
permalink #default(value) ⇒ Object
[View source]
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lazy_graph/builder/dsl.rb', line 29 def default(value) schema[:default] = \ if value.is_a?(Hash) HashUtils.deep_merge(schema.fetch(:default, {}), value) elsif value.is_a?(Array) schema.fetch(:default, []).concat(value).uniq! else value end self end |
permalink #dependencies(dependencies) ⇒ Object
[View source]
271 272 273 |
# File 'lib/lazy_graph/builder/dsl.rb', line 271 def dependencies(dependencies) schema[:dependencies] = HashUtils.deep_merge(schema[:dependencies] || {}, dependencies) end |
permalink #depends_on(*dependencies) ⇒ Object
[View source]
343 344 345 346 347 348 349 |
# File 'lib/lazy_graph/builder/dsl.rb', line 343 def depends_on(*dependencies) @resolved_dependencies ||= Hash.new do |h, k| h[k] = true send(k) # Load dependency once end dependencies.each(&@resolved_dependencies.method(:[])) end |
permalink #items(&blk) ⇒ Object
[View source]
302 303 304 |
# File 'lib/lazy_graph/builder/dsl.rb', line 302 def items(&blk) yields(schema[:items], &blk) end |
permalink #matches(name, invisible: true, **when_clause, &blk) ⇒ Object
[View source]
102 103 104 105 |
# File 'lib/lazy_graph/builder/dsl.rb', line 102 def matches(name, invisible: true, **when_clause, &blk) @match_cases << { name: name, when_clause: when_clause, schema: { invisible: invisible } } yields(@match_cases.last[:schema], &blk) end |
permalink #object(name = nil, required: false, pattern_property: false, rule: nil, copy: nil, default: nil, description: nil, extend: nil, **opts, &blk) ⇒ Object
[View source]
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/lazy_graph/builder/dsl.rb', line 112 def object( name = nil, required: false, pattern_property: false, rule: nil, copy: nil, default: nil, description: nil, extend: nil, **opts, &blk ) rule ||= rule_from_when(opts.delete(:when)) if opts[:when] rule ||= rule_from_first_of(opts.delete(:first_of)) if opts[:first_of] rule ||= rule_from_copy(copy) new_object = { type: :object, properties: {}, additionalProperties: false, **(!default.nil? ? { default: default } : {}), **(description ? { description: description } : {}), **(rule ? { rule: rule, rule_location: rule_location } : {}), validate_presence: required, **opts } yields(new_object, &lambda { blk&.call extend&.call }) required(name) if required && default.nil? && rule.nil? pattern_property ? set_pattern_property(name, new_object) : set_property(name, new_object) end |
permalink #object_conditional(name = nil, required: false, pattern_property: false, rule: nil, copy: nil, default: nil, description: nil, extend: nil, conditions: nil, **opts, &blk) ⇒ Object
[View source]
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lazy_graph/builder/dsl.rb', line 69 def object_conditional( name = nil, required: false, pattern_property: false, rule: nil, copy: nil, default: nil, description: nil, extend: nil, conditions: nil, **opts, &blk ) new_object = { type: :object, properties: {}, additionalProperties: false, **(!default.nil? ? { default: default } : {}), **(description ? { description: description } : {}), validate_presence: required, **opts } @prev_match_cases = @match_cases @match_cases = [] yields(new_object, &lambda { blk&.call extend&.call }) object_names = @match_cases.map do |match_case| rule = rule_from_when(match_case[:when_clause]) set_property(match_case[:name], { type: :object, rule: rule, rule_location: rule_location, **match_case[:schema] }) match_case[:name] end new_object[:rule] = rule_from_first_of(object_names, conditions) @match_cases = @prev_match_cases required(name) if required && default.nil? && rule.nil? pattern_property ? set_pattern_property(name, new_object) : set_property(name, new_object) end |
permalink #one_of(one_of) ⇒ Object
[View source]
275 276 277 |
# File 'lib/lazy_graph/builder/dsl.rb', line 275 def one_of(one_of) schema[:oneOf] = (schema[:one_of] || []).concat(one_of).uniq end |
permalink #primitive(name = nil, type, required: false, pattern_property: false, default: nil, description: nil, enum: nil, rule: nil, copy: nil, **additional_options, &blk) ⇒ Object
[View source]
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/lazy_graph/builder/dsl.rb', line 137 def primitive( name = nil, type, required: false, pattern_property: false, default: nil, description: nil, enum: nil, rule: nil, copy: nil, **, &blk ) rule ||= rule_from_copy(copy) new_primitive = { type: type, **(enum ? { enum: enum } : {}), **(!default.nil? ? { default: default } : {}), **(description ? { description: description } : {}), **(rule ? { rule: rule, rule_location: rule_location } : {}), ** } yields(new_primitive, &blk) required(name) if required && default.nil? && rule.nil? pattern_property ? set_pattern_property(name, new_primitive) : set_property(name, new_primitive) end |
permalink #required(*keys) ⇒ Object
[View source]
24 25 26 27 |
# File 'lib/lazy_graph/builder/dsl.rb', line 24 def required(*keys) (schema[:required] ||= []).concat(keys.map(&:to_s)).uniq! self end |
permalink #rule_from_copy(copy) ⇒ Object
[View source]
316 317 318 319 320 |
# File 'lib/lazy_graph/builder/dsl.rb', line 316 def rule_from_copy(copy) return unless copy "${#{copy}}" end |
permalink #rule_from_first_of(prop_list, conditions = nil) ⇒ Object
[View source]
334 335 336 337 338 339 340 341 |
# File 'lib/lazy_graph/builder/dsl.rb', line 334 def rule_from_first_of(prop_list, conditions = nil) prop_list += conditions.keys if conditions { inputs: prop_list, calc: "itself.get_first_of(:#{prop_list.join(', :')})", **(conditions ? { conditions: conditions } : {}) } end |
permalink #rule_from_when(when_clause) ⇒ Object
[View source]
322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/lazy_graph/builder/dsl.rb', line 322 def rule_from_when(when_clause) inputs = when_clause.keys conditions = when_clause calc = "{#{when_clause.keys.map { |k| "#{k}: #{k}}" }.join(', ')}" { inputs: inputs, conditions: conditions, fixed_result: when_clause, calc: calc } end |
permalink #rule_location ⇒ Object
[View source]
107 108 109 110 |
# File 'lib/lazy_graph/builder/dsl.rb', line 107 def rule_location @dir ||= File.(File.dirname(__FILE__), '../../../../') caller.find { |c| !c.include?(@dir) }.split(':').first(2) end |
permalink #set_pattern_property(pattern, value) ⇒ Object
[View source]
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lazy_graph/builder/dsl.rb', line 41 def set_pattern_property(pattern, value) raise 'Trying to set a property without a name' unless pattern.is_a?(String) || pattern.is_a?(Symbol) pattern = pattern.to_sym properties = schema[:patternProperties] ||= {} properties[pattern] = \ if properties.key?(pattern) && %i[object array].include?(properties[pattern][:type]) HashUtils.deep_merge(properties[pattern], value, key) else value end self end |
permalink #set_property(key, value) ⇒ Object
[View source]
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lazy_graph/builder/dsl.rb', line 55 def set_property(key, value) raise 'Trying to set a property without a name' unless key.is_a?(String) || key.is_a?(Symbol) key = key.to_sym properties = schema[:properties] ||= {} properties[key] = \ if properties.key?(key) && %i[object array].include?(properties[key][:type]) HashUtils.deep_merge(properties[key], value, key) else value end self end |
permalink #time(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) ⇒ Object
[View source]
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/lazy_graph/builder/dsl.rb', line 217 def time(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) rule ||= rule_from_copy(copy) new_time = { type: :time, # Custom extended type # Matches HH:mm[:ss[.SSS]] pattern: '^\\d{2}:\\d{2}(:\\d{2}(\\.\\d{1,3})?)?$', **(!default.nil? ? { default: default } : {}), **(description ? { description: description } : {}), **(rule ? { rule: rule, rule_location: rule_location } : {}), validate_presence: required, **opts } yields(new_time, &blk) required(name) if required && default.nil? && rule.nil? pattern_property ? set_pattern_property(name, new_time) : set_property(name, new_time) end |
permalink #timestamp(name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) ⇒ Object
[View source]
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/lazy_graph/builder/dsl.rb', line 188 def (name = nil, required: false, pattern_property: false, default: nil, description: nil, rule: nil, copy: nil, **opts, &blk) rule ||= rule_from_copy(copy) = { anyOf: [ { type: :string, # Matches ISO 8601 timestamp without timezone pattern: '^\d{4}-\d{2}-\d{2}(T\d{2}(:\d{2}(:\d{2}(\.\d{1,3})?)?)?(Z|[+-]\d{2}(:\d{2})?)?)?$' }, { type: :number # Allows numeric epoch timestamps }, { type: :integer # Allows both float and int } ], type: :timestamp, # Custom extended type **(!default.nil? ? { default: default } : {}), **(description ? { description: description } : {}), **(rule ? { rule: rule, rule_location: rule_location } : {}), validate_presence: required, **opts } yields(, &blk) required(name) if required && default.nil? && rule.nil? pattern_property ? set_pattern_property(name, ) : set_property(name, ) end |
permalink #yields(other) ⇒ Object
306 307 308 309 310 311 312 313 314 |
# File 'lib/lazy_graph/builder/dsl.rb', line 306 def yields(other) raise ArgumentError, 'Builder DSL used outside of rules module' unless schema return unless block_given? prev_schema = schema self.schema = other yield self.schema = prev_schema end |