Class: ActsAsTable::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_table/writer.rb

Overview

ActsAsTable writer object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row_model, output = $stdout, **options) {|writer| ... } ⇒ ActsAsTable::Writer

Returns a new ActsAsTable writer object using the given ActsAsTable row model, output stream and options.

Parameters:

  • (defaults to: $stdout)

Yield Parameters:

Yield Returns:

  • (void)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/acts_as_table/writer.rb', line 39

def initialize(row_model, output = $stdout, **options, &block)
  @row_model, @output, @options = row_model, output, options.dup

  if block_given?
    self.write_prologue

    case block.arity
      when 1 then block.call(self)
      else self.instance_eval(&block)
    end

    self.write_epilogue
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) {|*args, &block| ... } ⇒ Object

Delegates to the output stream for this ActsAsTable writer object.

Parameters:

Yields:

  • (*args, &block)

Yield Returns:

  • (Object)

Returns:

Raises:



62
63
64
# File 'lib/acts_as_table/writer.rb', line 62

def method_missing(method_name, *args, &block)
  @output.respond_to?(method_name, false) ? @output.send(method_name, *args, &block) : super(method_name, *args, &block)
end

Instance Attribute Details

#optionsHash<Symbol, Object> (readonly)

Returns the options for this ActsAsTable reader object.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/acts_as_table/writer.rb', line 16

class Writer
  # Returns a new ActsAsTable writer object based on a symbolic name using the given arguments.
  #
  # @param [Symbol] format
  # @param [Array<Object>] args
  # @yieldparam [ActsAsTable::Writer] writer
  # @yieldreturn [void]
  # @return [ActsAsTable::Writer]
  # @raise [ArgumentError] If the given symbolic name is invalid.
  def self.for(format, *args, &block)
    ActsAsTable.for(format).writer(*args, &block)
  end

  attr_reader :row_model, :output, :options

  # Returns a new ActsAsTable writer object using the given ActsAsTable row model, output stream and options.
  #
  # @param [ActsAsTable::RowModel] row_model
  # @param [IO] output
  # @param [Hash<Symbol, Object>] options
  # @yieldparam [ActsAsTable::Writer] writer
  # @yieldreturn [void]
  # @return [ActsAsTable::Writer]
  def initialize(row_model, output = $stdout, **options, &block)
    @row_model, @output, @options = row_model, output, options.dup

    if block_given?
      self.write_prologue

      case block.arity
        when 1 then block.call(self)
        else self.instance_eval(&block)
      end

      self.write_epilogue
    end
  end

  # Delegates to the output stream for this ActsAsTable writer object.
  #
  # @param [String] method_name
  # @param [Array<Object>] args
  # @yield [*args, &block]
  # @yieldreturn [Object]
  # @return [Object]
  # @raise [NoMethodError]
  def method_missing(method_name, *args, &block)
    @output.respond_to?(method_name, false) ? @output.send(method_name, *args, &block) : super(method_name, *args, &block)
  end

  # Delegates to the output stream for this ActsAsTable writer object.
  #
  # @param [String] method_name
  # @param [Boolean] include_all
  # @return [Boolean]
  def respond_to?(method_name, include_all = false)
    @output.respond_to?(method_name, false) || super(method_name, include_all)
  end

  # Writes the epilogue to the output stream.
  #
  # @return [ActsAsTable::Writer]
  def write_epilogue
    self
  end

  # Writes the prologue to the output stream.
  #
  # @return [ActsAsTable::Writer]
  def write_prologue
    # @return [ActsAsTable::Headers::Array]
    headers = @row_model.to_headers

    headers.each do |header|
      self.write_row(header)
    end

    self
  end

  # Writes a record to the output stream.
  #
  # @param [ActiveRecord::Base] base
  # @return [ActsAsTable::Writer]
  # @raise [ArgumentError] If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.
  def write_base(base)
    row = @row_model.to_row(base)

    self.write_row(row)
  end
  alias_method :<<, :write_base

  # Writes a row to the output stream.
  #
  # @param [Array<String, nil>, nil] row
  # @return [ActsAsTable::Writer]
  def write_row(row)
    raise ::NotImplementedError.new("#{self.class}#write_row")
  end
