All Projects → dogweather → Validated_object

dogweather / Validated_object

Licence: mit
Self-validating Ruby objects

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Validated object

Octicons
A scalable set of icons handcrafted with <3 by GitHub
Stars: ✭ 7,039 (+12249.12%)
Mutual labels:  gem
Modern Resume Theme
A modern static resume template and theme. Powered by Jekyll and GitHub pages.
Stars: ✭ 868 (+1422.81%)
Mutual labels:  gem
Render async
render_async lets you include pages asynchronously with AJAX
Stars: ✭ 974 (+1608.77%)
Mutual labels:  gem
Green Button Data
Fast Ruby parser and API client for Green Button data
Stars: ✭ 18 (-68.42%)
Mutual labels:  gem
Moab Versioning
Gem to process digital object version content, metadata, and manifests
Stars: ✭ 9 (-84.21%)
Mutual labels:  gem
Sixarm ruby magic number type
SixArm.com » Ruby » MagicNumberType infers a data type from the data's leading bytes
Stars: ✭ 13 (-77.19%)
Mutual labels:  gem
Batch Loader
⚡️ Powerful tool for avoiding N+1 DB or HTTP queries
Stars: ✭ 812 (+1324.56%)
Mutual labels:  gem
Colorize
Ruby string class extension. It add some methods to set color, background color and text effect on console easier using ANSI escape sequences.
Stars: ✭ 1,082 (+1798.25%)
Mutual labels:  gem
Itamae Plugin Resource Encrypted remote file
encrypt secret data (e.g. id_rsa), and forward decrypted file to remote.
Stars: ✭ 10 (-82.46%)
Mutual labels:  gem
Ruby Gem Downloads Badge
Clean and simple gem downloads count badge, courtesy of http://shields.io/. You can checkout the application directly at the following URL:
Stars: ✭ 29 (-49.12%)
Mutual labels:  gem
Datagrid
Gem to create tables grids with sortable columns and filters
Stars: ✭ 921 (+1515.79%)
Mutual labels:  gem
Puree
Metadata extraction from the Pure Research Information System.
Stars: ✭ 8 (-85.96%)
Mutual labels:  gem
Lita Line
A Line adapter for Lita
Stars: ✭ 15 (-73.68%)
Mutual labels:  gem
Bh
Bootstrap Helpers for Ruby
Stars: ✭ 834 (+1363.16%)
Mutual labels:  gem
Pig Ci Rails
Monitor your Ruby Applications metrics (Memory, SQL Requests & Request Time) as part of your test suite.
Stars: ✭ 53 (-7.02%)
Mutual labels:  gem
Exception notification Shoryuken
Exception Notifier Plugin for Rails with Shoryuken http://smartinez87.github.com/exception_notification
Stars: ✭ 5 (-91.23%)
Mutual labels:  gem
Lumberyard Cubism3 Gem
An Amazon Lumberyard Gem that adds in Live2D Cubism3 functionality to LyShine.
Stars: ✭ 13 (-77.19%)
Mutual labels:  gem
Dry Validation
Validation library with type-safe schemas and rules
Stars: ✭ 1,087 (+1807.02%)
Mutual labels:  gem
Matchete
A DSL for method overloading in Ruby based on pattern matching
Stars: ✭ 53 (-7.02%)
Mutual labels:  gem
Administrate Field Belongs to search
Plugin that adds search capabilities to belongs_to associations for Administrate
Stars: ✭ 29 (-49.12%)
Mutual labels:  gem

Gem Version Build Status Code Climate

ValidatedObject

Plain Old Ruby Objects + Rails Validations = self-checking Ruby objects.

Goals

  • Very readable error messages
  • Clean, minimal syntax

This is a small layer around ActiveModel::Validations. (About 18 lines of code.) So if you know how to use Rails Validations, you're good to go. I wrote this to help with CSV data imports and website microdata generation.

Usage

Writing a self-validating object

All of the ActiveModel::Validations are available, plus a new one, TypeValidator.

class Dog < ValidatedObject::Base
  # Plain old Ruby
  attr_accessor :name, :birthday  # attr_reader is supported as well for read-only attributes

  # Plain old Rails
  validates :name, presence: true
  
  # A new type-validation if you'd like to use it
  validates :birthday, type: Date, allow_nil: true  # Strongly typed but optional
end

The included TypeValidator is what enables type: Date, above. All classes can be checked, as well as a pseudo-class Boolean. E.g.:

#...
validates :premium_membership, type: Boolean
#...

Instantiating and automatically validating

# This Dog instance validates itself at the end of instantiation.
spot = Dog.new(name: 'Spot')
# We can also explicitly test for validity because all of
# ActiveModel::Validations is available.
spot.valid?  # => true

spot.birthday = Date.new(2015, 1, 23)
spot.valid?  # => true

Good error messages

Any of the standard Validations methods can be used to test an instance, plus the custom check_validations! convenience method:

spot.birthday = '2015-01-23'
spot.valid?  # => false
spot.check_validations!  # => ArgumentError: Birthday is a String, not a Date

Note the clear, explicit error message. These are great when reading a log file following a data import. It describes all the invalid conditions. Let's test it by making another attribute invalid:

spot.name = nil
spot.check_validations!  # => ArgumentError: Name can't be blank; Birthday is a String, not a Date

Use in parsing data

I often use a validated object in a loop to import data, e.g.:

# Import a CSV file of dogs
dogs = []
csv.next_row do |row|
  begin
    dogs << Dog.new(name: row.name)
  rescue ArgumentError => e
    logger.warn(e)
  end
end

The result is that dogs is an array of guaranteed valid Dog objects. And the error log lists unparseable rows with good info for tracking down problems in the data.

Use in code generation

My Schema.org microdata generation gem uses ValidatedObjects to recursively create well formed HTML / JSON-LD.

Installation

Add this line to your application's Gemfile:

gem 'validated_object'

And then execute:

$ bundle

Or install it yourself as:

$ gem install validated_object

Development

(TODO: Verify these instructions.) After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].