Class: OutputMode::Output Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/output_mode/output.rb

Overview

This class is abstract.

Defines the public interface to all subclasses

Base outputting class that wraps an array of procs or other callable object. Each implementation must override the #render method so it returns an array.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*procs, default: nil, yes: 'true', no: 'false', context: {}, **config) ⇒ Output

Creates a new outputting instance from an array of procs

Parameters:

  • *procs (Array<#call>) —

    an array of procs (or callable objects)

  • default: (String) (defaults to: nil) —

    replaces blanks with a static string

  • yes: (String) (defaults to: 'true') —

    replaces true with a static string

  • no: (String) (defaults to: 'false') —

    replaces false with a static string

  • context: (Hash) (defaults to: {}) —

    of keys to be provided to the callables

  • **config (Hash) —

    a hash of additional keys to be stored



56
57
58
59
60
61
62
63
# File 'lib/output_mode/output.rb', line 56

def initialize(*procs, default: nil, yes: 'true', no: 'false', context: {}, **config)
  @procs = Callables.new(procs)
  @config = config
  @yes = yes
  @no = no
  @default = default
  @context = context
end

Instance Attribute Details

#config ⇒ Hash (readonly)

Returns additional key-values to modify the render.

Returns:

  • (Hash) —

    additional key-values to modify the render



46
# File 'lib/output_mode/output.rb', line 46

attr_reader :procs, :config, :yes, :no, :default, :context

#context ⇒ Object (readonly)

Returns the value of attribute context.



46
# File 'lib/output_mode/output.rb', line 46

attr_reader :procs, :config, :yes, :no, :default, :context

#default ⇒ Object (readonly)

Returns either a static default.

Returns:

  • either a static default



46
# File 'lib/output_mode/output.rb', line 46

attr_reader :procs, :config, :yes, :no, :default, :context

#no ⇒ Object (readonly)

Returns either a static no value.

Returns:

  • either a static no value



46
# File 'lib/output_mode/output.rb', line 46

attr_reader :procs, :config, :yes, :no, :default, :context

#procs ⇒ Array<#call> (readonly)

Returns the callable methods to generate output.

Returns:

  • (Array<#call>) —

    the callable methods to generate output



46
47
48
# File 'lib/output_mode/output.rb', line 46

def procs
  @procs
end

#yes ⇒ Object (readonly)

Returns either a static yes value.

Returns:

  • either a static yes value



46
# File 'lib/output_mode/output.rb', line 46

attr_reader :procs, :config, :yes, :no, :default, :context

Instance Method Details

#callables ⇒ Object



65
66
67
# File 'lib/output_mode/output.rb', line 65

def callables
  procs
end

#generate(object) ⇒ Object

Returns the results of the procs for a particular object. It will apply the default, yes, and no values.



71
72
73
74
75
# File 'lib/output_mode/output.rb', line 71

def generate(object)
  procs.map do |callable|
    callable.generator(self).call(object)
  end
end

#render(*data) ⇒ String

This method is abstract.

It should be implemented by the subclass using the generate method

Renders the results of the procs into a string. Each data objects should be passed individual to each proc to generate the final output.

The method must be overridden on all inherited classes

Parameters:

  • *data (Array) —

    a set of data to be rendered into the output

Returns:

  • (String) —

    the output string

Raises:

  • (NotImplementedError)

See Also:



87
88
89
# File 'lib/output_mode/output.rb', line 87

def render(*data)
  raise NotImplementedError
end