Class: DataMiner::Attribute
- Inherits:
-
Object
- Object
- DataMiner::Attribute
- Defined in:
- lib/data_miner/attribute.rb
Overview
A mapping between a local model column and a remote data source column.
Constant Summary collapse
- VALID_OPTIONS =
[ :from_units, :to_units, :static, :dictionary, :matcher, :field_name, :delimiter, :split, :units, :sprintf, :nullify, # deprecated :nullify_blank_strings, :overwrite, :upcase, :units_field_name, :units_field_number, :field_number, :chars, :synthesize, ]
- VALID_UNIT_DEFINITION_SETS =
[ [:units], # no conversion [:from_units, :to_units], # yes [:units_field_name], # no [:units_field_name, :to_units], # yes [:units_field_number], # no [:units_field_number, :to_units], # yes ]
- DEFAULT_SPLIT_PATTERN =
/\s+/
- DEFAULT_SPLIT_KEEP =
0
- DEFAULT_DELIMITER =
', '
- DEFAULT_NULLIFY_BLANK_STRINGS =
false
- DEFAULT_UPCASE =
false
- DEFAULT_OVERWRITE =
true
- TRUE_VALUES =
activerecord-3.2.6/lib/active_record/connection_adapters/column.rb
[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON', 'yes', 'YES', 'y', 'Y']
- FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'no', 'NO', 'n', 'N']
Instance Attribute Summary collapse
-
#chars ⇒ Range
readonly
Which characters in a field to keep.
-
#delimiter ⇒ String
readonly
A delimiter to be used when joining fields together into a single final value.
-
#field_name ⇒ Symbol
readonly
Where to find the data in the row.
-
#field_number ⇒ Integer, Range
readonly
Index of where to find the data in the row, starting from zero.
-
#from_units ⇒ Symbol
readonly
Initial units.
-
#matcher ⇒ Object
readonly
An object that will be sent #match(row) and should return a final value.
-
#name ⇒ Symbol
readonly
Local column name.
-
#nullify_blank_strings ⇒ TrueClass, FalseClass
readonly
Only meaningful for string columns.
-
#overwrite ⇒ TrueClass, FalseClass
readonly
Whether to overwrite the value in a local column if it is not null.
-
#split ⇒ Hash
readonly
How to split a field.
-
#sprintf ⇒ String
readonly
A
sprintf
-style format to apply. -
#static ⇒ String, ...
readonly
A static value to be used.
-
#synthesize ⇒ Proc
readonly
Synthesize a value by passing a proc that will receive
row
and should return a final value. -
#to_units ⇒ Symbol
readonly
Final units.
-
#units_field_name ⇒ Symbol
readonly
If every row specifies its own units, where to find the units.
-
#units_field_number ⇒ Integer
readonly
If every row specifies its own units, index of where to find the units.
-
#upcase ⇒ TrueClass, FalseClass
readonly
Whether to upcase value.
Instance Method Summary collapse
-
#dictionary ⇒ DataMiner::Dictionary
Dictionary for translating.
-
#set_from_row(local_record, remote_row) ⇒ Object
# @private TODO make sure that nil handling is replicated when using upsert.
Instance Attribute Details
#chars ⇒ Range (readonly)
Which characters in a field to keep. Zero-based.
110 111 112 |
# File 'lib/data_miner/attribute.rb', line 110 def chars @chars end |
#delimiter ⇒ String (readonly)
A delimiter to be used when joining fields together into a single final value. Used when :field_number
is a Range
. Defaults to DEFAULT_DELIMITER.
106 107 108 |
# File 'lib/data_miner/attribute.rb', line 106 def delimiter @delimiter end |
#field_name ⇒ Symbol (readonly)
Where to find the data in the row.
102 103 104 |
# File 'lib/data_miner/attribute.rb', line 102 def field_name @field_name end |
#field_number ⇒ Integer, Range (readonly)
Index of where to find the data in the row, starting from zero.
If you pass a Range
, then multiple fields will be joined together.
98 99 100 |
# File 'lib/data_miner/attribute.rb', line 98 def field_number @field_number end |
#from_units ⇒ Symbol (readonly)
Initial units. May invoke a conversion using a conversion gem like rubygems.org/gems/alchemist Be sure to set DataMiner.unit_converter
130 131 132 |
# File 'lib/data_miner/attribute.rb', line 130 def from_units @from_units end |
#matcher ⇒ Object (readonly)
An object that will be sent #match(row) and should return a final value.
Can be specified as a String which will be constantized into a class and an object of that class instantized with no arguments.
row
will be a Hash
with string keys or (less often) an Array
91 92 93 |
# File 'lib/data_miner/attribute.rb', line 91 def matcher @matcher end |
#name ⇒ Symbol (readonly)
Local column name.
76 77 78 |
# File 'lib/data_miner/attribute.rb', line 76 def name @name end |
#nullify_blank_strings ⇒ TrueClass, FalseClass (readonly)
Only meaningful for string columns. Whether to store blank input (“ ”) as NULL. Defaults to DEFAULT_NULLIFY_BLANK_STRINGS.
150 151 152 |
# File 'lib/data_miner/attribute.rb', line 150 def nullify_blank_strings @nullify_blank_strings end |
#overwrite ⇒ TrueClass, FalseClass (readonly)
Whether to overwrite the value in a local column if it is not null. Defaults to DEFAULT_OVERWRITE.
158 159 160 |
# File 'lib/data_miner/attribute.rb', line 158 def overwrite @overwrite end |
#split ⇒ Hash (readonly)
How to split a field. You specify two options:
:pattern
: what to split on. Defaults to DEFAULT_SPLIT_PATTERN. :keep
: which of elements resulting from the split to keep. Defaults to DEFAULT_SPLIT_KEEP.
118 119 120 |
# File 'lib/data_miner/attribute.rb', line 118 def split @split end |
#sprintf ⇒ String (readonly)
A sprintf
-style format to apply.
142 143 144 |
# File 'lib/data_miner/attribute.rb', line 142 def sprintf @sprintf end |
#static ⇒ String, ... (readonly)
A static value to be used.
146 147 148 |
# File 'lib/data_miner/attribute.rb', line 146 def static @static end |
#synthesize ⇒ Proc (readonly)
Synthesize a value by passing a proc that will receive row
and should return a final value.
row
will be a Hash
with string keys or (less often) an Array
83 84 85 |
# File 'lib/data_miner/attribute.rb', line 83 def synthesize @synthesize end |
#to_units ⇒ Symbol (readonly)
Final units. May invoke a conversion using rubygems.org/gems/alchemist
If a local column named [name]_units
exists, it will be populated with this value.
125 126 127 |
# File 'lib/data_miner/attribute.rb', line 125 def to_units @to_units end |
#units_field_name ⇒ Symbol (readonly)
If every row specifies its own units, where to find the units.
138 139 140 |
# File 'lib/data_miner/attribute.rb', line 138 def units_field_name @units_field_name end |
#units_field_number ⇒ Integer (readonly)
If every row specifies its own units, index of where to find the units. Zero-based.
134 135 136 |
# File 'lib/data_miner/attribute.rb', line 134 def units_field_number @units_field_number end |
#upcase ⇒ TrueClass, FalseClass (readonly)
Whether to upcase value. Defaults to DEFAULT_UPCASE.
154 155 156 |
# File 'lib/data_miner/attribute.rb', line 154 def upcase @upcase end |
Instance Method Details
#dictionary ⇒ DataMiner::Dictionary
Dictionary for translating.
You pass a Hash
of options which is used to initialize a DataMiner::Dictionary
.
206 207 208 209 210 |
# File 'lib/data_miner/attribute.rb', line 206 def dictionary @dictionary || @dictionary_mutex.synchronize do @dictionary ||= Dictionary.new(@dictionary_settings) end end |
#set_from_row(local_record, remote_row) ⇒ Object
# @private TODO make sure that nil handling is replicated when using upsert
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/data_miner/attribute.rb', line 214 def set_from_row(local_record, remote_row) previously_nil = local_record.send(name).nil? currently_nil = false if previously_nil or overwrite new_value = read remote_row local_record.send "#{name}=", new_value currently_nil = new_value.nil? end if not currently_nil and persist_units? and (final_to_units = (to_units || read_units(remote_row))) local_record.send "#{name}_units=", final_to_units end end |