Class: DeclarativeGrid::Renderers::AbstractRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_grid/renderers/abstract_renderer.rb

Direct Known Subclasses

Csv, Html

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, options = {}) ⇒ AbstractRenderer

Constructor

PARAM: records is an object representing the records of model

it can be:
  1) Enumerator
  2) Enumerable

PARAM: options is a hash object containing optional configuration

:columns => an array of column names to render


30
31
32
33
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 30

def initialize(records, options = {})
  self.records = records
  self.options = self.class.options.merge options
end

Instance Attribute Details

#optionsObject

Attributes



17
18
19
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 17

def options
  @options
end

#recordsObject

Attributes



17
18
19
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 17

def records
  @records
end

Class Method Details

.grid_classObject

Attributes of class



6
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 6

def self.grid_class; @grid_class; end

.grid_class=(k) ⇒ Object



7
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 7

def self.grid_class=(k); @grid_class = k; end

.inherited(subclass) ⇒ Object



11
12
13
14
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 11

def self.inherited(subclass)
  super(subclass)
  subclass.options = self.options.dup
end

.optionsObject



8
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 8

def self.options; @options ||= {}; end

.options=(opt) ⇒ Object



9
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 9

def self.options=(opt); @options = opt; end

Instance Method Details

#columnsObject

Return selected columns to render



36
37
38
39
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 36

def columns
  names = options[:columns] || []
  self.class.grid_class.columns(*names)
end

#each_recordObject

Return enumerator of self.records



56
57
58
59
60
61
62
63
64
65
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 56

def each_record
  if block_given?
    case records
    when Enumerator, Enumerable then records.each{|r| yield r }
    else raise RendererError, "#{records}: invalid type"
    end
  else
    Enumerator.new self, :each_record
  end
end

#each_rowObject

Return enumerator of rows



47
48
49
50
51
52
53
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 47

def each_row
  if block_given?
    self.each_record{|r| yield row_for(r) }
  else
    Enumerator.new self, :each_row
  end
end

#headersObject

Return array of localized headers



42
43
44
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 42

def headers
  self.columns.map(&:header)
end

#inspectObject

Prevent from @records dumping



68
69
70
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 68

def inspect
  self.class.inspect
end

#perform(io) ⇒ Object

Render data to the stream

Raises:

  • (NotImplementedError)


73
74
75
76
77
78
79
80
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 73

def perform(io)
  #
  # io.write string1
  # io.puts string2
  # ...
  #
  raise NotImplementedError
end

#to_stringObject

Render to string



83
84
85
86
87
88
# File 'lib/declarative_grid/renderers/abstract_renderer.rb', line 83

def to_string
  io = StringIO.new
  self.perform(io)
  io.close
  io.string
end