Module: Imperator::Mongoid::AttributeHelper::ClassMethods

Defined in:
lib/imperator/mongoid/attribute_helper.rb

Instance Method Summary collapse

Instance Method Details

#attributes_for(clazz, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/imperator/mongoid/attribute_helper.rb', line 7

def attributes_for clazz, options = {}
  use_attributes = clazz.attribute_names

  unless options[:except].blank?
    use_attributes = use_attributes - [options[:except]].flatten.map(&:to_s)
  end
  unless options[:only].blank?
    use_attributes = use_attributes & [options[:only]].flatten.map(&:to_s) # intersection
  end

  clazz.fields.each do |key, field|
    name = key
    type = field.type
    # skip if this field is excluded for use in command
    next unless use_attributes.include? name.to_s
    
    attribute field.name, field.type, :default => field.default_val
  end
end