Class: Tabledata::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/tabledata/presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, multiple_capable, options) ⇒ Presenter


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tabledata/presenter.rb', line 42

def initialize(table, multiple_capable, options)
  if table.is_a?(Tables)
    @tables = table
    @table  = nil
  else
    @tables = Tables.new(nil => table)
    @table  = table
  end
  raise "Not capable to process multiple tables" if multiple_tables? && !multiple_capable
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


40
41
42
# File 'lib/tabledata/presenter.rb', line 40

def options
  @options
end

#tableObject (readonly)

Returns the value of attribute table


40
41
42
# File 'lib/tabledata/presenter.rb', line 40

def table
  @table
end

#tablesObject (readonly)

Returns the value of attribute tables


40
41
42
# File 'lib/tabledata/presenter.rb', line 40

def tables
  @tables
end

Class Method Details

.add_presenter!(name, *args) ⇒ Object


21
22
23
24
# File 'lib/tabledata/presenter.rb', line 21

def self.add_presenter!(name, *args)
  raise "Presenter #{name.inspect} already exists" if presenter_exists?(name)
  @presenters[name] = args
end

.present(table, format, options) ⇒ Object

Raises:

  • (ArgumentError)

31
32
33
34
35
36
37
38
# File 'lib/tabledata/presenter.rb', line 31

def self.present(table, format, options)
  code, constant, multitable_capable, default_options = *@presenters[format]
  raise ArgumentError, "Unknown format #{format.inspect}" unless code
  require code
  klass = constant.inject(Object) { |source, current| source.const_get(current) }

  klass.new(table, multitable_capable, options ? default_options.merge(options) : default_options.dup)
end

.presenter_exists?(name) ⇒ Boolean


17
18
19
# File 'lib/tabledata/presenter.rb', line 17

def self.presenter_exists?(name)
  @presenters.has_key?(name)
end

.replace_presenter!(name, *args) ⇒ Object


26
27
28
29
# File 'lib/tabledata/presenter.rb', line 26

def self.replace_presenter!(name, *args)
  raise "Presenter #{name.inspect} does not exist" unless presenter_exists?(name)
  @presenters[name] = args
end

Instance Method Details

#multiple_tables?Boolean


58
59
60
# File 'lib/tabledata/presenter.rb', line 58

def multiple_tables?
  @table ? false : true
end

#single_table?Boolean


54
55
56
# File 'lib/tabledata/presenter.rb', line 54

def single_table?
  @table ? true : false
end

#write(path, options = nil) ⇒ Object


62
63
64
# File 'lib/tabledata/presenter.rb', line 62

def write(path, options=nil)
  File.write(path, string(options), encoding: 'utf-8')
end