Module: Mongoid::Denormalize::ClassMethods

Defined in:
lib/mongoid-denormalize.rb

Instance Method Summary collapse

Instance Method Details

#denormalize(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mongoid-denormalize.rb', line 10

def denormalize(*args)
  *fields, options = args

  unless options.is_a?(Hash) && options[:from]
    raise ArgumentError, 'Option :from is needed (e.g. denormalize :name, from: :user).'
  end

  fields = Mongoid::Denormalize.get_fields_with_names(self, fields, options)

  # Add fields to model
  fields.each { |field| field field[:as], type: field[:type] }

  # Add hooks
  Mongoid::Denormalize.add_hook_to_child(self, fields, options)
  Mongoid::Denormalize.add_hook_to_parent(self, fields, options)
end