Class: Jschematic::Schema
- Inherits:
-
Object
- Object
- Jschematic::Schema
- Includes:
- Composite
- Defined in:
- lib/jschematic/schema.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#unknown_attributes ⇒ Object
readonly
Returns the value of attribute unknown_attributes.
Attributes included from Element
Class Method Summary collapse
-
.with_name(raw_schema, name) ⇒ Object
TODO: Spec.
Instance Method Summary collapse
- #accepts?(instance) ⇒ Boolean
-
#initialize(raw_schema, parent = Context.empty) ⇒ Schema
constructor
A new instance of Schema.
- #required? ⇒ Boolean
- #schema_for(ref) ⇒ Object
Methods included from Composite
Methods included from Element
Constructor Details
#initialize(raw_schema, parent = Context.empty) ⇒ Schema
Returns a new instance of Schema.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jschematic/schema.rb', line 22 def initialize(raw_schema, parent = Context.empty) @raw_schema = raw_schema.dup || {} @parent = parent @default = @raw_schema.delete("default") @title = @raw_schema.delete("title") || "" @description = @raw_schema.delete("description") || "" @schema = @raw_schema.delete("$schema") @id = Addressable::URI.parse(@raw_schema.delete("id") || "") @unknown_attributes = {} @parent.add_schema(@id, self) unless @id.to_s.empty? @raw_schema.each_pair do |attribute, value| begin attribute = Attributes[attribute].new(value){ |dep| @raw_schema[dep] } add_child(attribute) rescue NameError => e warn "Attribute lookup failed for '#{attribute}' with: #{e.}" if Jschematic.debug @unknown_attributes[attribute] = value end end end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
18 19 20 |
# File 'lib/jschematic/schema.rb', line 18 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
18 19 20 |
# File 'lib/jschematic/schema.rb', line 18 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/jschematic/schema.rb', line 20 def name @name end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
18 19 20 |
# File 'lib/jschematic/schema.rb', line 18 def schema @schema end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
18 19 20 |
# File 'lib/jschematic/schema.rb', line 18 def title @title end |
#unknown_attributes ⇒ Object (readonly)
Returns the value of attribute unknown_attributes.
18 19 20 |
# File 'lib/jschematic/schema.rb', line 18 def unknown_attributes @unknown_attributes end |
Class Method Details
.with_name(raw_schema, name) ⇒ Object
TODO: Spec
12 13 14 15 16 |
# File 'lib/jschematic/schema.rb', line 12 def self.with_name(raw_schema, name) schema = new(raw_schema) schema.name = name schema end |
Instance Method Details
#accepts?(instance) ⇒ Boolean
47 48 49 |
# File 'lib/jschematic/schema.rb', line 47 def accepts?(instance) children.all?{ |child| child.accepts?(add_default(instance)) } end |
#required? ⇒ Boolean
51 52 53 |
# File 'lib/jschematic/schema.rb', line 51 def required? children.any?{ |child| child.required? } end |
#schema_for(ref) ⇒ Object
55 56 57 |
# File 'lib/jschematic/schema.rb', line 55 def schema_for(ref) parent.schema_for(ref) end |