Class: DataShift::ModelMethod

Inherits:
Operator
  • Object
show all
Includes:
Comparable, Logging
Defined in:
lib/datashift/model_methods/model_method.rb

Instance Attribute Summary collapse

Attributes inherited from Operator

#operator, #operator_type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

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

Methods inherited from Operator

#operator?, #operator_for, #operator_type?, supported_types_enum

Constructor Details

#initialize(klass, operator, type, connection_adapter_column = nil) ⇒ ModelMethod

Operator is a population type method call on klass

Type determines the style of operator call; simple assignment, an association or a method call



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datashift/model_methods/model_method.rb', line 40

def initialize(klass, operator, type, connection_adapter_column = nil)

  super(operator, type)

  @klass = klass

  # Note : Not all assignments will currently have a column type, for example
  # those that are derived from a delegate_belongs_to
  keys =  Module.const_defined?(:Mongoid) ? klass.fields.keys : klass.columns.map(&:name)
  @connection_adapter_column = keys.find { |col| col == operator } if connection_adapter_column.nil?

  @connection_adapter_column = DataShift::ModelMethods::Catalogue.column_type_for(klass, operator) if connection_adapter_column.nil?
end

Instance Attribute Details

#connection_adapter_columnObject (readonly)

The real column data from the DB - via ActiveRecord::ConnectionAdapters::Column when available



34
35
36
# File 'lib/datashift/model_methods/model_method.rb', line 34

def connection_adapter_column
  @connection_adapter_column
end

#klassObject

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



30
31
32
# File 'lib/datashift/model_methods/model_method.rb', line 30

def klass
  @klass
end

Class Method Details

.association_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/datashift/model_methods/model_method.rb', line 24

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

.association_types_enumObject



19
20
21
22
# File 'lib/datashift/model_methods/model_method.rb', line 19

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

Instance Method Details

#<=>(other) ⇒ Object



90
91
92
# File 'lib/datashift/model_methods/model_method.rb', line 90

def <=>(other)
  state <=> other.state
end

#==(other) ⇒ Object Also known as: eql?



84
85
86
# File 'lib/datashift/model_methods/model_method.rb', line 84

def ==(other)
  other.class == self.class && other.state == state
end

#association_columnsObject



80
81
82
# File 'lib/datashift/model_methods/model_method.rb', line 80

def association_columns
  klass.reflect_on_association(operator).klass.columns
end

#association_type?Boolean

Returns true of MM is an association (rather than plain attribute or, enum or method)

Returns:

  • (Boolean)


76
77
78
# File 'lib/datashift/model_methods/model_method.rb', line 76

def association_type?
  ModelMethod.association_type?( operator_type )
end

#can_cast?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/datashift/model_methods/model_method.rb', line 117

def can_cast?
  connection_adapter_column && connection_adapter_column.respond_to?(:cast_type)
end

#hashObject



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

def hash
  state.hash
end

#operator_classObject

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



71
72
73
# File 'lib/datashift/model_methods/model_method.rb', line 71

def operator_class
  @operator_class ||= determine_operator_class
end

#operator_class_nameObject

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/datashift/model_methods/model_method.rb', line 55

def operator_class_name
  @operator_class_name ||=
    if operator_for(:has_many) || operator_for(:belongs_to) || operator_for(:has_one)

      determine_operator_class.name

    elsif connection_adapter_column
      connection_adapter_column.type.to_s.classify
    else
      ''
    end

  @operator_class_name
end

#ppObject



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

def pp
  x = <<-EOS
  Class         [#{klass.name}]
  Operator Type [#{operator_type}]
  Operator      [#{operator}]
  EOS

  if can_cast?
    x += <<-EOS
  Col/SqlType   [#{connection_adapter_column.class} - #{connection_adapter_column.cast_type.class.name}]
    EOS
  end
  x
end