module MongoidNestedFields::NestedField::ClassMethods

Public Instance Methods

add_field(name) click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 91
def add_field(name)
  @_fields ||= []
  @_fields << name.to_sym
end
add_nested_field(name) click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 96
def add_nested_field(name)
  add_field(name)
  @_nested_fields ||= []
  @_nested_fields << name.to_sym
end
allowed_types_in_field(field) click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 78
def allowed_types_in_field(field)
  field = field.to_sym
  @_allowed_types_in_field ||= {}
  return @_allowed_types_in_field[field] || []
end
field(name, options = {}) click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 42
def field(name, options = {})
  add_field(name)
  set_field_options(name, options)
  define_method("#{name}=") do |value|
    klass = options[:type].nil? ? String : options.delete(:type).classify.constantize
    write_attribute(name, klass.new(value))
  end
  
  define_method(name) do
    read_attribute(name)
  end
  
end
field_options() click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 61
def field_options
  @_field_options || {}
end
is_allowed_type?(field, type) click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 84
def is_allowed_type?(field, type)
  field = field.to_sym
  allowed_types_in_field(field).include?(type)
end
nested_field(name, options = {}) click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 7
def nested_field(name, options = {})
  options.merge!({:nested => true})
  
  add_nested_field(name)
  
  set_validations
  set_field_options(name, options)
  set_allowed_types_in_field(name, options[:allowed_types])
  
  define_method("#{name}=") do |value|
    raise TypeError unless [String, Array].include?(value.class)
    if value.is_a? String
      parser = Yajl::Parser.new
      value = parser.parse(value)
    end
    processed_values = []
    value.to_a.each do |v|
      v.stringify_keys!
      if(v.is_a?(Hash) && !v['_type'].nil?)
        v = v['_type'].classify.constantize.new(v)
        
      end
      raise MongoidNestedFields::Errors::UnexpectedType.new(v.class, name) unless self.class.is_allowed_type?(name, v.class)
      processed_values << v
    end
    write_attribute(name, processed_values)
  end
  
  define_method(name) do
    read_attribute(name).map{ |v| v.respond_to?(:to_mongo) ? v.to_mongo : v }
    read_attribute(name)
  end

end
set_allowed_types_in_field(field, type) click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 69
def set_allowed_types_in_field(field, type)
  field = field.to_sym
  @_allowed_types_in_field ||= {}
  @_allowed_types_in_field[field] ||= []
  @_allowed_types_in_field[field] << type
  @_allowed_types_in_field[field].flatten!
  @_allowed_types_in_field[field].uniq! unless @_allowed_types_in_field[field].nil?
end
set_field_options(field, options) click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 56
def set_field_options(field, options)
  @_field_options ||= {}
  @_field_options[field] = options
end
set_validations() click to toggle source
# File lib/mongoid_nested_fields/nested_field_part.rb, line 65
def set_validations
  validate :nested_fields_must_be_valid
end