Class: DataShift::MethodBinding
- Inherits:
-
Object
- Object
- DataShift::MethodBinding
show all
- Includes:
- Logging
- Defined in:
- lib/datashift/inbound_data/method_binding.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Logging
#logdir, #logdir=, #logger, #verbose
Constructor Details
#initialize(name, idx, model_method) ⇒ MethodBinding
Store the raw (client supplied) name against the active record klass(model).
Operator is the associated method call on klass,
i.e client supplies name 'Price' in a spreadsheet,
but true operator to call on klass is price
type determines the style of operator call; simple assignment, an association or a method call
col_types can typically be derived from klass.columns - set of ActiveRecord::ConnectionAdapters::Column
38
39
40
41
42
43
44
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 38
def initialize(name, idx, model_method)
@inbound_column = InboundData::Column.new(name, idx)
@model_method = model_method
@valid = (name && model_method) ? true : false
end
|
Instance Attribute Details
#inbound_column ⇒ Object
Returns the value of attribute inbound_column.
22
23
24
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 22
def inbound_column
@inbound_column
end
|
#model_method ⇒ Object
Returns the value of attribute model_method.
20
21
22
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 20
def model_method
@model_method
end
|
Is this method detail a valid mapping, aids identifying unmapped/unmappable columns
27
28
29
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 27
def valid
@valid
end
|
Instance Method Details
#add_column_data(data) ⇒ Object
71
72
73
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 71
def add_column_data(data)
inbound_column.data << data
end
|
#add_lookup(model_method, field, value) ⇒ Object
Example :
Project:name:My Best Project
User (klass) has_one project lookup where name(field) == 'My Best Project' (value)
User.project.where( :name => 'My Best Project')
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 80
def add_lookup( model_method, field, value)
klass = model_method.klass
association = klass.reflect_on_association(model_method.operator)
if association && association.klass.new.respond_to?(field)
inbound_column.add_lookup(association.klass, field, value)
logger.info("Complex Lookup specified for [#{model_method.operator}] : on field [#{field}] (optional value [#{value}])")
else
logger.error("Check MethodBinding [#{inbound_name}](#{inbound_index}) - Association field names are case sensitive")
raise NoSuchOperator.new("Field [#{field}] Not Found on Association [#{model_method.operator}] within Class #{klass.name}")
end
end
|
#class_name ⇒ Object
59
60
61
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 59
def class_name
model_method.klass.name
end
|
#inbound_index ⇒ Object
67
68
69
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 67
def inbound_index
inbound_column.index
end
|
#inbound_name ⇒ Object
63
64
65
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 63
def inbound_name
inbound_column.source
end
|
#invalid? ⇒ Boolean
101
102
103
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 101
def invalid?
!valid?
end
|
55
56
57
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 55
def klass
model_method.klass
end
|
47
48
49
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 47
def operator
model_method ? model_method.operator : ''
end
|
#operator?(name, case_sensitive = false) ⇒ Boolean
51
52
53
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 51
def operator?(name, case_sensitive = false)
model_method ? model_method.operator?(name, case_sensitive) : false
end
|
105
106
107
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 105
def pp
"Binding: Column [#{inbound_index}] : Header [#{inbound_name}] : Operator [#{model_method.operator}]"
end
|
109
110
111
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 109
def spp
"Column [#{inbound_index}] : Header [#{inbound_name}]"
end
|
#valid? ⇒ Boolean
97
98
99
|
# File 'lib/datashift/inbound_data/method_binding.rb', line 97
def valid?
(@valid == true)
end
|