Class: DataShift::ModelMethods::Collection

Inherits:
Object
  • Object
show all
Extended by:
Logging, Forwardable
Includes:
Logging, Enumerable
Defined in:
lib/datashift/model_methods/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

logdir, logdir=, logger, verbose

Constructor Details

#initialize(klass) ⇒ Collection

Returns a new instance of Collection.



37
38
39
40
41
# File 'lib/datashift/model_methods/collection.rb', line 37

def initialize( klass )
  @managed_class = klass
  @model_method_list = []
  @by_optype = {}
end

Instance Attribute Details

#by_optypeObject

Hash of all available methods by type

[:assignment] => [:id, :name], [:belongs_to] => [:user, :address]


29
30
31
# File 'lib/datashift/model_methods/collection.rb', line 29

def by_optype
  @by_optype
end

#managed_classObject (readonly)

Returns the value of attribute managed_class.



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

def managed_class
  @managed_class
end

#model_method_listObject

Returns the value of attribute model_method_list.



31
32
33
# File 'lib/datashift/model_methods/collection.rb', line 31

def model_method_list
  @model_method_list
end

Instance Method Details

#<<(model_method) ⇒ Object



68
69
70
# File 'lib/datashift/model_methods/collection.rb', line 68

def <<(model_method)
  add(model_method)
end

#add(model_method) ⇒ Object



55
56
57
58
59
# File 'lib/datashift/model_methods/collection.rb', line 55

def add(model_method)
  model_method_list << model_method

  maintain_by_optype_helper model_method
end

#available_operatorsObject



123
124
125
# File 'lib/datashift/model_methods/collection.rb', line 123

def available_operators
  model_method_list.collect(&:operator)
end

#available_operators_with_typeObject

A reverse map showing all operators with their associated ‘type’



128
129
130
131
132
133
134
135
136
# File 'lib/datashift/model_methods/collection.rb', line 128

def available_operators_with_type
  h = {}
  by_optype.each { |t, mms| mms.each { |v| h[v.operator] = t } }

  # this is meant to be more efficient that Hash[h.sort]
  sh = {}
  h.keys.sort.each { |k| sh[k] = h[k] }
  sh
end

#eachObject



81
82
83
# File 'lib/datashift/model_methods/collection.rb', line 81

def each
  model_method_list.each { |mm| yield(mm) }
end

#find_association(name) ⇒ Object

Search for matching ModelMethod for given name across Association types



103
104
105
106
107
108
109
110
# File 'lib/datashift/model_methods/collection.rb', line 103

def find_association(name)
  ModelMethod.association_types_enum.each do |type|
    model_method = find_by_name_and_type(name, type)
    return model_method if model_method
  end

  nil
end

#find_by_name_and_type(name, type) ⇒ Object

Return matching ModelMethod for given name and specific type



96
97
98
99
100
# File 'lib/datashift/model_methods/collection.rb', line 96

def find_by_name_and_type(name, type)
  by_optype = model_method_list.find_all { |mm| mm.operator_type? type }

  by_optype.find { |mm| mm.operator? name }
end

#for_type(type) ⇒ Object

Returns all ModelMethod(s) for supplied type e.g :belongs_to type is expected to be one of ModelMethod::supported_types_enum



114
115
116
# File 'lib/datashift/model_methods/collection.rb', line 114

def for_type( type )
  model_method_list.find_all { |mm| mm.operator_type? type }
end

#get_operators(type) ⇒ Object

Get list of Rails model operators



119
120
121
# File 'lib/datashift/model_methods/collection.rb', line 119

def get_operators(type)
  for_type(type).collect(&:operator)
end

#insert(operator, type) ⇒ Object



49
50
51
52
53
# File 'lib/datashift/model_methods/collection.rb', line 49

def insert(operator, type)
  mm = ModelMethod.new(managed_class, operator, type)
  add( mm )
  mm
end

#maintain_by_optype_helper(model_method) ⇒ Object



61
62
63
64
65
66
# File 'lib/datashift/model_methods/collection.rb', line 61

def maintain_by_optype_helper( model_method )
  # Helper list of all available by type  [:belongs_to]
  by_optype[model_method.operator_type.to_sym] ||= []
  by_optype[model_method.operator_type.to_sym] << model_method
  by_optype[model_method.operator_type.to_sym].uniq!
end

#managed_class_nameObject Also known as: klass



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

def managed_class_name
  @managed_class.name
end

#push(model_method) ⇒ Object



72
73
74
# File 'lib/datashift/model_methods/collection.rb', line 72

def push(model_method)
  add(model_method)
end

#search(name) ⇒ Object

Search for matching ModelMethod for given name across all types in supported_types_enum order



86
87
88
89
90
91
92
93
# File 'lib/datashift/model_methods/collection.rb', line 86

def search(name)
  ModelMethod.supported_types_enum.each do |type|
    model_method = find_by_name_and_type(name, type)
    return model_method if model_method
  end

  nil
end

#unshift(model_method) ⇒ Object



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

def unshift(model_method)
  model_method_list.unshift model_method
  maintain_by_optype_helper model_method
end