end

#outputIO (readonly)

Returns the output stream for this ActsAsTable reader object.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/acts_as_table/writer.rb', line 16

class Writer
  # Returns a new ActsAsTable writer object based on a symbolic name using the given arguments.
  #
  # @param [Symbol] format
  # @param [Array<Object>] args
  # @yieldparam [ActsAsTable::Writer] writer
  # @yieldreturn [void]
  # @return [ActsAsTable::Writer]
  # @raise [ArgumentError] If the given symbolic name is invalid.
  def self.for(format, *args, &block)
    ActsAsTable.for(format).writer(*args, &block)
  end

  attr_reader :row_model, :output, :options

  # Returns a new ActsAsTable writer object using the given ActsAsTable row model, output stream and options.
  #
  # @param [ActsAsTable::RowModel] row_model
  # @param [IO] output
  # @param [Hash<Symbol, Object>] options
  # @yieldparam [ActsAsTable::Writer] writer
  # @yieldreturn [void]
  # @return [ActsAsTable::Writer]
  def initialize(row_model, output = $stdout, **options, &block)
    @row_model, @output, @options = row_model, output, options.dup

    if block_given?
      self.write_prologue

      case block.arity
        when 1 then block.call(self)
        else self.instance_eval(&block)
      end

      self.write_epilogue
    end
  end

  # Delegates to the output stream for this ActsAsTable writer object.
  #
  # @param [String] method_name
  # @param [Array<Object>] args
  # @yield [*args, &block]
  # @yieldreturn [Object]
  # @return [Object]
  # @raise [NoMethodError]
  def method_missing(method_name, *args, &block)
    @output.respond_to?(method_name, false) ? @output.send(method_name, *args, &block) : super(method_name, *args, &block)
  end

  # Delegates to the output stream for this ActsAsTable writer object.
  #
  # @param [String] method_name
  # @param [Boolean] include_all
  # @return [Boolean]
  def respond_to?(method_name, include_all = false)
    @output.respond_to?(method_name, false) || super(method_name, include_all)
  end

  # Writes the epilogue to the output stream.
  #
  # @return [ActsAsTable::Writer]
  def write_epilogue
    self
  end

  # Writes the prologue to the output stream.
  #
  # @return [ActsAsTable::Writer]
  def write_prologue
    # @return [ActsAsTable::Headers::Array]
    headers = @row_model.to_headers

    headers.each do |header|
      self.write_row(header)
    end

    self
  end

  # Writes a record to the output stream.
  #
  # @param [ActiveRecord::Base] base
  # @return [ActsAsTable::Writer]
  # @raise [ArgumentError] If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.
  def write_base(base)
    row = @row_model.to_row(base)

    self.write_row(row)
  end
  alias_method :<<, :write_base

  # Writes a row to the output stream.
  #
  # @param [Array<String, nil>, nil] row
  # @return [ActsAsTable::Writer]
  def write_row(row)
    raise ::NotImplementedError.new("#{self.class}#write_row")
  end
end

#row_modelActsAsTable::RowModel (readonly)

Returns the ActsAsTable row model for this ActsAsTable reader object.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/acts_as_table/writer.rb', line 16

