Class: Goodsheet::ReadResult

Inherits:
Object
  • Object
show all
Defined in:
lib/goodsheet/read_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row_attributes, max_errors, collector = :a_arr) ⇒ ReadResult

Returns a new instance of ReadResult.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/goodsheet/read_result.rb', line 5

def initialize(row_attributes, max_errors, collector=:a_arr)
  @row_attributes = row_attributes
  case collector
  when :hash
    @hash = {}
  when :a_arr
    @a_arr = []
  when :h_arr
    @h_arr = []
  end
  @errors = ValidationErrors.new(max_errors)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/goodsheet/read_result.rb', line 3

def errors
  @errors
end

Instance Method Details

#add(row_number, row) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/goodsheet/read_result.rb', line 19

def add(row_number, row)
  if defined? @hash
    row.to_hash.each_pair do |k, v|
      (@hash[k]||=[]) << v                  # @hash = {:a => ["dog, "cat", ...], :b => [3, 7, ...]}
    end
  end
  @a_arr << row.to_a if defined? @a_arr     # @a_arr = [["dog", 3], ["cat", 7], ...]
  @h_arr << row.to_hash if defined? @h_arr  # @h_arr = [{:a => "dog", :b => 3}, {:a => "cat", :b => 7}, ...]
  @errors.add(row_number, row)
end

#invalid?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/goodsheet/read_result.rb', line 34

def invalid?
  !valid?
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/goodsheet/read_result.rb', line 30

def valid?
  @errors.empty?
end

#values(format = :columns) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/goodsheet/read_result.rb', line 38

def values(format=:columns)
  if defined? @hash
    return [] if @hash.empty?
    values_for_hash(format)

  elsif defined? @h_arr
    return [] if @h_arr.empty?
    values_for_h_arr(format)

  elsif defined? @a_arr # @a_arr = [["dog", 3], ["cat", 7], ...]
    return [] if @a_arr.empty?
    values_for_a_arr(format)
  end
end

#values_for_a_arr(format) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/goodsheet/read_result.rb', line 76

def values_for_a_arr(format)
  case format
  when :columns
    h = {}
    @row_attributes.each_with_index do |attrib, i|
      h[attrib] = @a_arr.map{|e| e[i]}
    end
    h

  when :rows_array
    @a_arr

  when :rows_hash
    @a_arr.map do |arr|
      Hash[@row_attributes.map.with_index{|attrib,i| [attrib, arr[i]]}]
    end
  end
end

#values_for_h_arr(format) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/goodsheet/read_result.rb', line 95

def values_for_h_arr(format)
  case format
  when :columns
    keys = @h_arr.first.keys
    h = {}
    @h_arr.each do |e|
      keys.each do |key|
        (h[key] ||= []) << e[key] 
      end
    end
    h

  when :rows_array
    @h_arr.map(&:values)

  when :rows_hash
    @h_arr
  end
end

#values_for_hash(format) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/goodsheet/read_result.rb', line 53

def values_for_hash(format)
  case format
  when :columns
    @hash

  when :rows_array
    a1 = []
    @hash.values.first.size.times do |i|
      a1 << @row_attributes.collect{|a| @hash[a][i]}
    end
    a1

  when :rows_hash
    a1 = []
    @hash.values.first.size.times do |i|
      a2 = {}
      @row_attributes.each{|a| a2[a] = @hash[a][i]}
      a1 << a2
    end
    a1
  end
end