Skip to content

Commit

Permalink
[base] Add pos accessor, and attribute_values and changed? methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquena committed May 20, 2012
1 parent b8f0bca commit ba02fbf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
29 changes: 21 additions & 8 deletions lib/sheet_mapper/base.rb
@@ -1,7 +1,9 @@
module SheetMapper
class Base

# SheetMapper::Base.new(0, ["foo", "bar"])
attr_reader :pos

# SheetMapper::Base.new(1, ["foo", "bar"])
def initialize(pos, data=[])
@pos = pos
@data = data
Expand Down Expand Up @@ -39,6 +41,18 @@ def valid_row?
true
end

# Returns an array of data values based on the order of the spreadsheet
# @record.to_a => ["Foo", "Bar", 15]
def attribute_values
self.column_order.inject([]) { |res, name| res << @attrs[name]; res }
end

# Returns true if a record has been modified from original state
# @record.changed? => true
def changed?
@data != self.attribute_values
end

protected

# column_order => [:offset_seconds, :body, :link_url, :category]
Expand All @@ -60,16 +74,15 @@ def log(text, newline=true)
# Process all columns into an attribute hash
def process_data
m = HashWithIndifferentAccess.new
column_order.each { |name| m[name.to_s] = self.attribute_value(name) }
column_order.each { |name| m[name.to_s] = self.fetch_data_value(name) }
m
end

# attribute_value(:body, 1, 1) => "Foo"
# attribute_value(:image_url, 1, 3) => nil
# attribute_value(:link_text, 2) => "Article"
# Create a method "format_<name>" to transform the column value (or pass the value directly)
# Column position defaults to matching named column in `column_order`
def attribute_value(name)
# fetch_data_value(:body) => "Foo"
# fetch_data_value(:image_url) => nil
# fetch_data_value(:link_text) => "Article"
# Column position is found by matching named column in `column_order`
def fetch_data_value(name)
val = @data[column_pos(name)]
val = val.to_i if val && name.to_s =~ /_(id|num)/
val
Expand Down
34 changes: 33 additions & 1 deletion test/base_test.rb
Expand Up @@ -17,10 +17,15 @@ def name
@data = ["Bob", 21, "Red"]
end

should "support columns accessor" do
should "support columns class accessor" do
assert_equal [:name, :age, :color], TestBase.columns
end

should "support pos instances accessor" do
@test = TestBase.new(1, @data)
assert_equal 1, @test.pos
end

context "for attributes method" do
setup do
@test = TestBase.new(1, @data)
Expand Down Expand Up @@ -53,4 +58,31 @@ def name
assert_equal 45, @test[:age]
end
end # assign []=

context "for attribute_values method" do
setup do
@test = TestBase.new(1, @data)
@test[:age] = 45
end

should "return attribute values in proper order as list" do
assert_equal ["Bob", 45, "Red"], @test.attribute_values
end
end # attribute_values

context "for changed? method" do
setup do
@test = TestBase.new(1, @data)
end

should "return false if not changed" do
@test[:age] = 21
assert_equal false, @test.changed?
end

should "return true if changed" do
@test[:age] = 45
assert_equal true, @test.changed?
end
end # attribute_values
end # Base

0 comments on commit ba02fbf

Please sign in to comment.