Class: Conformist::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/conformist/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *sources, &preprocessor) ⇒ Column

Returns a new instance of Column.



5
6
7
8
9
10
# File 'lib/conformist/column.rb', line 5

def initialize name, *sources, &preprocessor
  self.name         = name
  self.sources      = sources
  self.indexes      = sources
  self.preprocessor = preprocessor
end

Instance Attribute Details

#indexesObject

Returns the value of attribute indexes.



3
4
5
# File 'lib/conformist/column.rb', line 3

def indexes
  @indexes
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/conformist/column.rb', line 3

def name
  @name
end

#preprocessorObject

Returns the value of attribute preprocessor.



3
4
5
# File 'lib/conformist/column.rb', line 3

def preprocessor
  @preprocessor
end

#sourcesObject

Returns the value of attribute sources.



3
4
5
# File 'lib/conformist/column.rb', line 3

def sources
  @sources
end

Instance Method Details

#calculate_indices!(headers) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/conformist/column.rb', line 12

def calculate_indices!(headers)
  headers = Array(headers).collect {|header| header.to_s.downcase.squeeze(' ').strip }

  self.indexes = sources.collect do |source|
    if source.is_a?(String)
      headers.index(source.downcase)
    else
      source
    end
  end
end

#values_in(enumerable, context = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/conformist/column.rb', line 24

def values_in enumerable, context = nil
  enumerable = Array(enumerable)

  values = Array(indexes).map do |index|
    value = enumerable.at(index) if index

    if value.respond_to? :strip
      value.strip
    else
      value
    end
  end
  values = values.first if values.size == 1
  if preprocessor
    if preprocessor.arity == 1
      preprocessor.call values
    else
      preprocessor.call values, context
    end
  else
    values
  end
end