Class: MethodMapper
Direct Known Subclasses
Constant Summary collapse
- @@has_many =
Hash.new
- @@belongs_to =
Hash.new
- @@assignments =
Hash.new
- @@column_types =
Hash.new
Instance Attribute Summary collapse
-
#header_row ⇒ Object
Returns the value of attribute header_row.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#methods ⇒ Object
Returns the value of attribute methods.
Class Method Summary collapse
- .assignments ⇒ Object
- .assignments_for(klass) ⇒ Object
- .clear ⇒ Object
- .column_key(klass, column) ⇒ Object
- .column_type_for(klass, column) ⇒ Object
- .column_types ⇒ Object
-
.find_method_detail(klass, name) ⇒ Object
Find the proper format of name, appropriate call + column type for a given name.
-
.find_operators(klass, options = {}) ⇒ Object
Create picture of the operators for assignment available on an AR model, including via associations (which provide both << and = ).
- .has_many ⇒ Object
- .has_many_for(klass) ⇒ Object
Instance Method Summary collapse
- #check_mandatory(mandatory_list) ⇒ Object
-
#find_method_details(klass, method_list) ⇒ Object
Build complete picture of the methods whose names listed in method_list Handles method names as defined by a user or in file headers where names may not be exactly as required e.g handles capitalisation, white space, _ etc.
- #find_or_new(klass, condition_hash = {}) ⇒ Object
-
#initialize ⇒ MethodMapper
constructor
A new instance of MethodMapper.
- #method_names ⇒ Object
Constructor Details
#initialize ⇒ MethodMapper
Returns a new instance of MethodMapper.
30 31 32 33 |
# File 'lib/engine/method_mapper.rb', line 30 def initialize @methods = [] @headers = [] end |
Instance Attribute Details
#header_row ⇒ Object
Returns the value of attribute header_row.
22 23 24 |
# File 'lib/engine/method_mapper.rb', line 22 def header_row @header_row end |
#headers ⇒ Object
Returns the value of attribute headers.
22 23 24 |
# File 'lib/engine/method_mapper.rb', line 22 def headers @headers end |
#methods ⇒ Object
Returns the value of attribute methods.
23 24 25 |
# File 'lib/engine/method_mapper.rb', line 23 def methods @methods end |
Class Method Details
.assignments ⇒ Object
129 130 131 |
# File 'lib/engine/method_mapper.rb', line 129 def self.assignments @@assignments end |
.assignments_for(klass) ⇒ Object
139 140 141 |
# File 'lib/engine/method_mapper.rb', line 139 def self.assignments_for(klass) @@assignments[klass] end |
.clear ⇒ Object
116 117 118 119 120 |
# File 'lib/engine/method_mapper.rb', line 116 def self.clear @@has_many.clear @@assignments.clear @@column_types.clear end |
.column_key(klass, column) ⇒ Object
122 123 124 |
# File 'lib/engine/method_mapper.rb', line 122 def self.column_key(klass, column) "#{klass.name}:#{column}" end |
.column_type_for(klass, column) ⇒ Object
143 144 145 |
# File 'lib/engine/method_mapper.rb', line 143 def self.column_type_for(klass, column) @@column_types[column_key(klass, column)] end |
.column_types ⇒ Object
132 133 134 |
# File 'lib/engine/method_mapper.rb', line 132 def self.column_types @@column_types end |
.find_method_detail(klass, name) ⇒ Object
Find the proper format of name, appropriate call + column type for a given name. e.g Given users entry in spread sheet check for pluralization, missing underscores etc
If not nil returned method can be used directly in for example klass.new.send( call, .… )
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/engine/method_mapper.rb', line 90 def self.find_method_detail( klass, name ) true_name, assign, belongs_to, has_many = nil, nil, nil, nil # TODO - check out regexp to do this work better plus Inflections ?? [ name, name.gsub(' ', '_'), name.gsub(' ', ''), name.gsub(' ', '_').downcase, name.gsub(' ', '').downcase, name.gsub(' ', '_').underscore ].each do |n| has_many = (@@has_many[klass] && @@has_many[klass].include?(n)) ? n : nil belongs_to = (@@belongs_to[klass] && @@belongs_to[klass].include?(n)) ? n : nil assign = (@@assignments[klass] && @@assignments[klass].include?(n))? n + '=' : nil if(assign || has_many || belongs_to) true_name = n break end end return MethodDetail.new(klass, true_name, assign, belongs_to, has_many, @@column_types[column_key(klass, true_name)]) end |
.find_operators(klass, options = {}) ⇒ Object
Create picture of the operators for assignment available on an AR model, including via associations (which provide both << and = )
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/engine/method_mapper.rb', line 56 def self.find_operators(klass, = {} ) if( [:reload] || @@has_many[klass].nil? ) @@has_many[klass] = klass.reflect_on_all_associations(:has_many).map { |i| i.name.to_s } klass.reflect_on_all_associations(:has_and_belongs_to_many).inject(@@has_many[klass]) { |x,i| x << i.name.to_s } end # puts "DEBUG: Has Many Associations:", @@has_many[klass].inspect if( [:reload] || @@belongs_to[klass].nil? ) @@belongs_to[klass] = klass.reflect_on_all_associations(:belongs_to).map { |i| i.name.to_s } end # puts "DEBUG: Belongs To Associations:", @@belongs_to[klass].inspect if( [:reload] || @@assignments[klass].nil? ) @@assignments[klass] = (klass.column_names + klass.instance_methods.grep(/=/).map{|i| i.gsub(/=/, '')}) @@assignments[klass] = @@assignments[klass] - @@has_many[klass] if(@@has_many[klass]) @@assignments[klass] = @@assignments[klass] - @@belongs_to[klass] if(@@belongs_to[klass]) @@assignments[klass].uniq! @@assignments[klass].each do |assign| found = klass.columns.find{ |col| col.name == assign } @@column_types[column_key(klass, assign)] = found if found end end end |
.has_many ⇒ Object
126 127 128 |
# File 'lib/engine/method_mapper.rb', line 126 def self.has_many @@has_many end |
.has_many_for(klass) ⇒ Object
136 137 138 |
# File 'lib/engine/method_mapper.rb', line 136 def self.has_many_for(klass) @@has_many[klass] end |
Instance Method Details
#check_mandatory(mandatory_list) ⇒ Object
47 48 49 50 51 |
# File 'lib/engine/method_mapper.rb', line 47 def check_mandatory( mandatory_list ) method_list = method_names() mandatory_list.each { |x| raise "Mandatory column missing - need a '#{x}' column" unless(method_list.index(x)) } end |
#find_method_details(klass, method_list) ⇒ Object
Build complete picture of the methods whose names listed in method_list Handles method names as defined by a user or in file headers where names may not be exactly as required e.g handles capitalisation, white space, _ etc
39 40 41 |
# File 'lib/engine/method_mapper.rb', line 39 def find_method_details( klass, method_list ) @methods = method_list.collect { |x| MethodMapper::find_method_detail( klass, x ) } end |
#find_or_new(klass, condition_hash = {}) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/engine/method_mapper.rb', line 148 def find_or_new( klass, condition_hash = {} ) @records[klass] = klass.find(:all, :conditions => condition_hash) if @records[klass].any? return @records[klass].first else return klass.new end end |
#method_names ⇒ Object
43 44 45 |
# File 'lib/engine/method_mapper.rb', line 43 def method_names() @methods.collect( &:name ) end |