Class: Copland::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/copland/schema.rb

Overview

This class represents a single node in a schema “tree”. The entire tree is the actual schema, where it is recursively defined using instances of this class.

Defined Under Namespace

Modules: Fixated

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, processor, description, required, type, definition, extends) ⇒ Schema

Create a new schema node with the given name, processor class, description, required-flag, type, and definition. If processor is nil, then the DefaultSchemaProcessor class will be used.



72
73
74
75
76
77
78
79
# File 'lib/copland/schema.rb', line 72

def initialize( name, processor, description,
                required, type, definition,
                extends )
#begin
  @definition, @description, @name = definition, description, name
  @processor_path, @required, @type = processor, required, type
  @extends = extends
end

Instance Attribute Details

#definitionObject (readonly)

The children nodes of this schema node. If nil, then this is a leaf node.



45
46
47
# File 'lib/copland/schema.rb', line 45

def definition
  @definition
end

#descriptionObject (readonly)

The optional description of this schema.



48
49
50
# File 'lib/copland/schema.rb', line 48

def description
  @description
end

#extendsObject

The schema that this schema extends.



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

def extends
  @extends
end

#nameObject (readonly)

The optional name of this schema.



55
56
57
# File 'lib/copland/schema.rb', line 55

def name
  @name
end

#ownerObject

The owning package of this schema.



64
65
66
# File 'lib/copland/schema.rb', line 64

def owner
  @owner
end

#processor_pathObject (readonly)

The path description that identifies the class to use when processing this schema.



52
53
54
# File 'lib/copland/schema.rb', line 52

def processor_path
  @processor_path
end

#requiredObject (readonly)

Whether or not this node is required.



58
59
60
# File 'lib/copland/schema.rb', line 58

def required
  @required
end

#typeObject (readonly)

The type of this node. If nil, then it may be of any type.



61
62
63
# File 'lib/copland/schema.rb', line 61

def type
  @type
end

Instance Method Details

#fixate!Object

Fixate the schema by recursively processing each subschema. If any subschema is actually a string, it is replaced by the schema with that name.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/copland/schema.rb', line 164

def fixate!
  extend Fixated

  if @extends
    new_definition = @owner.find_schema( @extends ).definition.dup
    new_definition.update @definition if @definition
    @definition = new_definition
  end

  if @definition.is_a?( String )
    @definition = @owner.find_schema( @definition ).definition
  elsif @definition.is_a?( Hash )
    @definition.each_key do |key|
      value = @definition[ key ]
      if value.is_a?( String )
        @definition[ key ] = @owner.find_schema( value )
      else
        value.fixate!
      end
    end
  end
end

#fixated?Boolean

Returns false.

Returns:

  • (Boolean)


188
189
190
# File 'lib/copland/schema.rb', line 188

def fixated?
  false
end

#full_nameObject

Returns the fully-qualified name of this schema, but only if the schema has both a name and an owning package. Otherwise, it returns nil.



83
84
85
86
87
# File 'lib/copland/schema.rb', line 83

def full_name
  if @owner && @name
    @owner.name + "." + @name
  end
end

#process(owner, client, data, path = "/", processor = nil) ⇒ Object

Preprocess the given data against this schema node. The owner is the service point (or configuration point) that the schema is being processed for, and the client is the service point (or configuration point) that the data is being processed for. If path is given, it represents the path through the schema leading to this node. If processor is given, then it should be used to process this node as long as it is of the same type as this node’s processor.

The preprocessed data is returned.



128
129
130
131
132
133
134
135
136
# File 'lib/copland/schema.rb', line 128

def process( owner, client, data, path="/", processor=nil )
  processor = get_processor( processor )

  if processor.respond_to?( :process )
    return processor.process( owner, client, self, data, path )
  end

  return data
end

#validate(owner, client, data, path = "/", processor = nil) ⇒ Object

Validate the given data against this schema node. The owner is the service point (or configuration point) that the schema is being processed for, and the client is the service point (or configuration point) that the data is being processed for. If path is given, it represents the path through the schema leading to this node. If processor is given, then it should be used to process this node as long as it is of the same type as this node’s processor.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/copland/schema.rb', line 96

def validate( owner, client, data, path="/", processor=nil )
  # always make sure the schema has been fixated before we try to validate
  # anything with it.
  fixate!

  processor = get_processor( processor )

  if processor.respond_to?( :validate )
    if data.is_a?( Array ) ||
      ( data.respond_to?( :type_id ) && data.type_id == "array" )
    # then
      data = data.value if data.respond_to?( :type_id )
      index = 0
      data.each do |item|
        processor.validate( owner, client, self, item, "#{path}[#{index}]" )
        index += 1
      end
    else
      processor.validate( owner, client, self, data, path )
    end
  end
end