Class: DataMiner::Attribute
- Inherits:
-
Object
- Object
- DataMiner::Attribute
- Defined in:
- lib/data_miner/attribute.rb
Constant Summary collapse
- VALID_OPTIONS =
[ :from_units, :to_units, :static, :dictionary, :matcher, :field_name, :delimiter, :split, :units, :sprintf, :nullify, :overwrite, :upcase, :units_field_name, :units_field_number, :field_number, :chars, :synthesize ]
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#step ⇒ Object
Returns the value of attribute step.
Instance Method Summary collapse
- #chars ⇒ Object
- #column_type ⇒ Object
- #delimiter ⇒ Object
- #dictionary ⇒ Object
- #do_convert(row, value) ⇒ Object
- #do_split(value) ⇒ Object
- #do_sprintf(value) ⇒ Object
-
#field_name ⇒ Object
Options that always have values.
- #field_number ⇒ Object
- #from_units ⇒ Object
-
#initialize(step, name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #inspect ⇒ Object
- #match_row(row) ⇒ Object
- #matcher ⇒ Object
- #nullify ⇒ Object
- #overwrite ⇒ Object
-
#set_record_from_row(record, row) ⇒ Object
this will overwrite nils, even if wants_overwriting? is false returns true if an attr was changed, otherwise false.
-
#split_options ⇒ Object
Options that can’t be referred to by their names.
- #sprintf ⇒ Object
- #static ⇒ Object
- #synthesize ⇒ Object
- #to_units ⇒ Object
- #unit_from_source(row) ⇒ Object
- #units_field_name ⇒ Object
- #units_field_number ⇒ Object
- #upcase ⇒ Object
- #value_from_row(row) ⇒ Object
- #value_in_dictionary(str) ⇒ Object
- #value_in_source(row) ⇒ Object
- #wants_chars? ⇒ Boolean
- #wants_conversion? ⇒ Boolean
- #wants_dictionary? ⇒ Boolean
- #wants_matcher? ⇒ Boolean
- #wants_nullification? ⇒ Boolean
- #wants_overwriting? ⇒ Boolean
-
#wants_split? ⇒ Boolean
Our wants and needs :).
- #wants_sprintf? ⇒ Boolean
- #wants_static? ⇒ Boolean
- #wants_synthesize? ⇒ Boolean
- #wants_units? ⇒ Boolean
- #wants_upcase? ⇒ Boolean
Constructor Details
#initialize(step, name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/data_miner/attribute.rb', line 30 def initialize(step, name, = {}) .symbolize_keys! @step = step @name = name invalid_option_keys = .keys.select { |k| not VALID_OPTIONS.include? k } DataMiner.log_or_raise "Invalid options: #{invalid_option_keys.map(&:inspect).to_sentence} (#{inspect})" if invalid_option_keys.any? @options = end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/data_miner/attribute.rb', line 4 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/data_miner/attribute.rb', line 5 def @options end |
#step ⇒ Object
Returns the value of attribute step.
3 4 5 |
# File 'lib/data_miner/attribute.rb', line 3 def step @step end |
Instance Method Details
#chars ⇒ Object
217 218 219 |
# File 'lib/data_miner/attribute.rb', line 217 def chars [:chars] end |
#column_type ⇒ Object
135 136 137 |
# File 'lib/data_miner/attribute.rb', line 135 def column_type resource.columns_hash[name.to_s].type end |
#delimiter ⇒ Object
181 182 183 |
# File 'lib/data_miner/attribute.rb', line 181 def delimiter ([:delimiter] || ', ') end |
#dictionary ⇒ Object
226 227 228 |
# File 'lib/data_miner/attribute.rb', line 226 def dictionary @_dictionary ||= ([:dictionary].is_a?(Dictionary) ? [:dictionary] : Dictionary.new([:dictionary])) end |
#do_convert(row, value) ⇒ Object
115 116 117 118 |
# File 'lib/data_miner/attribute.rb', line 115 def do_convert(row, value) DataMiner.log_or_raise "If you use :from_units, you need to set :to_units (#{inspect})" unless wants_units? value.to_f.convert((from_units || unit_from_source(row)), (to_units || unit_from_source(row))) end |
#do_split(value) ⇒ Object
129 130 131 132 133 |
# File 'lib/data_miner/attribute.rb', line 129 def do_split(value) pattern = [:pattern] || /\s+/ # default is split on whitespace keep = [:keep] || 0 # default is keep first element value.to_s.split(pattern)[keep].to_s end |
#do_sprintf(value) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/data_miner/attribute.rb', line 120 def do_sprintf(value) if /\%[0-9\.]*f/.match sprintf value = value.to_f elsif /\%[0-9\.]*d/.match sprintf value = value.to_i end sprintf % value end |
#field_name ⇒ Object
Options that always have values
178 179 180 |
# File 'lib/data_miner/attribute.rb', line 178 def field_name ([:field_name] || name).to_s end |
#field_number ⇒ Object
214 215 216 |
# File 'lib/data_miner/attribute.rb', line 214 def field_number [:field_number] end |
#from_units ⇒ Object
190 191 192 |
# File 'lib/data_miner/attribute.rb', line 190 def from_units [:from_units] end |
#inspect ⇒ Object
41 42 43 |
# File 'lib/data_miner/attribute.rb', line 41 def inspect "Attribute(#{resource}##{name})" end |
#match_row(row) ⇒ Object
77 78 79 |
# File 'lib/data_miner/attribute.rb', line 77 def match_row(row) matcher.match row end |
#matcher ⇒ Object
229 230 231 |
# File 'lib/data_miner/attribute.rb', line 229 def matcher @_matcher ||= ([:matcher].is_a?(String) ? [:matcher].constantize.new : [:matcher]) end |
#nullify ⇒ Object
199 200 201 |
# File 'lib/data_miner/attribute.rb', line 199 def nullify [:nullify] end |
#overwrite ⇒ Object
202 203 204 |
# File 'lib/data_miner/attribute.rb', line 202 def overwrite [:overwrite] end |
#set_record_from_row(record, row) ⇒ Object
this will overwrite nils, even if wants_overwriting? is false returns true if an attr was changed, otherwise false
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/data_miner/attribute.rb', line 92 def set_record_from_row(record, row) return false if !wants_overwriting? and !record.send(name).nil? what_it_was = record.send name what_it_should_be = value_from_row row record.send "#{name}=", what_it_should_be record.send "#{name}_units=", (to_units || unit_from_source(row)).to_s if wants_units? what_it_is = record.send name if what_it_is.nil? and !what_it_should_be.nil? DataMiner.log_debug "ActiveRecord didn't like trying to set #{resource}.#{name} = #{what_it_should_be} (it came out as nil)" nil elsif what_it_is == what_it_was false else true end end |
#split_options ⇒ Object
Options that can’t be referred to by their names
186 187 188 |
# File 'lib/data_miner/attribute.rb', line 186 def [:split] end |
#sprintf ⇒ Object
196 197 198 |
# File 'lib/data_miner/attribute.rb', line 196 def sprintf [:sprintf] end |
#static ⇒ Object
223 224 225 |
# File 'lib/data_miner/attribute.rb', line 223 def static [:static] end |
#synthesize ⇒ Object
220 221 222 |
# File 'lib/data_miner/attribute.rb', line 220 def synthesize [:synthesize] end |
#to_units ⇒ Object
193 194 195 |
# File 'lib/data_miner/attribute.rb', line 193 def to_units [:to_units] || [:units] end |
#unit_from_source(row) ⇒ Object
111 112 113 |
# File 'lib/data_miner/attribute.rb', line 111 def unit_from_source(row) row[units_field_name || units_field_number].to_s.strip.underscore.to_sym end |
#units_field_name ⇒ Object
208 209 210 |
# File 'lib/data_miner/attribute.rb', line 208 def units_field_name [:units_field_name] end |
#units_field_number ⇒ Object
211 212 213 |
# File 'lib/data_miner/attribute.rb', line 211 def units_field_number [:units_field_number] end |
#upcase ⇒ Object
205 206 207 |
# File 'lib/data_miner/attribute.rb', line 205 def upcase [:upcase] end |
#value_from_row(row) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/data_miner/attribute.rb', line 81 def value_from_row(row) return match_row row if wants_matcher? value = value_in_source row return value if value.is_a? ActiveRecord::Base # carry through trapdoor value = value_in_dictionary value if wants_dictionary? value = synthesize.call(row) if wants_synthesize? value end |
#value_in_dictionary(str) ⇒ Object
45 46 47 |
# File 'lib/data_miner/attribute.rb', line 45 def value_in_dictionary(str) dictionary.lookup str end |
#value_in_source(row) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/data_miner/attribute.rb', line 49 def value_in_source(row) if wants_static? value = static elsif field_number if field_number.is_a?(Range) value = field_number.map { |n| row[n] }.join(delimiter) else value = row[field_number] end else value = row[field_name] end return nil if value.nil? return value if value.is_a?(ActiveRecord::Base) # escape valve for parsers that look up associations directly value = value.to_s value = value[chars] if wants_chars? value = do_split(value) if wants_split? # taken from old errata... maybe we want to do this here value.gsub! /[ ]+/, ' ' # text.gsub!('- ', '-') value.gsub! /([^\\])~/, '\1 ' value.strip! value.upcase! if wants_upcase? value = do_convert row, value if wants_conversion? value = do_sprintf value if wants_sprintf? value end |
#wants_chars? ⇒ Boolean
155 156 157 |
# File 'lib/data_miner/attribute.rb', line 155 def wants_chars? chars.present? end |
#wants_conversion? ⇒ Boolean
164 165 166 |
# File 'lib/data_miner/attribute.rb', line 164 def wants_conversion? from_units.present? or units_field_name.present? or units_field_number.present? end |
#wants_dictionary? ⇒ Boolean
170 171 172 |
# File 'lib/data_miner/attribute.rb', line 170 def wants_dictionary? [:dictionary].present? end |
#wants_matcher? ⇒ Boolean
173 174 175 |
# File 'lib/data_miner/attribute.rb', line 173 def wants_matcher? [:matcher].present? end |
#wants_nullification? ⇒ Boolean
152 153 154 |
# File 'lib/data_miner/attribute.rb', line 152 def wants_nullification? nullify != false end |
#wants_overwriting? ⇒ Boolean
161 162 163 |
# File 'lib/data_miner/attribute.rb', line 161 def wants_overwriting? overwrite != false end |
#wants_split? ⇒ Boolean
Our wants and needs :)
140 141 142 |
# File 'lib/data_miner/attribute.rb', line 140 def wants_split? .present? end |
#wants_sprintf? ⇒ Boolean
143 144 145 |
# File 'lib/data_miner/attribute.rb', line 143 def wants_sprintf? sprintf.present? end |
#wants_static? ⇒ Boolean
149 150 151 |
# File 'lib/data_miner/attribute.rb', line 149 def wants_static? .has_key? :static end |
#wants_synthesize? ⇒ Boolean
158 159 160 |
# File 'lib/data_miner/attribute.rb', line 158 def wants_synthesize? synthesize.is_a?(Proc) end |
#wants_units? ⇒ Boolean
167 168 169 |
# File 'lib/data_miner/attribute.rb', line 167 def wants_units? to_units.present? or units_field_name.present? or units_field_number.present? end |
#wants_upcase? ⇒ Boolean
146 147 148 |
# File 'lib/data_miner/attribute.rb', line 146 def wants_upcase? upcase.present? end |