Class: StandardId::ConfigSchema::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_id/config_schema.rb

Overview

DSL: ‘define { scope :base do field :foo, type: :string, default: “x” end }`. Anonymous-class form keeps both levels in one place; #scope yields a sub-DSL that closes over the parent schema + scope name.

Instance Method Summary collapse

Constructor Details

#initialize(schema, scope_name = nil) ⇒ DSL

Returns a new instance of DSL.



93
94
95
96
# File 'lib/standard_id/config_schema.rb', line 93

def initialize(schema, scope_name = nil)
  @schema = schema
  @scope_name = scope_name
end

Instance Method Details

#field(name, type: :string, default: nil) ⇒ Object



103
104
105
# File 'lib/standard_id/config_schema.rb', line 103

def field(name, type: :string, default: nil, **)
  @schema.add_field(scope: @scope_name, name: name, type: type, default: default)
end

#scope(name, &block) ⇒ Object



98
99
100
101
# File 'lib/standard_id/config_schema.rb', line 98

def scope(name, &block)
  @schema.ensure_scope(name)
  DSL.new(@schema, name.to_sym).instance_eval(&block) if block
end