Class: DataMiner::Attribute

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

Instance Method Summary collapse

Instance Attribute Details

#charsRange (readonly)

Which characters in a field to keep. Zero-based.

Returns:

  • (Range)


110
111
112
# File 'lib/data_miner/attribute.rb', line 110

def chars
  @chars
end

#delimiterString (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.

Returns:

  • (String)


106
107
108
# File 'lib/data_miner/attribute.rb', line 106

def delimiter
  @delimiter
end

#field_nameSymbol (readonly)

Where to find the data in the row.

Returns:

  • (Symbol)


102
103
104
# File 'lib/data_miner/attribute.rb', line 102

def field_name
  @field_name
end

#field_numberInteger, 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.

Returns:

  • (Integer, Range)


98
99
100
# File 'lib/data_miner/attribute.rb', line 98

def field_number
  @field_number
end

#from_unitsSymbol (readonly)

Initial units. May invoke a conversion using a conversion gem like rubygems.org/gems/alchemist Be sure to set DataMiner.unit_converter

Returns:

  • (Symbol)


130
131
132
# File 'lib/data_miner/attribute.rb', line 130

def from_units
  @from_units
end

#matcherObject (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

Returns:

  • (Object)


91
92
93
# File 'lib/data_miner/attribute.rb', line 91

def matcher
  @matcher
end

#nameSymbol (readonly)

Local column name.

Returns:

  • (Symbol)


76
77
78
# File 'lib/data_miner/attribute.rb', line 76

def name
  @name
end

#nullify_blank_stringsTrueClass, FalseClass (readonly)

Only meaningful for string columns. Whether to store blank input (“ ”) as NULL. Defaults to DEFAULT_NULLIFY_BLANK_STRINGS.

Returns:

  • (TrueClass, FalseClass)


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

def nullify_blank_strings
  @nullify_blank_strings
end

#overwriteTrueClass, FalseClass (readonly)

Whether to overwrite the value in a local column if it is not null. Defaults to DEFAULT_OVERWRITE.

Returns:

  • (TrueClass, FalseClass)


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

def overwrite
  @overwrite
end

#splitHash (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.

Returns:

  • (Hash)


118
119
120
# File 'lib/data_miner/attribute.rb', line 118

def split
  @split
end

#sprintfString (readonly)

A sprintf-style format to apply.

Returns:

  • (String)


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

def sprintf
  @sprintf
end

#staticString, ... (readonly)

A static value to be used.

Returns:

  • (String, Numeric, TrueClass, FalseClass, Object)


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

def static
  @static
end

#synthesizeProc (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

Returns:

  • (Proc)


83
84
85
# File 'lib/data_miner/attribute.rb', line 83

def synthesize
  @synthesize
end

#to_unitsSymbol (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.

Returns:

  • (Symbol)


125
126
127
# File 'lib/data_miner/attribute.rb', line 125

def to_units
  @to_units
end

#units_field_nameSymbol (readonly)

If every row specifies its own units, where to find the units.

Returns:

  • (Symbol)


138
139
140
# File 'lib/data_miner/attribute.rb', line 138

def units_field_name
  @units_field_name
end

#units_field_numberInteger (readonly)

If every row specifies its own units, index of where to find the units. Zero-based.

Returns:

  • (Integer)


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

def units_field_number
  @units_field_number
end

#upcaseTrueClass, FalseClass (readonly)

Whether to upcase value. Defaults to DEFAULT_UPCASE.

Returns:

  • (TrueClass, FalseClass)


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

def upcase
  @upcase
end

Instance Method Details

#dictionaryDataMiner::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