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



103
104
105
106
# File 'lib/datashift/model_methods/catalogue.rb', line 103

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

.assignments_for(klass) ⇒ Object



136
137
138
# File 'lib/datashift/model_methods/catalogue.rb', line 136

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

.belongs_toObject

rubocop:disable Style/PredicateName



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

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

.belongs_to_for(klass) ⇒ Object



124
125
126
# File 'lib/datashift/model_methods/catalogue.rb', line 124

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



77
78
79
80
81
82
83
84
# File 'lib/datashift/model_methods/catalogue.rb', line 77

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

.column_names(klass) ⇒ Object



144
145
146
# File 'lib/datashift/model_methods/catalogue.rb', line 144

def self.column_names( klass )
  Module.const_defined?(:Mongoid) ? klass.fields.keys : klass.column_names
end

.column_type_for(klass, column) ⇒ Object



140
141
142
# File 'lib/datashift/model_methods/catalogue.rb', line 140

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

.column_typesObject



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

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

.has_manyObject



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

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

.has_many_for(klass) ⇒ Object



128
129
130
# File 'lib/datashift/model_methods/catalogue.rb', line 128

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

.has_oneObject



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

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

.has_one_for(klass) ⇒ Object



132
133
134
# File 'lib/datashift/model_methods/catalogue.rb', line 132

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
70
71
72
73
74
75
# 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?
    if Module.const_defined?(:Mongoid)
      has_many[klass] = klass.reflect_on_all_associations(:embeds_many).map { |i| i.name.to_s }
    else
      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
  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] = if Module.const_defined?(:Mongoid)
                       klass.reflect_on_all_associations(:embeds_one).map { |i| i.name.to_s }
                     else
                       klass.reflect_on_all_associations(:has_one).map { |i| i.name.to_s }
                     end
  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
  build_assignments( klass, options[:instance_methods] ) if options[:reload] || assignments[klass].nil?
end

.setters(klass) ⇒ Object

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



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

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