class HALPresenter::Property

Constants

NO_VALUE

Attributes

embed_depth[R]
name[R]

Public Class Methods

new(name, value = NO_VALUE, **kwargs, &block) click to toggle source
# File lib/hal_presenter/property.rb, line 9
def initialize(name, value = NO_VALUE, **kwargs, &block)
  @name = name.to_sym
  @value = value.freeze
  @embed_depth = kwargs[:embed_depth].freeze
  @context = kwargs[:context]
  @lazy = block_given? ? LazyEvaluator.new(block, @context) : nil
end

Public Instance Methods

change_context(context) click to toggle source
# File lib/hal_presenter/property.rb, line 33
def change_context(context)
  @context = context
  @lazy.update_context(context) if @lazy
  self
end
nested_depth_ok?(level) click to toggle source
# File lib/hal_presenter/property.rb, line 39
def nested_depth_ok?(level)
  return true unless embed_depth
  level <= embed_depth
end
value(resource = nil, options = {}) click to toggle source
# File lib/hal_presenter/property.rb, line 17
    def value(resource = nil, options = {})
      if @lazy
        @lazy.evaluate(resource, options)
      elsif @value != NO_VALUE
        @value
      elsif resource&.respond_to? name_without_curie
        resource.public_send(name_without_curie)
      else
        raise ArgumentError, <<~ERR
          Cannot serialize #{name.inspect}.
          No value given and resource does not respond to #{name_without_curie}. Resource:
          #{resource.inspect}"
          ERR
      end
    end

Private Instance Methods

initialize_copy(source) click to toggle source
Calls superclass method
# File lib/hal_presenter/property.rb, line 46
def initialize_copy(source)
  @lazy = source.instance_variable_get(:@lazy).clone
  super
end
name_without_curie() click to toggle source
# File lib/hal_presenter/property.rb, line 51
def name_without_curie
  @name_without_curie ||= name.to_s.split(':', 2).last
end