Class: Halibut::Builder::RootContext
- Inherits:
-
Object
- Object
- Halibut::Builder::RootContext
- Defined in:
- lib/halibut/builder.rb
Overview
This is the root context of Halibut::Builder.
Instance Method Summary collapse
-
#initialize(resource, &resource_definition) ⇒ RootContext
constructor
A new instance of RootContext.
-
#link(relation, href, options = {}) ⇒ Halibut::Core::Resource
Adds a link to the respection relation of the resource.
-
#namespace(name, href) ⇒ Object
Adds a namespace to the resource.
-
#property(name, value) ⇒ Halibut::Core::resource
Sets a property on the resource.
-
#relation(rel, &relation_definition) ⇒ Object
Adds links or resources to a relation.
-
#resource(rel, href = nil, &embedded_definition) ⇒ Object
Adds an embedded resource.
Constructor Details
#initialize(resource, &resource_definition) ⇒ RootContext
Returns a new instance of RootContext.
31 32 33 34 35 |
# File 'lib/halibut/builder.rb', line 31 def initialize(resource, &resource_definition) @resource = resource instance_eval(&resource_definition) if block_given? end |
Instance Method Details
#link(relation, href, options = {}) ⇒ Halibut::Core::Resource
Adds a link to the respection relation of the resource.
53 54 55 |
# File 'lib/halibut/builder.rb', line 53 def link(relation, href, ={}) @resource.add_link relation, href, end |
#namespace(name, href) ⇒ Object
Adds a namespace to the resource.
A namespace is a conceptual abstraction of CURIE links. Since the client of the library doesn’t need to handle CURIE links directly because they’re just for dereferencing link relations, no CURIE links are presented.
resource = Halibut::Builder.new do
namespace :john, 'http://appleseed.com'
end.resource
resource.namespace(:john).href
# => "http://appleseed.com"
72 73 74 |
# File 'lib/halibut/builder.rb', line 72 def namespace(name, href) @resource.add_namespace(name, href) end |
#property(name, value) ⇒ Halibut::Core::resource
Sets a property on the resource. Will overwrite any same-named existing property.
43 44 45 |
# File 'lib/halibut/builder.rb', line 43 def property(name, value) @resource.set_property name, value end |
#relation(rel, &relation_definition) ⇒ Object
Adds links or resources to a relation.
Relation allows the user to specify links, or resources, per relation, instead of individually. This feature was introduced as an attempt to reduce repeating the relation per link/resource, and thus reducing typos.
resource = Halibut::Builder.new do
relation :john do
link 'http://appleseed.com/john'
end
end.resource
resource.links[:john].first.href
104 105 106 |
# File 'lib/halibut/builder.rb', line 104 def relation(rel, &relation_definition) RelationContext.new(@resource, rel, &relation_definition) end |