Git Product home page Git Product logo

semantic-attributes's Introduction

SemanticAttributes
==================
-by Lance Ivy, 2007

http://github.com/cainlevy/semantic-attributes

http://code.google.com/p/semanticattributes/

==Summary

A validation library that allows introspection (User.name_is_required?) and supports database normalization (aka "form input cleaning").

==Philosophy

The method-chained validation routine built into ActiveRecord must die! It's time for an object-oriented approach to attribute validations. The Semantic Attributes plugin provides this approach by letting you attach predicates to your attributes with a tasty DSL. These predicates package up some really sweet behavior, where validations are really only the beginning. I've also discovered that it can be really useful to use these predicates to convert between human and machine formats: for example, with the phone number predicate you can let your users enter phone numbers with whatever formatting they want, always save the values to the database as numeric strings, and then present the values back to the user with standard formatting.

I've also found other nifty uses for object-oriented predicates that package up validation. For example, it becomes easy to run a quick validation check on a field with a sample value and report true/false. This is exactly what the <tt>expected_error_for(:field, value)</tt> method does, and it lets you build a validation routine that listens to form data as it's being typed and report problems without duplicating your validation code client-side. In a similar vein, the <tt>_valid?</tt> attribute suffix lets you do single-attribute validation on a record anytime you want.

==Example

  class User < ActiveRecord::Base
    email_is_an_email
    home_page_is_a_url :domains => ['com', 'net', 'org'], :allow_ip_address => false
    mobile_is_a_phone_number
  end

Now imagine a sample script/console session:

  >> User.name_is_required?
  => true
  >> User.mobile_is_required?
  => false

Ok, we have a DSL for introspection. What if we want to retrieve configuration details?

  >> User.semantic_attributes[:home_page].get(:url).domains
  => ['com', 'net', 'org']

Let's create a user and play around with some instance methods:

  >> user = User.new
  >> user.mobile = '222 333.4444'
  >> user.mobile_valid?
  => true
  >> user.mobile
  => '+12223334444'
  >> user.mobile_for_human
  => '(222) 333-4444'

==See Also
* gist.rdoc
* Predicates
* ActiveRecord::Predicates::ClassMethods (see #method_missing)
* ActiveRecord::AttributeFormats

semantic-attributes's People

Contributors

cainlevy avatar tiegz avatar ktheory avatar

Stargazers

Felipe Monteiro avatar Ezequiel Erbaro avatar

Watchers

Erik Kastner avatar  avatar Andy Baio avatar Justin Jones avatar Alex Cox avatar Miguel Senquiz avatar Garrow Bedrossian avatar Brandon Williams avatar David Peter avatar John Dimatos avatar Molly Neuman avatar Zack Sears avatar Corey Farwell avatar Cory Roy avatar Rebecca Sliter avatar Patrick Presto avatar Will Duffy avatar James Cloos avatar Rushaine McBean avatar Jon Chang avatar Kilian McMahon avatar Olivier Gillaizeau avatar  avatar Sarah Groff Hennigh-Palermo avatar Daniel Hunninghake avatar Nick Smit avatar Rae Farine avatar slim avatar David Ouyang Moench avatar Pedro Carmo avatar Jackson Lynch avatar Amanda Pickering avatar Brett Shollenberger avatar Setsun avatar Heather Swift Hunt  avatar  avatar Carol Benovic avatar Margaret Smith avatar Stefan Dufgran avatar Robert Lum avatar  avatar  avatar  avatar  avatar  avatar  avatar Mil Reyes avatar George Schmalz avatar Terry Hope Romero avatar Alfie Palao avatar Nick Novak avatar  avatar Melissa Wong avatar Oluwatobi Popoola avatar  avatar Courtney avatar Susanna Brumm avatar travis brace avatar Amy Fain avatar Caitlin Bahari avatar Sid Orlando avatar  avatar  avatar Owen Katz avatar  avatar  avatar Saarim Zaman avatar Christella avatar Graham Jones avatar  avatar Darell Koh avatar Ruti Wajnberg avatar Lena Imamura avatar  avatar Wen Fu avatar Tom (Tung Ling) Lin avatar Arjun Mahesh avatar Brittany Levers avatar Elliott Cost avatar Jonathan Lee avatar Xiaojie Wu avatar Anya Combs avatar  avatar Clarissa Redwine avatar David Ninh avatar Fabiola Carcamo avatar Glory Edim avatar  avatar Toy Vano avatar  avatar  avatar Peninah "Rose" Mbogho avatar  avatar Jia Li avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.