Class: Sinatra::Schema::Reference
- Inherits:
-
Object
- Object
- Sinatra::Schema::Reference
- Defined in:
- lib/sinatra/schema/reference.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#options ⇒ Object
Returns the value of attribute options.
-
#ref_spec ⇒ Object
Returns the value of attribute ref_spec.
-
#referenced_def ⇒ Object
Returns the value of attribute referenced_def.
-
#resource ⇒ Object
Returns the value of attribute resource.
Class Method Summary collapse
-
.delegate(attribute) ⇒ Object
helper to lazily delegate method to the referenced definition.
Instance Method Summary collapse
-
#initialize(resource, id, options = {}) ⇒ Reference
constructor
A new instance of Reference.
- #resolve! ⇒ Object
-
#valid?(value) ⇒ Boolean
redefine to make sure overrides on “optional” are considered.
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, ={}) @resource = resource @id = id @ref_spec = [:to] || id @options = end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/sinatra/schema/reference.rb', line 4 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/sinatra/schema/reference.rb', line 4 def @options end |
#ref_spec ⇒ Object
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_def ⇒ Object
Returns the value of attribute referenced_def.
4 5 6 |
# File 'lib/sinatra/schema/reference.rb', line 4 def referenced_def @referenced_def end |
#resource ⇒ Object
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 [attribute] if .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
40 41 42 43 |
# File 'lib/sinatra/schema/reference.rb', line 40 def valid?(value) resolve! referenced_def.valid?(value, self.optional) end |