Class: Halibut::Builder::RelationContext

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

Instance Method Summary collapse

Constructor Details

#initialize(resource, rel, &relation_definition) ⇒ RelationContext

Evaluates the DSL methods in the context of a relation.


134
135
136
137
138
139
# File 'lib/halibut/builder.rb', line 134

def initialize(resource, rel, &relation_definition)
  @resource = resource
  @rel      = rel

  instance_eval(&relation_definition) if block_given?
end

Instance Method Details

Adds a link to the resource for the parent relation. Since we're in the context of a relation, only the href and the options are necessary.

Examples:

Builds a resource with a link to a `games` relation.

builder = Halibut::Builder.new do
  relation 'games' do
    link '/games/1'
  end
end

154
155
156
# File 'lib/halibut/builder.rb', line 154

def link(href, opts={})
  @resource.tap {|obj| obj.add_link(@rel, href, opts) }
end

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

Adds an embedded resource for the parent relation. Since we're in the context of a relation, only the href and the resource definition is necessary.

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

Examples:

Builds a resource with an embedded user for the `users`

relation.

175
176
177
178
179
# File 'lib/halibut/builder.rb', line 175

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

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