Class: AmsHal::Embed

Inherits:
Object
  • Object
show all
Includes:
ActiveModelSerializers::SerializationContext::UrlHelpers
Defined in:
lib/ams_hal/embed.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, name, options = {}, &block) ⇒ Embed

Returns a new instance of Embed.



9
10
11
12
13
# File 'lib/ams_hal/embed.rb', line 9

def initialize(serializer, name, options = {}, &block)
  @name = name
  @options = options
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



31
32
33
34
# File 'lib/ams_hal/embed.rb', line 31

def method_missing(method, *args, &block)
  return super unless @self_before_instance_eval
  @self_before_instance_eval.send method, *args, &block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/ams_hal/embed.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/ams_hal/embed.rb', line 7

def options
  @options
end

Instance Method Details

#resource_for(serializer) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ams_hal/embed.rb', line 15

def resource_for(serializer)
  @object = serializer.object
  @scope = serializer.scope

  @self_before_instance_eval = nil
  # Use the return value of the block unless it is nil.
  if @block
    @self_before_instance_eval = eval "self", @block.binding
    @resource = instance_eval(&@block)
  else
    @resource = @object.public_send(name)
  end
end