Class: Mongoid::Max::Denormalize::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/max/denormalize/base.rb

Direct Known Subclasses

ManyToOne, OneToMany

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, meta, inverse_meta, fields, options) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
# File 'lib/mongoid/max/denormalize/base.rb', line 10

def initialize(klass, meta, inverse_meta, fields, options)
  @klass = klass
  @meta = meta
  @inverse_meta = inverse_meta
  @fields = fields
  @options = options

  verify
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



8
9
10
# File 'lib/mongoid/max/denormalize/base.rb', line 8

def fields
  @fields
end

#inverse_metaObject

Returns the value of attribute inverse_meta.



8
9
10
# File 'lib/mongoid/max/denormalize/base.rb', line 8

def inverse_meta
  @inverse_meta
end

#klassObject

Returns the value of attribute klass.



8
9
10
# File 'lib/mongoid/max/denormalize/base.rb', line 8

def klass
  @klass
end

#metaObject

Returns the value of attribute meta.



8
9
10
# File 'lib/mongoid/max/denormalize/base.rb', line 8

def meta
  @meta
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/mongoid/max/denormalize/base.rb', line 8

def options
  @options
end

Class Method Details

.array_code_for(fields) ⇒ Object



67
68
69
# File 'lib/mongoid/max/denormalize/base.rb', line 67

def array_code_for(fields)
  fields.map { |field| ":#{field}" }.join(", ")
end

Instance Method Details

#allowed_optionsObject



37
38
39
# File 'lib/mongoid/max/denormalize/base.rb', line 37

def allowed_options
  []
end

#fields_methodsObject



56
57
58
59
60
# File 'lib/mongoid/max/denormalize/base.rb', line 56

def fields_methods
  @fields_methods ||= fields.select do |field|
    meta.klass.fields[field.to_s].nil?
  end
end

#fields_onlyObject



61
62
63
# File 'lib/mongoid/max/denormalize/base.rb', line 61

def fields_only
  @fields_only ||= fields - fields_methods
end

#inverse_klassObject



51
52
53
# File 'lib/mongoid/max/denormalize/base.rb', line 51

def inverse_klass
  meta.klass
end

#inverse_relationObject



48
49
50
# File 'lib/mongoid/max/denormalize/base.rb', line 48

def inverse_relation
  meta.inverse
end

#relationObject



45
46
47
# File 'lib/mongoid/max/denormalize/base.rb', line 45

def relation
  meta.name
end

#unallowed_optionsObject



41
42
43
# File 'lib/mongoid/max/denormalize/base.rb', line 41

def unallowed_options
  options.keys - allowed_options
end

#verifyObject

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid/max/denormalize/base.rb', line 20

def verify
  # There are fields
  raise ConfigError.new("Nothing to denormalize", klass, relation) if fields.empty? && options.empty?

  # All fields/methods are well defined
  fields.each do |field|
    unless meta.klass.instance_methods.include? field
      raise ConfigError.new("Unknown field or method :#{field}", klass, relation)
    end
  end

  # All options are allowed
  unless unallowed_options.empty?
    raise ConfigError.new("Unknown or not supported options :#{unallowed_options.first}", klass, relation)
  end
end