Class: JSON::SchemaBuilder::Entity

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

Direct Known Subclasses

Any, Array, Boolean, Null, Numeric, Object, String

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#empty_string, #id, #object_or_array

Methods included from Validation

#fully_validate, #validate, #validate!

Methods included from DSL

#entity

Constructor Details

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

Returns a new instance of Entity.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/json/schema_builder/entity.rb', line 37

def initialize(name, opts = { }, &block)
  @name = name
  @children = []
  @fragments = Hash.new { |hash, key| hash[key] = ::Array.new }
  @fragments["#/"] << self if opts[:root]
  self.type = self.class.registered_type
  initialize_parent_with opts
  initialize_with opts
  eval_block &block
  extract_types
  @initialized = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



111
112
113
114
115
116
117
# File 'lib/json/schema_builder/entity.rb', line 111

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

#childrenObject

Returns the value of attribute children.



15
16
17
# File 'lib/json/schema_builder/entity.rb', line 15

def children
  @children
end

#errorObject

Returns the value of attribute error.



15
16
17
# File 'lib/json/schema_builder/entity.rb', line 15

def error
  @error
end

#fragmentObject

Returns the value of attribute fragment.



15
16
17
# File 'lib/json/schema_builder/entity.rb', line 15

def fragment
  @fragment
end

#fragmentsObject

Returns the value of attribute fragments.



15
16
17
# File 'lib/json/schema_builder/entity.rb', line 15

def fragments
  @fragments
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/json/schema_builder/entity.rb', line 15

def name
  @name
end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/json/schema_builder/entity.rb', line 15

def options
  @options
end

#parentObject

Returns the value of attribute parent.



15
16
17
# File 'lib/json/schema_builder/entity.rb', line 15

def parent
  @parent
end

Class Method Details

.disable_attributes!(*attributes) ⇒ Object



30
31
32
33
34
35
# File 'lib/json/schema_builder/entity.rb', line 30

def self.disable_attributes!(*attributes)
  attributes.each do |attr|
    undef_method attr rescue NameError
    undef_method "#{attr}=" rescue NameError
  end
end

Instance Method Details

#add_fragment(child) ⇒ Object



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

def add_fragment(child)
  @fragments[child.fragment] << child
  parent.add_fragment(child) if @parent
end

#as_jsonObject



95
96
97
# File 'lib/json/schema_builder/entity.rb', line 95

def as_json
  schema.as_json
end

#extend(child_name, &block) ⇒ Object



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

def extend(child_name, &block)
  children.find { |c| c.name == child_name.to_sym }.tap do |child|
    raise "Property #{child_name} does not exist" unless child
    child.eval_block(&block) if block_given?
  end
end

#initialized?Boolean

Returns:



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

def initialized?
  !!@initialized
end

#inspectObject



99
100
101
# File 'lib/json/schema_builder/entity.rb', line 99

def inspect
  "#<#{self.class.name}:#{object_id} @schema=#{schema.as_json}>"
end

#merge_children!Object



88
89
90
91
92
93
# File 'lib/json/schema_builder/entity.rb', line 88

def merge_children!
  return if any_of.present?
  children.each do |child|
    schema.merge! child.schema
  end
end

#reinitializeObject



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

def reinitialize
end

#requiredObject



79
80
81
# File 'lib/json/schema_builder/entity.rb', line 79

def required
  schema["required"] || []
end

#required=(*values) ⇒ Object



83
84
85
86
# File 'lib/json/schema_builder/entity.rb', line 83

def required=(*values)
  @parent.schema["required"] ||= []
  @parent.schema["required"] << @name if values.any?
end

#reset_fragmentObject



69
70
71
72
73
# File 'lib/json/schema_builder/entity.rb', line 69

def reset_fragment
  @fragment = [@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
  root._reset_fragments
  root.fragments["#/"] << root
end

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

Returns:



103
104
105
106
107
108
109
# File 'lib/json/schema_builder/entity.rb', line 103

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

#schemaObject



75
76
77
# File 'lib/json/schema_builder/entity.rb', line 75

def schema
  @schema ||= Schema.new({}, self)
end