Skip to content

Commit

Permalink
Tidy up Roo::Base#find, #find_by_row, #find_by_conditions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Empact committed Mar 27, 2014
1 parent e4e9044 commit 1ccedab
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/roo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,13 @@ def inspect
def find(*args) # :nodoc
options = (args.last.is_a?(Hash) ? args.pop : {})

if args[0].class == Fixnum
find_by_row(args)
elsif args[0] == :all
case args[0]
when Fixnum
find_by_row(args[0])
when :all
find_by_conditions(options)
else
raise ArgumentError, "unexpected arg #{args[0].inspect}, pass a row index or :all"
end
end

Expand Down Expand Up @@ -521,35 +524,33 @@ def key_to_string(arr)

private

def find_by_row(args)
rownum = args[0]
current_row = rownum
current_row += header_line - 1 if @header_line
def find_by_row(row_index)
row_index += (header_line - 1) if @header_line

self.row(current_row).size.times.map do |j|
cell(current_row, j + 1)
row(row_index).size.times.map do |cell_index|
cell(row_index, cell_index + 1)
end
end

def find_by_conditions(options)
rows = first_row.upto(last_row)
result_array = options[:array]
header_for = Hash[1.upto(last_column).map do |col|
[col, cell(@header_line,col)]
end]

# are all conditions met?
if (conditions = options[:conditions]) && !conditions.empty?
conditions = options[:conditions]
if conditions && !conditions.empty?
column_with = header_for.invert
rows = rows.select do |i|
conditions.all? { |key,val| cell(i,column_with[key]) == val }
end
end

rows.map do |i|
if result_array
self.row(i)
else
if options[:array]
rows.map {|i| self.row(i) }
else
rows.map do |i|
Hash[1.upto(self.row(i).size).map do |j|
[header_for.fetch(j), cell(i,j)]
end]
Expand Down

0 comments on commit 1ccedab

Please sign in to comment.