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, ref_spec = nil) ⇒ Reference

Returns a new instance of Reference.



14
15
16
17
18
# File 'lib/sinatra/schema/reference.rb', line 14

def initialize(resource, id, ref_spec=nil)
  @id = id
  @ref_spec = ref_spec || id
  @resource = resource
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

#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
# File 'lib/sinatra/schema/reference.rb', line 7

def self.delegate(attribute)
  define_method(attribute) do |*args|
    resolve!
    referenced_def.send(attribute, *args)
  end
end

Instance Method Details

#resolve!Object



20
21
22
23
24
25
26
# File 'lib/sinatra/schema/reference.rb', line 20

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