Class: Exrt::Cli::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/exrt/cli/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(d:, t:) ⇒ Renderer

Returns a new instance of Renderer.



12
13
14
15
16
# File 'lib/exrt/cli/renderer.rb', line 12

def initialize(d:, t:)
  @data = d
  @type = t
  @table = TTY::Table.new header, rows
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/exrt/cli/renderer.rb', line 10

def data
  @data
end

#tableObject (readonly)

Returns the value of attribute table.



10
11
12
# File 'lib/exrt/cli/renderer.rb', line 10

def table
  @table
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/exrt/cli/renderer.rb', line 10

def type
  @type
end

Instance Method Details

#headerObject



26
27
28
29
30
31
32
33
34
# File 'lib/exrt/cli/renderer.rb', line 26

def header
  if is_latest?
    latest_table_header
  elsif is_history?
    history_table_header
  else
    []
  end
end

#history_table_headerObject



50
51
52
# File 'lib/exrt/cli/renderer.rb', line 50

def history_table_header
  %w[Currency Rate Date]
end

#is_history?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/exrt/cli/renderer.rb', line 22

def is_history?
  type == Exrt::Cli::HISTORY
end

#is_latest?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/exrt/cli/renderer.rb', line 18

def is_latest?
  type == Exrt::Cli::LATEST
end

#latest_table_headerObject



46
47
48
# File 'lib/exrt/cli/renderer.rb', line 46

def latest_table_header
  %w[Currency Rate]
end

#renderObject



54
55
56
# File 'lib/exrt/cli/renderer.rb', line 54

def render
  table.render(:ascii)
end

#rowsObject



36
37
38
39
40
41
42
43
44
# File 'lib/exrt/cli/renderer.rb', line 36

def rows
  if is_latest?
    Exrt::Cli::Format.latest(data)
  elsif is_history?
    Exrt::Cli::Format.history(data)
  else
    []
  end
end