Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for optional attributes #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Dec 23, 2015

  1. Support for optional attributes

    This commit introduces support for optional attributes. It avoids
    silently assigning the value `nil` to attributes that are not
    present in the original input, but are attributes of the validator
    object
    
    Consider the following, similar to the example in the README:
    
    ```ruby
    post.inspect
    
    class UpdatePost < Scrivener
      attr_accessor :title
      attr_accessor :author
    
      def validate
        if assert_present :title
          assert_length :title, 3..150
        end
      end
    end
    
    input = UpdatePost.new({ :title => "A Changed Title" })
    input.valid?
    ```
    
    This is correct. No assertions for `:author` are made, so no errors
    are expected. When using the output of `#attributes` however, data
    loss is very easy to occur.
    
    ```ruby
    post.update_attributes(input.attributes)
    post.inspect
    ```
    
    That's because `#attributes` assigns `nil` for "missing" attributes.
    
    ```ruby
    input.attributes
    ```
    
    This commit changes `#attributes` to not assign `nil` to "missing"
    attributes, so they become optional.
    
    ```ruby
    input.attributes
    ```
    
    It's still possible to make assertions on optional attributes:
    
    ```ruby
    class UpdatePost < Scrivener
      attr_accessor :title
      attr_accessor :author
    
      def validate
        if assert_present :title
          assert_length :title, 3..150
        end
    
        if author
          assert_match :author, /[A-Z]\w+ [A-Z]\w+/
        end
      end
    end
    ```
    
    The change is subtle, but existing code might expect missing
    attributes to be `nil. If it is to be merged, I think it's wise to
    bump the version to 2.0.
    Jip van Reijsen committed Dec 23, 2015
    Configuration menu
    Copy the full SHA
    eb1e265 View commit details
    Browse the repository at this point in the history