class Writer
  # Returns a new ActsAsTable writer object based on a symbolic name using the given arguments.
  #
  # @param [Symbol] format
  # @param [Array<Object>] args
  # @yieldparam [ActsAsTable::Writer] writer
  # @yieldreturn [void]
  # @return [ActsAsTable::Writer]
  # @raise [ArgumentError] If the given symbolic name is invalid.
  def self.for(format, *args, &block)
    ActsAsTable.for(format).writer(*args, &block)
  end

  attr_reader :row_model, :output, :options

  # Returns a new ActsAsTable writer object using the given ActsAsTable row model, output stream and options.
  #
  # @param [ActsAsTable::RowModel] row_model
  # @param [IO] output
  # @param [Hash<Symbol, Object>] options
  # @yieldparam [ActsAsTable::Writer] writer
  # @yieldreturn [void]
  # @return [ActsAsTable::Writer]
  def initialize(row_model, output = $stdout, **options, &block)
    @row_model, @output, @options = row_model, output, options.dup

    if block_given?
      self.write_prologue

      case block.arity
        when 1 then block.call(self)
        else self.instance_eval(&block)
      end

      self.write_epilogue
    end
  end

  # Delegates to the output stream for this ActsAsTable writer object.
  #
  # @param [String] method_name
  # @param [Array<Object>] args
  # @yield [*args, &block]
  # @yieldreturn [Object]
  # @return [Object]
  # @raise [NoMethodError]
  def method_missing(method_name, *args, &block)
    @output.respond_to?(method_name, false) ? @output.send(method_name, *args, &block) : super(method_name, *args, &block)
  end

  # Delegates to the output stream for this ActsAsTable writer object.
  #
  # @param [String] method_name
  # @param [Boolean] include_all
  # @return [Boolean]
  def respond_to?(method_name, include_all = false)
    @output.respond_to?(method_name, false) || super(method_name, include_all)
  end

  # Writes the epilogue to the output stream.
  #
  # @return [ActsAsTable::Writer]
  def write_epilogue
    self
  end

  # Writes the prologue to the output stream.
  #
  # @return [ActsAsTable::Writer]
  def write_prologue
    # @return [ActsAsTable::Headers::Array]
    headers = @row_model.to_headers

    headers.each do |header|
      self.write_row(header)
    end

    self
  end

  # Writes a record to the output stream.
  #
  # @param [ActiveRecord::Base] base
  # @return [ActsAsTable::Writer]
  # @raise [ArgumentError] If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.
  def write_base(base)
    row = @row_model.to_row(base)

    self.write_row(row)
  end
  alias_method :<<, :write_base

  # Writes a row to the output stream.
  #
  # @param [Array<String, nil>, nil] row
  # @return [ActsAsTable::Writer]
  def write_row(row)
    raise ::NotImplementedError.new("#{self.class}#write_row")
  end
end

Class Method Details

.for(format, *args) {|writer| ... } ⇒ ActsAsTable::Writer

Returns a new ActsAsTable writer object based on a symbolic name using the given arguments.

Parameters:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

Raises:

  • If the given symbolic name is invalid.



25
26
27
# File 'lib/acts_as_table/writer.rb', line 25

def self.for(format, *args, &block)
  ActsAsTable.for(format).writer(*args, &block)
end

Instance Method Details

#respond_to?(method_name, include_all = false) ⇒ Boolean

Delegates to the output stream for this ActsAsTable writer object.

Parameters:

  • (defaults to: false)

Returns:



71
72
73
# File 'lib/acts_as_table/writer.rb', line 71

def respond_to?(method_name, include_all = false)
  @output.respond_to?(method_name, false) || super(method_name, include_all)
end

#write_base(base) ⇒ ActsAsTable::Writer Also known as: <<

Writes a record to the output stream.

Parameters:

Returns:

Raises:

  • If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.



101
102
103
104
105
# File 'lib/acts_as_table/writer.rb', line 101

def write_base(base)
  row = @row_model.to_row(base)

  self.write_row(row)
end

#write_epilogueActsAsTable::Writer

Writes the epilogue to the output stream.

Returns:



78
79
80
# File 'lib/acts_as_table/writer.rb', line 78

def write_epilogue
  self
end

#write_prologueActsAsTable::Writer

Writes the prologue to the output stream.

Returns:



85
86
87
88
89
90
91
92
93
94
# File 'lib/acts_as_table/writer.rb', line 85

def write_prologue
  # @return [ActsAsTable::Headers::Array]
  headers = @row_model.to_headers

  headers.each do |header|
    self.write_row(header)
  end

  self
end

#write_row(row) ⇒ ActsAsTable::Writer

Writes a row to the output stream.

Parameters:

Returns:

Raises:



112
113
114
# File 'lib/acts_as_table/writer.rb', line 112

def write_row(row)
  raise ::NotImplementedError.new("#{self.class}#write_row")
end