Class: JSON::Schema::Lite::Block

Inherits:
Hash
  • Object
show all
Defined in:
lib/json/schema/lite/block.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Block

Returns a new instance of Block.



6
7
8
# File 'lib/json/schema/lite/block.rb', line 6

def initialize(&block)
  self.instance_eval &block
end

Instance Method Details

#array(key, *args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/json/schema/lite/block.rb', line 17

def array(key, *args, &block)
  prop = {
    type: :array
  }
  if block_given?
    prop[:items] = self.class.new(&block)
    opt_index = 0
  else
    prop[:items] = { type: args[0] }
    opt_index = 1
  end
  if args[opt_index].is_a?(Hash)
    option = args[opt_index]
    add_required key if option[:required]
  end
  set_properties key, prop
end

#object(key, option = {}, &block) ⇒ Object



35
36
37
38
# File 'lib/json/schema/lite/block.rb', line 35

def object(key, option = {}, &block)
  set_properties key, self.class.new(&block)
  add_required key if option[:required]
end