Class: Dcc::Convert::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/dcc/convert/result.rb

Overview

Output of a Dcc::Convert::* converter. Carries the format and the string payload. Outputs are models per the user's directive: to_s returns the rendered payload, to_json / to_yaml return the formatted payload directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format:, payload:, source_class: nil, schema_version: nil) ⇒ Result

Returns a new instance of Result.

Parameters:

  • format (Symbol, String)

    the conversion target (:json, :yaml, etc.).

  • payload (String)

    the rendered output.

  • source_class (String, nil) (defaults to: nil)

    the source model's class name.

  • schema_version (String, nil) (defaults to: nil)

    the DCC schema version.



16
17
18
19
20
21
# File 'lib/dcc/convert/result.rb', line 16

def initialize(format:, payload:, source_class: nil, schema_version: nil)
  @format = format.to_sym
  @payload = payload.to_s
  @source_class = source_class
  @schema_version = schema_version
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



10
11
12
# File 'lib/dcc/convert/result.rb', line 10

def format
  @format
end

#payloadObject (readonly)

Returns the value of attribute payload.



10
11
12
# File 'lib/dcc/convert/result.rb', line 10

def payload
  @payload
end

#schema_versionObject (readonly)

Returns the value of attribute schema_version.



10
11
12
# File 'lib/dcc/convert/result.rb', line 10

def schema_version
  @schema_version
end

#source_classObject (readonly)

Returns the value of attribute source_class.



10
11
12
# File 'lib/dcc/convert/result.rb', line 10

def source_class
  @source_class
end

Instance Method Details

#parsedObject

Convenience: parse the payload back into Ruby data structures.

Returns:

  • (Object)


30
31
32
33
34
35
36
37
38
39
# File 'lib/dcc/convert/result.rb', line 30

def parsed
  case @format
  when :json
    ::JSON.parse(@payload)
  when :yaml
    ::YAML.safe_load(@payload)
  else
    @payload
  end
end

#to_json(*_args) ⇒ Object

Already-rendered JSON payload (not a re-render).



42
43
44
# File 'lib/dcc/convert/result.rb', line 42

def to_json(*_args)
  @payload
end

#to_sObject

to_s returns the rendered output.



24
25
26
# File 'lib/dcc/convert/result.rb', line 24

def to_s
  @payload
end

#to_yaml(*_args) ⇒ Object



46
47
48
# File 'lib/dcc/convert/result.rb', line 46

def to_yaml(*_args)
  @payload
end