Module: MongoidNestedFields::NestedField::ClassMethods
- Defined in:
- lib/mongoid_nested_fields/nested_field_part.rb
Instance Method Summary collapse
- #add_field(name) ⇒ Object
- #add_nested_field(name) ⇒ Object
- #allowed_types_in_field(field) ⇒ Object
- #field(name, options = {}) ⇒ Object
- #field_options ⇒ Object
- #is_allowed_type?(field, type) ⇒ Boolean
- #nested_field(name, options = {}) ⇒ Object
- #set_allowed_types_in_field(field, type) ⇒ Object
- #set_field_options(field, options) ⇒ Object
- #set_validations ⇒ Object
Instance Method Details
#add_field(name) ⇒ Object
91 92 93 94 |
# 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) ⇒ Object
96 97 98 99 100 |
# 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) ⇒ Object
78 79 80 81 82 |
# 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 = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mongoid_nested_fields/nested_field_part.rb', line 42 def field(name, = {}) add_field(name) (name, ) define_method("#{name}=") do |value| klass = [:type].nil? ? String : .delete(:type).classify.constantize write_attribute(name, klass.new(value)) end define_method(name) do read_attribute(name) end end |
#field_options ⇒ Object
61 62 63 |
# File 'lib/mongoid_nested_fields/nested_field_part.rb', line 61 def @_field_options || {} end |
#is_allowed_type?(field, type) ⇒ Boolean
84 85 86 87 |
# 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 = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mongoid_nested_fields/nested_field_part.rb', line 7 def nested_field(name, = {}) .merge!({:nested => true}) add_nested_field(name) set_validations (name, ) set_allowed_types_in_field(name, [: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) ⇒ Object
69 70 71 72 73 74 75 76 |
# 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) ⇒ Object
56 57 58 59 |
# File 'lib/mongoid_nested_fields/nested_field_part.rb', line 56 def (field, ) @_field_options ||= {} @_field_options[field] = end |
#set_validations ⇒ Object
65 66 67 |
# File 'lib/mongoid_nested_fields/nested_field_part.rb', line 65 def set_validations validate :nested_fields_must_be_valid end |