-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors Report row handling, fixes a couple of bugs
* Accessing report rows via #column_1 etc will be deprecated, please use the column title (e.g `report.rows.first.account`) or an array/hash-style accessor (e.g `report.rows.first[0]` or `report.rows.first["Account"]` * Fixed a bug where we weren't including all Sections in the report object * Added the ability to get the section name for a Row by calling #section_name on the Row
- Loading branch information
Showing
6 changed files
with
147 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module XeroGateway | ||
class Report | ||
class Row | ||
COLUMN_METHOD_NAME_RE = /^column\_([0-9])+$/ | ||
|
||
attr_accessor :section_name | ||
|
||
def initialize(column_titles, columns, section_name = nil) | ||
@columns = columns | ||
@column_titles = column_titles | ||
@column_titles_underscored = column_titles.map(&:to_s).map(&:underscore) | ||
@section_name = section_name | ||
end | ||
|
||
def [](key) | ||
return @columns[key] if key.is_a?(Integer) | ||
|
||
[ @column_titles, @column_titles_underscored ].each do |names| | ||
if index = names.index(key.to_s) | ||
return @columns[index] | ||
end | ||
end | ||
|
||
nil | ||
end | ||
|
||
def method_missing(method_name, *args, &block) | ||
if method_name =~ COLUMN_METHOD_NAME_RE | ||
# support column_#{n} style deprecated API | ||
ActiveSupport::Deprecation.warn("XeroGateway: The #column_n API for accessing report cells will be deprecated in a future version. Please use the underscored column title, a hash or array index accessor", caller_locations) | ||
@columns[$1.to_i - 1] | ||
elsif (column_index = @column_titles_underscored.index(method_name.to_s)) | ||
@columns[column_index] | ||
else | ||
super | ||
end | ||
end | ||
|
||
def respond_to_missing?(method_name, *args) | ||
(method_name =~ COLUMN_METHOD_NAME_RE) || @column_titles_underscored.include?(method_name.to_s) || super | ||
end | ||
|
||
def inspect | ||
"<XeroGateway::Report::Row:#{object_id} #{pairs}>" | ||
end | ||
|
||
private | ||
|
||
def pairs | ||
@column_titles.zip(@columns).map do |title, value| | ||
"#{title.to_s.underscore}: #{value.inspect}" | ||
end.join(" ") | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require_relative '../../test_helper.rb' | ||
|
||
class ReportRowTest < Test::Unit::TestCase | ||
|
||
context "with a sample row" do | ||
setup do | ||
@row = XeroGateway::Report::Row.new( | ||
["Account", "Debit", "Credit"], | ||
["Sales (200)", 560_00, 0], | ||
"Bank Accounts" | ||
) | ||
end | ||
|
||
should "be able to access using the deprecated column_n API" do | ||
ActiveSupport::Deprecation.silence do | ||
assert_equal @row.column_1, "Sales (200)" | ||
assert_equal @row.column_3, 0 | ||
|
||
assert @row.respond_to?(:column_1) | ||
end | ||
end | ||
|
||
should "be able to access using an underscored column name" do | ||
assert_equal @row.account, "Sales (200)" | ||
assert @row.respond_to?(:account) | ||
end | ||
|
||
should "be able to access using an array index" do | ||
assert_equal @row[0], "Sales (200)" | ||
assert_equal @row[1], 560_00 | ||
end | ||
|
||
should "be able to access using a string index" do | ||
assert_equal @row["Account"], "Sales (200)" | ||
assert_equal @row["Debit"], 560_00 | ||
end | ||
|
||
should "be able to access using a symbol index" do | ||
assert_equal @row[:account], "Sales (200)" | ||
assert_equal @row[:debit], 560_00 | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters