Class: DataMiner::Attribute

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  options.symbolize_keys!

  @step = step
  @name = name
  
  invalid_option_keys = options.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 = options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/data_miner/attribute.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/data_miner/attribute.rb', line 5

def options
  @options
end

#stepObject

Returns the value of attribute step.



3
4
5
# File 'lib/data_miner/attribute.rb', line 3

def step
  @step
end

Instance Method Details

#charsObject



217
218
219
# File 'lib/data_miner/attribute.rb', line 217

def chars
  options[:chars]
end

#column_typeObject



135
136
137
# File 'lib/data_miner/attribute.rb', line 135

def column_type
  resource.columns_hash[name.to_s].type
end

#delimiterObject



181
182
183
# File 'lib/data_miner/attribute.rb', line 181

def delimiter
  (options[:delimiter] || ', ')
end

#dictionaryObject



226
227
228
# File 'lib/data_miner/attribute.rb', line 226

def dictionary
  @_dictionary ||= (options[:dictionary].is_a?(Dictionary) ? options[:dictionary] : Dictionary.new(options[: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 = split_options[:pattern] || /\s+/ # default is split on whitespace
  keep = split_options[: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_nameObject

Options that always have values



178
179
180
# File 'lib/data_miner/attribute.rb', line 178

def field_name
  (options[:field_name] || name).to_s
end

#field_numberObject



214
215
216
# File 'lib/data_miner/attribute.rb', line 214

def field_number
  options[:field_number]
end

#from_unitsObject



190
191
192
# File 'lib/data_miner/attribute.rb', line 190

def from_units
  options[:from_units]
end

#inspectObject



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

#matcherObject



229
230
231
# File 'lib/data_miner/attribute.rb', line 229

def matcher
  @_matcher ||= (options[:matcher].is_a?(String) ? options[:matcher].constantize.new : options[:matcher])
end

#nullifyObject



199
200
201
# File 'lib/data_miner/attribute.rb', line 199

def nullify
  options[:nullify]
end

#overwriteObject



202
203
204
# File 'lib/data_miner/attribute.rb', line 202

def overwrite
  options[: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_optionsObject

Options that can’t be referred to by their names



186
187
188
# File 'lib/data_miner/attribute.rb', line 186

def split_options
  options[:split]
end

#sprintfObject



196
197
198
# File 'lib/data_miner/attribute.rb', line 196

def sprintf
  options[:sprintf]
end

#staticObject



223
224
225
# File 'lib/data_miner/attribute.rb', line 223

def static
  options[:static]
end

#synthesizeObject



220
221
222
# File 'lib/data_miner/attribute.rb', line 220

def synthesize
  options[:synthesize]
end

#to_unitsObject



193
194
195
# File 'lib/data_miner/attribute.rb', line 193

def to_units
  options[:to_units] || options[: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_nameObject



208
209
210
# File 'lib/data_miner/attribute.rb', line 208

def units_field_name
  options[:units_field_name]
end

#units_field_numberObject



211
212
213
# File 'lib/data_miner/attribute.rb', line 211

def units_field_number
  options[:units_field_number]
end

#upcaseObject



205
206
207
# File 'lib/data_miner/attribute.rb', line 205

def upcase
  options[: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

Returns:

  • (Boolean)


155
156
157
# File 'lib/data_miner/attribute.rb', line 155

def wants_chars?
  chars.present?
end

#wants_conversion?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


170
171
172
# File 'lib/data_miner/attribute.rb', line 170

def wants_dictionary?
  options[:dictionary].present?
end

#wants_matcher?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/data_miner/attribute.rb', line 173

def wants_matcher?
  options[:matcher].present?
end

#wants_nullification?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/data_miner/attribute.rb', line 152

def wants_nullification?
  nullify != false
end

#wants_overwriting?Boolean

Returns:

  • (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 :)

Returns:

  • (Boolean)


140
141
142
# File 'lib/data_miner/attribute.rb', line 140

def wants_split?
  split_options.present?
end

#wants_sprintf?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/data_miner/attribute.rb', line 143

def wants_sprintf?
  sprintf.present?
end

#wants_static?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/data_miner/attribute.rb', line 149

def wants_static?
  options.has_key? :static
end

#wants_synthesize?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/data_miner/attribute.rb', line 158

def wants_synthesize?
  synthesize.is_a?(Proc)
end

#wants_units?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


146
147
148
# File 'lib/data_miner/attribute.rb', line 146

def wants_upcase?
  upcase.present?
end