Class: DataShift::ModelMethods::Catalogue

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Logging
Defined in:
lib/datashift/model_methods/catalogue.rb

Overview

HIGH LEVEL COLLECTION METHODS

Class Method Summary collapse

Methods included from Logging

logdir, logdir=, logger, verbose

Class Method Details

.assignmentsObject



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

def self.assignments
  @assignments ||= {}
  @assignments
end

.assignments_for(klass) ⇒ Object



130
131
132
# File 'lib/datashift/model_methods/catalogue.rb', line 130

def self.assignments_for(klass)
  assignments[klass] || []
end

.belongs_toObject

rubocop:disable Style/PredicateName



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

def self.belongs_to
  @belongs_to ||= {}
  @belongs_to
end

.belongs_to_for(klass) ⇒ Object



118
119
120
# File 'lib/datashift/model_methods/catalogue.rb', line 118

def self.belongs_to_for(klass)
  belongs_to[klass] || []
end

.catalogued?(klass) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/datashift/model_methods/catalogue.rb', line 22

def self.catalogued?(klass)
  catalogued.include?(klass)
end

.clearObject



71
72
73
74
75
76
77
78
# File 'lib/datashift/model_methods/catalogue.rb', line 71

def self.clear
  belongs_to.clear
  has_many.clear
  assignments.clear
  column_types.clear
  has_one.clear
  catalogued.clear
end

.column_type_for(klass, column) ⇒ Object



134
135
136
# File 'lib/datashift/model_methods/catalogue.rb', line 134

def self.column_type_for(klass, column)
  column_types[klass] ? column_types[klass][column] : []
end

.column_typesObject



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

def self.column_types
  @column_types ||= {}
  @column_types
end

.has_manyObject



87
88
89
90
# File 'lib/datashift/model_methods/catalogue.rb', line 87

def self.has_many
  @has_many ||= {}
  @has_many
end

.has_many_for(klass) ⇒ Object



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

def self.has_many_for(klass)
  has_many[klass] || []
end

.has_oneObject



92
93
94
95
# File 'lib/datashift/model_methods/catalogue.rb', line 92

def self.has_one
  @has_one ||= {}
  @has_one
end

.has_one_for(klass) ⇒ Object



126
127
128
# File 'lib/datashift/model_methods/catalogue.rb', line 126

def self.has_one_for(klass)
  has_one[klass] || []
end

.populate(klass, options = {}) ⇒ Object

Create simple picture of all the operator names for assignment available on a domain model, grouped by type of association (includes belongs_to and has_many which provides both << and = ) Options:

:reload => clear caches and re-perform  lookup
:instance_methods => if true include instance method type 'setters' as well as model's pure columns


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/datashift/model_methods/catalogue.rb', line 36

def self.populate(klass, options = {} )

  raise "Cannot find operators supplied klass nil #{klass}" if klass.nil?

  register(klass)

  logger.debug("Catalogue - building operators information for #{klass}")

  # Find the has_many associations which can be populated via <<
  if options[:reload] || has_many[klass].nil?
    has_many[klass] = klass.reflect_on_all_associations(:has_many).map { |i| i.name.to_s }

    klass.reflect_on_all_associations(:has_and_belongs_to_many).inject(has_many[klass]) do |x, i|
      x << i.name.to_s
    end
  end

  # Find the belongs_to associations which can be populated via  Model.belongs_to_name = OtherArModelObject
  if options[:reload] || belongs_to[klass].nil?
    belongs_to[klass] = klass.reflect_on_all_associations(:belongs_to).map { |i| i.name.to_s }
  end

  # Find the has_one associations which can be populated via  Model.has_one_name = OtherArModelObject
  if options[:reload] || has_one[klass].nil?
    has_one[klass] = klass.reflect_on_all_associations(:has_one).map { |i| i.name.to_s }
  end

  # Find the model's column associations which can be populated via xxxxxx= value
  # Note, not all reflections return method names in same style so we convert all to
  # the raw form i.e without the '='  for consistency
  if options[:reload] || assignments[klass].nil?
    build_assignments( klass, options[:instance_methods] )
  end
end

.setters(klass) ⇒ Object

N.B this return strings for consistency with other collections Removes methods that start with ‘_’



105
106
107
108
109
110
111
# File 'lib/datashift/model_methods/catalogue.rb', line 105

def self.setters( klass )

  @keep_only_pure_setters ||= Regexp.new(/^[a-zA-Z]\w+=/)

  setters = klass.instance_methods.grep(@keep_only_pure_setters).sort.collect( &:to_s )
  setters.uniq # TOFIX is this really required ?
end

.sizeObject



26
27
28
# File 'lib/datashift/model_methods/catalogue.rb', line 26

def self.size
  catalogued.size
end