Class: DataShift::MethodDetail

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logdir, #logger

Constructor Details

#initialize(client_name, klass, operator, type, col_types = {}, find_by_operator = nil, find_by_value = nil) ⇒ MethodDetail

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

col_types can typically be derived from klass.columns - set of ActiveRecord::ConnectionAdapters::Column



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/datashift/method_detail.rb', line 64

def initialize(client_name, klass, operator, type, col_types = {}, find_by_operator = nil, find_by_value = nil )
  @klass, @name = klass, client_name
  @find_by_operator = find_by_operator
  @find_by_value = find_by_value

  if( MethodDetail::supported_types_enum.member?(type.to_sym) )
    @operator_type = type.to_sym
  else
    raise "Bad operator Type #{type} passed to Method Detail"
  end

  @operator = operator

  # Note : Not all assignments will currently have a column type, for example
  # those that are derived from a delegate_belongs_to
  if(col_types.empty?)
    @col_type = klass.columns.find{ |col| col.name == operator }
  else
    @col_type = col_types[operator]
  end
  
  @column_index = -1
end

Instance Attribute Details

#col_typeObject (readonly)

The rel col type from the DB



46
47
48
# File 'lib/datashift/method_detail.rb', line 46

def col_type
  @col_type
end

#column_indexObject

Returns the value of attribute column_index.



43
44
45
# File 'lib/datashift/method_detail.rb', line 43

def column_index
  @column_index
end

#find_by_operatorObject

TODO make it a list/primary keys Additional helpers for where clauses



55
56
57
# File 'lib/datashift/method_detail.rb', line 55

def find_by_operator
  @find_by_operator
end

#find_by_valueObject

TODO make it a list/primary keys Additional helpers for where clauses



55
56
57
# File 'lib/datashift/method_detail.rb', line 55

def find_by_value
  @find_by_value
end

#klassObject

Klass is the class of the ‘parent’ object i.e with the associations, For example Product which may have operator orders



39
40
41
# File 'lib/datashift/method_detail.rb', line 39

def klass
  @klass
end

#nameObject

Name is the raw, client supplied name e.g Orders



42
43
44
# File 'lib/datashift/method_detail.rb', line 42

def name
  @name
end

#operatorObject (readonly)

The :operator that can be called to assign e.g orders or Products.new.orders << Order.new

The type of operator e.g :assignment, :belongs_to, :has_one, :has_many etc



51
52
53
# File 'lib/datashift/method_detail.rb', line 51

def operator
  @operator
end

#operator_typeObject (readonly)

The :operator that can be called to assign e.g orders or Products.new.orders << Order.new

The type of operator e.g :assignment, :belongs_to, :has_one, :has_many etc



51
52
53
# File 'lib/datashift/method_detail.rb', line 51

def operator_type
  @operator_type
end

Class Method Details

.association_types_enumObject



26
27
28
29
# File 'lib/datashift/method_detail.rb', line 26

def self.association_types_enum
  @assoc_type_enum ||= Set[:belongs_to, :has_one, :has_many]
  @assoc_type_enum
end

.is_association_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/datashift/method_detail.rb', line 32

def self.is_association_type? ( type )
  association_types_enum.member?( type )
end

.supported_types_enumObject



21
22
23
24
# File 'lib/datashift/method_detail.rb', line 21

def self.supported_types_enum
  @type_enum ||= Set[:assignment, :belongs_to, :has_one, :has_many]
  @type_enum
end

Instance Method Details

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

Returns:

  • (Boolean)


96
97
98
# File 'lib/datashift/method_detail.rb', line 96

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

#operator_classObject

Return the operator’s expected class, if can be derived, else nil



117
118
119
120
# File 'lib/datashift/method_detail.rb', line 117

def operator_class()
  @operator_class ||= get_operator_class()
  @operator_class
end

#operator_class_nameObject

Return the operator’s expected class name, if can be derived, else nil



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/datashift/method_detail.rb', line 101

def operator_class_name()
  @operator_class_name ||= 
    if(operator_for(:has_many) || operator_for(:belongs_to) || operator_for(:has_one))
   
    get_operator_class.name
  
  elsif(@col_type)
    @col_type.type.to_s.classify
  else
    ""
  end

  @operator_class_name
end

#operator_for(type) ⇒ Object

Return the actual operator’s name for supplied method type where type one of :assignment, :has_one, :belongs_to, :has_many etc



91
92
93
94
# File 'lib/datashift/method_detail.rb', line 91

def operator_for( type )
  return operator if(@operator_type == type.to_sym)
  nil
end

#ppObject



122
123
124
# File 'lib/datashift/method_detail.rb', line 122

def pp
  "#{@name} => #{operator}"
end