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
# 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
  @connection_adapter_column = klass.columns.find { |col| col.name == 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



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

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

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



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

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

#association_columnsObject



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

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)


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

def association_type?
  ModelMethod.association_type?( operator_type )
end

#hashObject



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

def hash
  state.hash
end

#operator_classObject

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



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

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



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

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



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

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

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