Class: DataShift::MethodBinding

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/datashift/inbound_data/method_binding.rb

Direct Known Subclasses

InternalMethodBinding, NoMethodBinding

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logdir, #logdir=, #logger, #verbose

Constructor Details

#initialize(name, model_method, idx: nil) ⇒ 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



39
40
41
42
43
44
45
# File 'lib/datashift/inbound_data/method_binding.rb', line 39

def initialize(name, model_method, idx: nil)
  @inbound_column = InboundData::Column.new(name, idx)

  @model_method = model_method

  @valid = name && model_method ? true : false
end

Instance Attribute Details

#inbound_columnObject (readonly)

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_methodObject (readonly)

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

#validObject

Is this method detail a valid mapping, aids identifying unmapped/unmappable columns



28
29
30
# File 'lib/datashift/inbound_data/method_binding.rb', line 28

def valid
  @valid
end

Instance Method Details

#add_column_data(data) ⇒ Object



64
65
66
# File 'lib/datashift/inbound_data/method_binding.rb', line 64

def add_column_data(data)
  inbound_column.data << data if(inbound_column)
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')


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/datashift/inbound_data/method_binding.rb', line 73

def add_lookup( model_method, field, value)

  # check the finder method name is a valid field on the actual association class
  klass = model_method.klass

  association = klass.reflect_on_association(model_method.operator)

  # TODO: - this is instance methods .. what about class methods ?
  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 [#{source}](#{index}) - Association field names are case sensitive")
    raise NoSuchOperator, "Field [#{field}] Not Found on Association [#{model_method.operator}] within Class #{klass.name}"
  end
end

#class_nameObject



60
61
62
# File 'lib/datashift/inbound_data/method_binding.rb', line 60

def class_name
  model_method.klass.name
end

#invalid?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/datashift/inbound_data/method_binding.rb', line 94

def invalid?
  !valid?
end

#klassObject



56
57
58
# File 'lib/datashift/inbound_data/method_binding.rb', line 56

def klass
  model_method.klass
end

#operatorObject

TODO: - use delegators



48
49
50
# File 'lib/datashift/inbound_data/method_binding.rb', line 48

def operator
  model_method ? model_method.operator : ''
end

#operator?(name, case_sensitive = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/datashift/inbound_data/method_binding.rb', line 52

def operator?(name, case_sensitive = false)
  model_method ? model_method.operator?(name, case_sensitive) : false
end

#ppObject



98
99
100
# File 'lib/datashift/inbound_data/method_binding.rb', line 98

def pp
  "Binding: Column [#{index}] : Header [#{source}] : Operator [#{model_method.operator}]"
end

#sppObject



102
103
104
# File 'lib/datashift/inbound_data/method_binding.rb', line 102

def spp
  "Column [#{index}] : Header [#{source}]"
end

#valid?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/datashift/inbound_data/method_binding.rb', line 90

def valid?
  (@valid == true)
end