Class: Lutra::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lutra/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Formatter

Returns a new instance of Formatter.



5
6
7
8
9
10
11
# File 'lib/lutra/formatter.rb', line 5

def initialize(options = {})
  @options = default_options.merge(options)
  @current = :default
  @formatters = [
    { name: :default, short: :d, class: Lutra::Formatters::Default }
  ]
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



3
4
5
# File 'lib/lutra/formatter.rb', line 3

def current
  @current
end

#formattersObject (readonly)

Returns the value of attribute formatters.



3
4
5
# File 'lib/lutra/formatter.rb', line 3

def formatters
  @formatters
end

Instance Method Details

#add(name, short_name, class_name) ⇒ Object



27
28
29
30
31
# File 'lib/lutra/formatter.rb', line 27

def add(name, short_name, class_name)
  if formatter?(class_name)
    @formatters << { name: name, short: short_name, class: class_name }
  end
end

#display(notes) ⇒ Object



33
34
35
# File 'lib/lutra/formatter.rb', line 33

def display(notes)
  get(@current)[:class].new(@options).display(notes)
end

#get(name) ⇒ Object



13
14
15
16
17
# File 'lib/lutra/formatter.rb', line 13

def get(name)
  @formatters.select do |f|
    f[:name] == name || f[:short] == name
  end.first
end

#set(name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/lutra/formatter.rb', line 19

def set(name)
  if get(name)
    @current = name
  else
    raise FormatterNotFound
  end
end