Class: Sinatra::Schema::Reference

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, id, options = {}) ⇒ Reference

Returns a new instance of Reference.



18
19
20
21
22
23
# File 'lib/sinatra/schema/reference.rb', line 18

def initialize(resource, id, options={})
  @resource = resource
  @id       = id
  @ref_spec = options[:to] || id
  @options  = options
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/sinatra/schema/reference.rb', line 4

def id
  @id
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/sinatra/schema/reference.rb', line 4

def options
  @options
end

#ref_specObject

Returns the value of attribute ref_spec.



4
5
6
# File 'lib/sinatra/schema/reference.rb', line 4

def ref_spec
  @ref_spec
end

#referenced_defObject

Returns the value of attribute referenced_def.



4
5
6
# File 'lib/sinatra/schema/reference.rb', line 4

def referenced_def
  @referenced_def
end

#resourceObject

Returns the value of attribute resource.



4
5
6
# File 'lib/sinatra/schema/reference.rb', line 4

def resource
  @resource
end

Class Method Details

.delegate(attribute) ⇒ Object

helper to lazily delegate method to the referenced definition



7
8
9
10
11
12
13
14
15
16
# File 'lib/sinatra/schema/reference.rb', line 7

def self.delegate(attribute)
  define_method(attribute) do |*args|
    # support overriding attributes. example: referenced
    # definition is optional, but parameter is not
    return options[attribute] if options.has_key?(attribute)

    resolve!
    referenced_def.send(attribute, *args)
  end
end

Instance Method Details

#resolve!Object



25
26
27
28
29
30
31
# File 'lib/sinatra/schema/reference.rb', line 25

def resolve!
  return if referenced_def
  unless @referenced_def = resource.defs[ref_spec] || Sinatra::Schema::Root.instance.find_definition(ref_spec)
    raise BadReference.new(ref_spec)
  end
  return @referenced_def
end

#valid?(value) ⇒ Boolean

redefine to make sure overrides on “optional” are considered

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/sinatra/schema/reference.rb', line 40

def valid?(value)
  resolve!
  referenced_def.valid?(value, self.optional)
end