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) ⇒ Schema

Returns a new instance of Schema.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jschematic/schema.rb', line 42

def initialize(raw_schema)
  @raw_schema  = raw_schema.dup || {}

  @default     = @raw_schema.delete("default")
  @title       = @raw_schema.delete("title") || ""
  @description = @raw_schema.delete("description") || ""
  @id          = Addressable::URI.parse(@raw_schema.delete("id") || "")

  self.class.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
      # Not finding an attribute is not necessarily an error, but this is
      # obviously not the right way to handle it. Need to find a better way to
      # report information.
      # should we create accessors for property on the schema?
      # we could have Attributes.[] raise a special exception rather than NameError
      puts "NameError #{e} encountered... continuing"
    end
  end
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



37
38
39
# File 'lib/jschematic/schema.rb', line 37

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



37
38
39
# File 'lib/jschematic/schema.rb', line 37

def description
  @description
end

#nameObject

Returns the value of attribute name.



39
40
41
# File 'lib/jschematic/schema.rb', line 39

def name
  @name
end

#parent=(value) ⇒ Object (writeonly)

Sets the attribute parent

Parameters:

  • value

    the value to set the attribute parent to.



40
41
42
# File 'lib/jschematic/schema.rb', line 40

def parent=(value)
  @parent = value
end

#titleObject (readonly)

Returns the value of attribute title.



37
38
39
# File 'lib/jschematic/schema.rb', line 37

def title
  @title
end

Class Method Details

.add_schema(id, schema) ⇒ Object



29
30
31
# File 'lib/jschematic/schema.rb', line 29

def self.add_schema(id, schema)
  schemas[id] = schema
end

.schema_for(ref) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/jschematic/schema.rb', line 18

def self.schema_for(ref)
  if ref.relative?
    rel = ref.omit(:scheme, :host, :port)
    if match = schemas.find{ |ref, _| rel == ref.omit(:scheme, :host, :port) }
      match[-1]
    end
  else
    schemas[ref]
  end
end

.schemasObject



33
34
35
# File 'lib/jschematic/schema.rb', line 33

def self.schemas
  @schemas ||= {}
end

.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)


67
68
69
# File 'lib/jschematic/schema.rb', line 67

def accepts?(instance)
  children.all?{ |child| child.accepts?(add_default(instance)) }
end

#required?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/jschematic/schema.rb', line 71

def required?
  children.any?{ |child| child.required? }
end