Class: SimpleJsonapi::Node::ErrorSource

Inherits:
Base
  • Object
show all
Defined in:
lib/simple_jsonapi/node/error_source.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#extras, #fields_spec, #include_spec, #root_node, #serializer, #serializer_inferrer, #sort_spec

Instance Method Summary collapse

Constructor Details

#initialize(error:, source_definition:, **options) ⇒ ErrorSource

Returns a new instance of ErrorSource.

Parameters:



12
13
14
15
16
17
# File 'lib/simple_jsonapi/node/error_source.rb', line 12

def initialize(error:, source_definition:, **options)
  super(options)

  @error = error
  @source_definition = source_definition
end

Instance Attribute Details

#errorObject (readonly)

Returns:

  • (Object)


6
7
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/error_source.rb', line 6

class ErrorSource < Base
  attr_reader :error, :source_definition

  # @param error [Object]
  # @param source_definition [Definition::ErrorSource]
  # @param options see {Node::Base#initialize} for additional parameters
  def initialize(error:, source_definition:, **options)
    super(options)

    @error = error
    @source_definition = source_definition
  end

  # @return [Hash{Symbol => Hash}]
  def as_jsonapi
    source_json = {}

    member_definitions_to_render.each do |name, defn|
      source_json[name] = evaluate(defn.value_proc, error).to_s
    end

    if source_json.any?
      { source: source_json }
    else
      {}
    end
  end

  private

  def member_definitions_to_render
    @member_definitions_to_render ||= source_definition.member_definitions.select { |_, defn| render?(defn, error) }
  end
end

#source_definitionDefinition::ErrorSource (readonly)



6
7
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/error_source.rb', line 6

class ErrorSource < Base
  attr_reader :error, :source_definition

  # @param error [Object]
  # @param source_definition [Definition::ErrorSource]
  # @param options see {Node::Base#initialize} for additional parameters
  def initialize(error:, source_definition:, **options)
    super(options)

    @error = error
    @source_definition = source_definition
  end

  # @return [Hash{Symbol => Hash}]
  def as_jsonapi
    source_json = {}

    member_definitions_to_render.each do |name, defn|
      source_json[name] = evaluate(defn.value_proc, error).to_s
    end

    if source_json.any?
      { source: source_json }
    else
      {}
    end
  end

  private

  def member_definitions_to_render
    @member_definitions_to_render ||= source_definition.member_definitions.select { |_, defn| render?(defn, error) }
  end
end

Instance Method Details

#as_jsonapiHash{Symbol => Hash}

Returns:

  • (Hash{Symbol => Hash})


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple_jsonapi/node/error_source.rb', line 20

def as_jsonapi
  source_json = {}

  member_definitions_to_render.each do |name, defn|
    source_json[name] = evaluate(defn.value_proc, error).to_s
  end

  if source_json.any?
    { source: source_json }
  else
    {}
  end
end