New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

robert

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

robert

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Robert

Robert is an implementation of the builder pattern that first runs the given arguments through a validation process.

Installation

gem install robert

Usage

Robert is helpful in creating form objects (validating Rack params), implementing business logic (service objects, if you will) and much more. Basically if you do not trust the data, run it through Robert.

  class Post
    attr_accessor :title, :body

    def initialize(attributes)
      attributes.each do |key, value|
        send(:"#{key}=", value)
      end
    end
  end

  class PostBuilder < Robert
    attr_accessor :title, :body

    private

    def validate
      error :title, :is_empty { empty? }
      error :body,  :is_empty { empty? }
    end
  end

  builder = PostBuilder.new(title: 'Title', body: '')

  result = builder.build(Post)

  result # false

  builder.errors # { body: [:is_empty] }

  builder.body = 'body'

  result = builder.build(Post)

  result.is_a?(Post) # true

Check out the tests for more examples.

API

::build - Convenience for ::new(params).build(klass)

::build! - Convenience for ::new(params).build!(klass)

#attributes - Hash of attributes that have been validated (successfully) and "cleaned".

#errors - Hash of errors.

#build - Initializes the given class if valid or returns false.

#build! - Initializes the given class if valid or raises.

#clean - Template method for cleaning values before assigning them the attributes hash.

#error - Bread and butter of the library. For a given attribute, adds error if the given block returns false. The block is evaluated in context of the attribute itself.

#validate - Template method for implementing your validation logic.

#valid? - Checks to see if the builder is valid.

Gotchas

Sending #errors before #valid? will return the default errors (an empty Hash by default), which will give the appearance of a valid object. The reason for this is that if you have an html view and are rendering a form/builder's errors, you don't want to #errors to trigger validation on the initial page load.

Prior Art

Robert is inspired by the following libraries:

  • Scrivener
  • Hatch
  • Django's Forms

FAQs

Package last updated on 21 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc