Class: SexyJSONSchemas::Properties::Array

Inherits:
Base
  • Object
show all
Defined in:
lib/sexy_json_schemas/properties/array.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Array

Returns a new instance of Array.



20
21
22
23
24
25
# File 'lib/sexy_json_schemas/properties/array.rb', line 20

def initialize(*args, &block)
  super(*args)
  @item_schemas = []

  instance_eval(&block) if block_given?
end

Class Method Details

.define_property_delegator(property) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/sexy_json_schemas/properties/array.rb', line 4

def self.define_property_delegator(property)
  define_method(property) do |*args|
    blank_object = Properties::AnonymousObject.new
    blank_object.send(property, '', *args)
    @item_schemas << blank_object
  end
end

Instance Method Details

#as_jsonObject



28
29
30
31
32
# File 'lib/sexy_json_schemas/properties/array.rb', line 28

def as_json
  super.tap do |json|
    json['items'] = items_json
  end
end

#items_jsonObject



34
35
36
37
38
39
40
41
42
# File 'lib/sexy_json_schemas/properties/array.rb', line 34

def items_json
  if @item_schemas.length == 0
    {}
  elsif @item_schemas.length == 1
    @item_schemas.first.as_json
  else
    @item_schemas.map(&:as_json)
  end
end

#typeObject



16
17
18
# File 'lib/sexy_json_schemas/properties/array.rb', line 16

def type
  "array"
end