denormalize_mm 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mongo_mapper/denormalization.rb +33 -33
- metadata +1 -1
| @@ -1,48 +1,48 @@ | |
| 1 | 
            -
             | 
| 2 | 
            -
              module Denormalization
         | 
| 3 | 
            -
                def self.included(mod)
         | 
| 4 | 
            -
                  mod.extend(MongoMapper::Denormalization::ClassMethods)
         | 
| 5 | 
            -
                end
         | 
| 1 | 
            +
            require 'mongo_mapper'
         | 
| 6 2 |  | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 3 | 
            +
            module MongoMapper::Denormalization
         | 
| 4 | 
            +
              def self.included(mod)
         | 
| 5 | 
            +
                mod.extend(MongoMapper::Denormalization::ClassMethods)
         | 
| 6 | 
            +
              end
         | 
| 11 7 |  | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 8 | 
            +
              module ClassMethods
         | 
| 9 | 
            +
                def denormalize_field(association, field, options={})
         | 
| 10 | 
            +
                  denormalize(association, field, options)
         | 
| 11 | 
            +
                end
         | 
| 15 12 |  | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 13 | 
            +
                def denormalize_association(dest, options={})
         | 
| 14 | 
            +
                  options = options.dup
         | 
| 15 | 
            +
                  source = options.delete(:from)
         | 
| 19 16 |  | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
                    }.merge(options))
         | 
| 17 | 
            +
                  if !source
         | 
| 18 | 
            +
                    raise "denormalize_association must take a from (source) option"
         | 
| 23 19 | 
             
                  end
         | 
| 24 20 |  | 
| 25 | 
            -
                   | 
| 26 | 
            -
                     | 
| 21 | 
            +
                  denormalize(source, dest, {
         | 
| 22 | 
            +
                    :target_field => dest,
         | 
| 23 | 
            +
                  }.merge(options))
         | 
| 24 | 
            +
                end
         | 
| 27 25 |  | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
                    target_field_code = options[:target_field] || "#{association}_#{field}"
         | 
| 26 | 
            +
                def denormalize(association, field, options={})
         | 
| 27 | 
            +
                  method_name = "denormalize_#{association}_#{field}"
         | 
| 31 28 |  | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 29 | 
            +
                  validation_method = options[:on] || "before_validation"
         | 
| 30 | 
            +
                  source_field_code = options[:source_field] || "#{association}.#{field}"
         | 
| 31 | 
            +
                  target_field_code = options[:target_field] || "#{association}_#{field}"
         | 
| 34 32 |  | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
                          self.#{target_field_code} = #{source_field_code}
         | 
| 38 | 
            -
                        end
         | 
| 33 | 
            +
                  self.class_eval <<-CODE, __FILE__, __LINE__
         | 
| 34 | 
            +
                    #{validation_method} :#{method_name}
         | 
| 39 35 |  | 
| 40 | 
            -
             | 
| 36 | 
            +
                    def #{method_name}
         | 
| 37 | 
            +
                      if self.#{association}
         | 
| 38 | 
            +
                        self.#{target_field_code} = #{source_field_code}
         | 
| 41 39 | 
             
                      end
         | 
| 42 40 |  | 
| 43 | 
            -
                       | 
| 44 | 
            -
                     | 
| 45 | 
            -
             | 
| 41 | 
            +
                      true
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                    private :#{method_name}
         | 
| 45 | 
            +
                  CODE
         | 
| 46 46 | 
             
                end
         | 
| 47 47 | 
             
              end
         | 
| 48 48 | 
             
            end
         |