Class: SimpleJsonapi::Node::ObjectLinks
- Defined in:
- lib/simple_jsonapi/node/object_links.rb
Overview
Represents a links
object (a collection of links).
Instance Attribute Summary collapse
- #link_definitions ⇒ Hash{Symbol => Definition::Link} readonly
- #object ⇒ Object readonly
Attributes inherited from Base
#extras, #fields_spec, #include_spec, #root_node, #serializer, #serializer_inferrer, #sort_spec
Instance Method Summary collapse
- #as_jsonapi ⇒ Hash{Symbol => Hash}
-
#initialize(object:, link_definitions:, **options) ⇒ ObjectLinks
constructor
A new instance of ObjectLinks.
Constructor Details
#initialize(object:, link_definitions:, **options) ⇒ ObjectLinks
Returns a new instance of ObjectLinks.
14 15 16 17 18 19 |
# File 'lib/simple_jsonapi/node/object_links.rb', line 14 def initialize(object:, link_definitions:, **) super() @object = object @link_definitions = link_definitions end |
Instance Attribute Details
#link_definitions ⇒ Hash{Symbol => Definition::Link} (readonly)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/simple_jsonapi/node/object_links.rb', line 8 class ObjectLinks < Base attr_reader :object, :link_definitions # @param object [Object] # @param link_definitions [Hash{Symbol => Definition::Link}] # @param options see {Node::Base#initialize} for additional parameters def initialize(object:, link_definitions:, **) super() @object = object @link_definitions = link_definitions end # @return [Hash{Symbol => Hash}] def as_jsonapi if link_definitions_to_render.any? json = {} link_definitions_to_render.each do |name, defn| json[name] = evaluate(defn.value_proc, object) end { links: json } else {} end end private def link_definitions_to_render @link_definitions_to_render ||= link_definitions.select { |_, defn| render?(defn, object) } end end |
#object ⇒ Object (readonly)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/simple_jsonapi/node/object_links.rb', line 8 class ObjectLinks < Base attr_reader :object, :link_definitions # @param object [Object] # @param link_definitions [Hash{Symbol => Definition::Link}] # @param options see {Node::Base#initialize} for additional parameters def initialize(object:, link_definitions:, **) super() @object = object @link_definitions = link_definitions end # @return [Hash{Symbol => Hash}] def as_jsonapi if link_definitions_to_render.any? json = {} link_definitions_to_render.each do |name, defn| json[name] = evaluate(defn.value_proc, object) end { links: json } else {} end end private def link_definitions_to_render @link_definitions_to_render ||= link_definitions.select { |_, defn| render?(defn, object) } end end |
Instance Method Details
#as_jsonapi ⇒ Hash{Symbol => Hash}
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/simple_jsonapi/node/object_links.rb', line 22 def as_jsonapi if link_definitions_to_render.any? json = {} link_definitions_to_render.each do |name, defn| json[name] = evaluate(defn.value_proc, object) end { links: json } else {} end end |