Class: Halibut::Builder::RootContext

Inherits:
Object
  • Object
show all
Defined in:
lib/halibut/builder.rb

Overview

This is the root context of Halibut::Builder.

Instance Method Summary collapse

Constructor Details

#initialize(resource, &resource_definition) ⇒ RootContext

Sets up the root context for the DSL methods.

Parameters:

  • resource (Halibut::Core::Resource)

    resource to be built

  • resource_definition (Proc)

    DSL methods to build the resource


37
38
39
40
41
# File 'lib/halibut/builder.rb', line 37

def initialize(resource, &resource_definition)
  @resource = resource

  instance_eval(&resource_definition) if block_given?
end

Instance Method Details

Adds a link to the respection relation of the resource.

Parameters:

  • relation (String)

    relation to which link will be added

  • href (String)

    URI of the link

  • options (Hash) (defaults to: {})

    Link optional parameters: templated, hreflang, etc

Returns:


59
60
61
# File 'lib/halibut/builder.rb', line 59

def link(relation, href, options={})
  @resource.add_link relation, href, options
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.

Examples:

Adds a namespace “john” to the resource

resource = Halibut::Builder.new do
  namespace :john, 'http://appleseed.com'
end.resource
resource.namespace(:john).href
# => "http://appleseed.com"

Parameters:

  • name (String, Symbol)

    name of the namespace

  • href (String)

    URI of the namespace


79
80
81
# File 'lib/halibut/builder.rb', line 79

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.

Parameters:

  • name (String)

    name of the property

  • value (String)

    value of the property

Returns:

  • (Halibut::Core::resource)

    resource with property set


49
50
51
# File 'lib/halibut/builder.rb', line 49

def property(name, value)
  @resource.set_property name, value
end

#relation(rel, &relation_definition) ⇒ RelationContext

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.

Examples:

resource = Halibut::Builder.new do
  relation :john do
    link 'http://appleseed.com/john'
  end
end.resource
resource.links[:john].first.href

Parameters:

  • rel (String, Symbol)
  • relation_definition (Proc)

    Instructions to be executed in the relation context

Returns:


121
122
123
# File 'lib/halibut/builder.rb', line 121

def relation(rel, &relation_definition)
  RelationContext.new(@resource, rel, &relation_definition)
end

#resource(rel, href = nil, &embedded_definition) ⇒ Object

Adds an embedded resource.

Examples:

Builds a resource with name and nick properties

resource = Halibut::Builder.new do
  resource '/users/1' do
    property :name, "foo"
    property :nick, "bar"
  end
end

Parameters:

  • rel (String)

    Embedded resource relation to the parent resource

  • href (String) (defaults to: nil)

    URI to the resource itself

  • embedded_definition (Proc)

    Instructions to construct the embedded resource


96
97
98
99
100
# File 'lib/halibut/builder.rb', line 96

def resource(rel, href=nil, &embedded_definition)
  embedded = Halibut::Builder.new(href, &embedded_definition)

  @resource.embed_resource(rel, embedded.resource)
end