Class: JSON::SchemaBuilder::Entity

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/json/schema_builder/entity.rb

Direct Known Subclasses

Array, Boolean, Null, Numeric, Object, String

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#entity

Constructor Details

#initialize(name, opts = { }, &block) ⇒ Entity

Returns a new instance of Entity.



35
36
37
38
39
40
41
42
# File 'lib/json/schema_builder/entity.rb', line 35

def initialize(name, opts = { }, &block)
  @name = name
  @children = []
  self.type = self.class.registered_type
  initialize_parent_with opts
  initialize_with opts
  eval_block &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/json/schema_builder/entity.rb', line 71

def method_missing(method_name, *args, &block)
  if @parent_context && respond_to?(method_name, true)
    @parent_context.send method_name, *args, &block
  else
    super
  end
end

Instance Attribute Details

#children ⇒ Object

Returns the value of attribute children.



9
10
11
# File 'lib/json/schema_builder/entity.rb', line 9

def children
  @children
end

#name ⇒ Object

Returns the value of attribute name.



9
10
11
# File 'lib/json/schema_builder/entity.rb', line 9

def name
  @name
end

#parent ⇒ Object

Returns the value of attribute parent.



9
10
11
# File 'lib/json/schema_builder/entity.rb', line 9

def parent
  @parent
end

Class Method Details

.attribute(name, as: nil, array: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/json/schema_builder/entity.rb', line 11

def self.attribute(name, as: nil, array: false)
  as_name = as || name.to_s.underscore.gsub(/_(\w)/){ $1.upcase }
  define_method name do |*values|
    value = array ? values.flatten : values.first
    if (array && value.empty?) || value.nil?
      self.schema[as_name]
    else
      self.schema[as_name] = value
    end
  end
  alias_method "#{ name }=", name
end

Instance Method Details

#as_json ⇒ Object



59
60
61
# File 'lib/json/schema_builder/entity.rb', line 59

def as_json
  schema.to_h.as_json
end

#merge_children! ⇒ Object



53
54
55
56
57
# File 'lib/json/schema_builder/entity.rb', line 53

def merge_children!
  children.each do |child|
    schema.merge! child.schema
  end
end

#required=(*values) ⇒ Object



48
49
50
51
# File 'lib/json/schema_builder/entity.rb', line 48

def required=(*values)
  @parent.required ||= []
  @parent.required << @name
end

#respond_to?(method_name, include_all = false) ⇒ Boolean

Returns:



63
64
65
66
67
68
69
# File 'lib/json/schema_builder/entity.rb', line 63

def respond_to?(method_name, include_all = false)
  if @parent_context
    @parent_context.respond_to? method_name, include_all
  else
    super
  end
end

#schema ⇒ Object



44
45
46
# File 'lib/json/schema_builder/entity.rb', line 44

def schema
  @schema ||= Schema.new
end