Class: Respect::CompositeSchema
- Inherits:
 - 
      Schema
      
        
- Object
 - Schema
 - Respect::CompositeSchema
 
 - Defined in:
 - lib/respect/composite_schema.rb
 
Overview
A composite schema is a schema composed of another schema.
Sub-classing CompositeSchema is the easiest way to add a user-defined schema. Indeed, you just have to override #schema_definition and optionally #sanitize. Your schema will be handled properly by all other part of the library (i.e. mainly dumpers and the DSL).
Example:
module Respect
  class PointSchema < CompositeSchema
    def schema_defintion
      HashSchema.define do |s|
        s.numeric "x"
        s.numeric "y"
      end
    end
    def sanitize(object)
      # Assuming you have defined a Point class.
      Point.new(object[:x], object[:y])
    end
  end
end
A "point" method will be available in the DSL so you could use your schema like that:
Example:
HashSchema.define do |s|
  s.point "origin"
end
  Instance Attribute Summary (collapse)
- 
  
    
      - (Object) schema 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the schema composing this schema.
 
Attributes inherited from Schema
#last_error, #options, #sanitized_object
Class Method Summary (collapse)
Instance Method Summary (collapse)
- 
  
    
      - (CompositeSchema) initialize(options = {}) 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of CompositeSchema.
 - 
  
    
      - (Object) sanitize(object) 
    
    
  
  
  
  
  
  
  
  
  
    
Sanitize the given validated object.
 - 
  
    
      - (Object) schema_definition 
    
    
  
  
  
  
  
  
  
  
  
    
Returns the schema composing this composite schema.
 - 
  
    
      - (Object) validate(object) 
    
    
  
  
  
  
  
  
  
  
  
    
Overloaded methods (see Schema#validate).
 
Methods inherited from Schema
#==, #allow_nil?, def_class, def_class_name, #default, default_options, define, #documentation, #documented?, #has_default?, #initialize_copy, #inspect, #non_default_options, #optional?, #required?, #sanitize!, #sanitize_object!, statement_name, #to_h, #to_json, #to_s, #validate!, #validate?
Methods included from DocHelper
Constructor Details
- (CompositeSchema) initialize(options = {})
A new instance of CompositeSchema
      41 42 43 44  | 
    
      # File 'lib/respect/composite_schema.rb', line 41 def initialize( = {}) super @schema = self.schema_definition end  | 
  
Instance Attribute Details
- (Object) schema (readonly)
Returns the schema composing this schema.
      47 48 49  | 
    
      # File 'lib/respect/composite_schema.rb', line 47 def schema @schema end  | 
  
Class Method Details
+ (Object) inherited(subclass)
      36 37 38  | 
    
      # File 'lib/respect/composite_schema.rb', line 36 def inherited(subclass) subclass.public_class_method :new end  | 
  
Instance Method Details
- (Object) sanitize(object)
Sanitize the given validated object. Overwrite this method in sub-class and returns the object that would be inserted in the sanitized object. The object passed as argument is an already sanitized sub-part of the overall object being validated. By default this method is a no-op. It returns the given object.
      81 82 83  | 
    
      # File 'lib/respect/composite_schema.rb', line 81 def sanitize(object) object end  | 
  
- (Object) schema_definition
Returns the schema composing this composite schema. Overwrite this methods in sub-class.
      71 72 73  | 
    
      # File 'lib/respect/composite_schema.rb', line 71 def schema_definition raise NoMethodError, "implement me in sub-class" end  | 
  
- (Object) validate(object)
Overloaded methods (see Schema#validate).
      50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67  | 
    
      # File 'lib/respect/composite_schema.rb', line 50 def validate(object) # Handle nil case. if object.nil? if allow_nil? self.sanitized_object = nil return true else raise ValidationError, "object is nil but this #{self.class.name} does not allow nil" end end @schema.validate(object) self.sanitized_object = sanitize(@schema.sanitized_object) true rescue ValidationError => e # Reset sanitized object. self.sanitized_object = nil raise e end  |