Class: MotionCSV::Row

Inherits:
Array
  • Object
show all
Defined in:
lib/motion-csv/motion-csv.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#depth, #to_csv

Constructor Details

#initialize(table, array, line = -1)) ⇒ Row



133
134
135
136
137
# File 'lib/motion-csv/motion-csv.rb', line 133

def initialize(table, array, line=-1)
  @table = table
  @line = line
  super(array)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



198
199
200
# File 'lib/motion-csv/motion-csv.rb', line 198

def method_missing(method, *args, &block)
  to_hash.send(method, *args, &block)
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line.



131
132
133
# File 'lib/motion-csv/motion-csv.rb', line 131

def line
  @line
end

Class Method Details

.to_key(key) ⇒ Object



121
122
123
124
# File 'lib/motion-csv/motion-csv.rb', line 121

def to_key(key)
  key = "#{key}".downcase.gsub(/\s+/, '_')
  key.empty? ? :_ : key.to_sym
end

Instance Method Details

#[](*is) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/motion-csv/motion-csv.rb', line 139

def [](*is)
  is.each do |i|
    val = if i.is_a? Fixnum
      super
    else
      found = headers.index(Row::to_key(i))
      found ? super(found) : nil
    end
    return val unless val.nil?
  end
  nil
end

#[]=(key, val) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/motion-csv/motion-csv.rb', line 152

def []=(key, val)
  if key.is_a? Fixnum
    super
  else
    key = Row::to_key(key)
    headers << key unless headers.include? key
    found = headers.index(key)
    super(found, val)
  end
end

#headersObject Also known as: keys



127
128
129
# File 'lib/motion-csv/motion-csv.rb', line 127

def headers
  @headers ||= @table.headers.dup
end

#key?(key) ⇒ Boolean Also known as: has_key?, member?, include?



190
191
192
# File 'lib/motion-csv/motion-csv.rb', line 190

def key?(key)
  keys.include?(Row.to_key(key))
end

#merge(row) ⇒ Object Also known as: merge!



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/motion-csv/motion-csv.rb', line 170

def merge(row)
  if row.is_a? Row
    row.headers.each do |header|
      self[header] = row[header]
    end
  else
    row.each do |key, value|
      self[key] = value
    end
  end
  self
end

#pull(*columns) ⇒ Object



163
164
165
166
167
168
# File 'lib/motion-csv/motion-csv.rb', line 163

def pull(*columns)
  columns.map do |column|
    column = [nil] if column.nil?
    self[*column]
  end
end

#to_hashObject



183
184
185
186
187
188
# File 'lib/motion-csv/motion-csv.rb', line 183

def to_hash
  headers.inject({}) do |memo, h|
    memo[h] = self[h]
    memo
  end
end

#value?(value) ⇒ Boolean Also known as: has_value?



194
195
196
# File 'lib/motion-csv/motion-csv.rb', line 194

def value?(value)
  values.include?(value)
end