Class: DataAccessorModule

Inherits:
Module
  • Object
show all
Defined in:
lib/csv_madness/data_accessor_module.rb

Overview

Each record within the spreadsheet is extended by a module with accessor functions which allow the user to treat column names as methods. When columns are added, dropped, or re-ordered, the accessor module needs to be updated to reflect the change.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ DataAccessorModule

Returns a new instance of DataAccessorModule.



9
10
11
12
13
# File 'lib/csv_madness/data_accessor_module.rb', line 9

def initialize( mapping )
  puts "Got mapping #{mapping.inspect}"
  @column_accessors_map = mapping
  remap_accessors
end

Instance Attribute Details

#column_accessors_mapObject

mapping : keys = column symbol values = column index



7
8
9
# File 'lib/csv_madness/data_accessor_module.rb', line 7

def column_accessors_map
  @column_accessors_map
end

Instance Method Details

#install_column_accessors(*columns) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/csv_madness/data_accessor_module.rb', line 15

def install_column_accessors( *columns )
  for column in columns.flatten
    unless is_valid_ruby_method?( column )
      warn( "#{column.inspect} is not a valid ruby method" )
      require "data_accessor_module"    # TODO:  WTF?
      column = "csv_column_#{index}"
      warn( "   ----> renaming to #{column.inspect}" )
    end
  
    if self.respond_to?( column )
      puts "Column already has accessors"
    else
      eval <<-EOF
          self.send( :define_method, :#{column} ) do
            self.csv_data[ self.column_accessors_map[ :#{column} ] ]
          end
    
          self.send( :define_method, :#{column}= ) do |val|
            self.csv_data[ self.column_accessors_map[ :#{column} ] ] = val
          end
      EOF
    end
  end
end

#is_valid_ruby_method?(str_or_sym) ⇒ Boolean

Symbol.inspect doesn't output " quotes when given a sym that expresses a valid method name or a valid identifier.

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/csv_madness/data_accessor_module.rb', line 73

def is_valid_ruby_method?( str_or_sym )
  sym = str_or_sym.to_sym
  test_string = sym.inspect
  
  test_string.match( /^:[@"]/ ).nil?   
end

#remap_accessors(*args) ⇒ Object

Partly obsoleted.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/csv_madness/data_accessor_module.rb', line 57

def remap_accessors( *args )
  # 
  
  
  remove_all_column_accessors
  
  if args.length == 1
    debugger
    @column_accessors_map = args.first
  end
  
  install_column_accessors( @column_accessors_map.keys )
end

#remove_all_column_accessorsObject



50
51
52
53
54
# File 'lib/csv_madness/data_accessor_module.rb', line 50

def remove_all_column_accessors
  # 2016-11-24 : was this necessary for anything
  # @column_accessors ||= []
  remove_column_accessors( @column_accessors_map.keys )
end

#remove_column_accessors(*columns) ⇒ Object

But if instead of hardcoding the index, the index was pulled from the spreadsheet mapping... a titch slower to look up? But a change to the mapping automatically updates the method



42
43
44
45
46
47
48
# File 'lib/csv_madness/data_accessor_module.rb', line 42

def remove_column_accessors( *columns )
  for column in columns.flatten
    for method_sym in [column, :"#{column}="]
      self.send( :remove_method, method_sym ) if self.respond_to?( method_sym )
    end
  end
end