Class: Jschematic::Schema

Inherits:
Object
  • Object
show all
Includes:
Composite
Defined in:
lib/jschematic/schema.rb

Instance Attribute Summary collapse

Attributes included from Element

#id, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composite

#add_child, #children, #each

Methods included from Element

#to_s

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.message}" if Jschematic.debug
      @unknown_attributes[attribute] = value
    end
  end
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



18
19
20
# File 'lib/jschematic/schema.rb', line 18

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



18
19
20
# File 'lib/jschematic/schema.rb', line 18

def description
  @description
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/jschematic/schema.rb', line 20

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



18
19
20
# File 'lib/jschematic/schema.rb', line 18

def schema
  @schema
end

#titleObject (readonly)

Returns the value of attribute title.



18
19
20
# File 'lib/jschematic/schema.rb', line 18

def title
  @title
end

#unknown_attributesObject (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

Returns:

  • (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

Returns:

  • (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