dynamometer 0.0.7 → 0.0.8
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.
- checksums.yaml +4 -4
- data/README.md +14 -0
- data/app/models/concerns/dynamic_attributes.rb +14 -0
- data/lib/dynamometer/version.rb +1 -1
- data/test/dummy/app/models/person.rb +4 -0
- data/test/person_test.rb +6 -0
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 921d2fe3bb72e407fb99012ded3142f80d25ba28
         | 
| 4 | 
            +
              data.tar.gz: 7419a085efb613ef2a5f78b22bdd450afb9f97d5
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 988d4cef8655ad8c5524eb646fe4f77f9e9b3fea24250bc48d6fb00966364f7114b498b056f0e3be63486ece12a7e3b7edc22e9e8936085c38cb8c924efe42ed
         | 
| 7 | 
            +
              data.tar.gz: ea42b47cfb048c87f34aec18ed908045cb5518e3801e8e5960a387cef5a1ddbbd656d3db0e15bea3c0577a0d2c571f8c3581b3da7f1f15d407954d0b49652f3d
         | 
    
        data/README.md
    CHANGED
    
    | @@ -54,6 +54,20 @@ Dynamic attributes will appear in your model's attributes `user.attributes` as i | |
| 54 54 |  | 
| 55 55 | 
             
            You can access just the dynamic attributes by calling `dynamic_attributes`.
         | 
| 56 56 |  | 
| 57 | 
            +
            ## Validating
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            You can validate dynamic attributes if you declare them in your model:
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                class Person < ActiveRecord::Base
         | 
| 62 | 
            +
                  include DynamicAttributes
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                  dynamic_attributes :hometown
         | 
| 65 | 
            +
                  validates_length_of :hometown, minimum: 2, allow_nil: true
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
            Attempting to validate undeclared dynamic attributes will fail with NoMethodError if
         | 
| 69 | 
            +
            the attribute hasn't been set at all; don't do that.
         | 
| 70 | 
            +
             | 
| 57 71 | 
             
            ## Querying
         | 
| 58 72 |  | 
| 59 73 | 
             
            You can query for matches to dynamic attributes just like regular attributes.
         | 
| @@ -8,6 +8,20 @@ module DynamicAttributes | |
| 8 8 | 
             
                def partition_wheres(wheres)
         | 
| 9 9 | 
             
                  wheres.partition { |k, v| column_names.include?(k.to_s) || reflect_on_association(k.to_sym) }.map { |x| Hash[x] }
         | 
| 10 10 | 
             
                end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def dynamic_attributes(*args)
         | 
| 13 | 
            +
                  args.each do |attr|
         | 
| 14 | 
            +
                    class_eval <<-ENDOFCODE
         | 
| 15 | 
            +
                      def #{attr}
         | 
| 16 | 
            +
                        read_dynamic_attribute(#{attr.inspect})
         | 
| 17 | 
            +
                      end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                      def #{attr}=(v)
         | 
| 20 | 
            +
                        write_dynamic_attribute(#{attr.inspect}, v)
         | 
| 21 | 
            +
                      end
         | 
| 22 | 
            +
                    ENDOFCODE
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 11 25 | 
             
              end
         | 
| 12 26 |  | 
| 13 27 | 
             
              def [](attr_name)
         | 
    
        data/lib/dynamometer/version.rb
    CHANGED
    
    
    
        data/test/person_test.rb
    CHANGED
    
    | @@ -103,6 +103,12 @@ class PersonTest < ActiveSupport::TestCase | |
| 103 103 | 
             
                assert_equal @person, results.first
         | 
| 104 104 | 
             
              end
         | 
| 105 105 |  | 
| 106 | 
            +
              test "can put validations on dynamic attributes" do
         | 
| 107 | 
            +
                @person = Person.new(name: 'Nobody', hometown: 'X')
         | 
| 108 | 
            +
                assert !@person.valid?
         | 
| 109 | 
            +
                assert @person.errors.has_key?(:hometown)
         | 
| 110 | 
            +
              end
         | 
| 111 | 
            +
             | 
| 106 112 | 
             
              test "chaining does not damage original Relation" do
         | 
| 107 113 | 
             
                original_relation = Person.where(id: 1)
         | 
| 108 114 | 
             
                new_relation = original_relation.where(magic_level: 'over 9000')
         |