github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

pluginaweek / attribute_predicates

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 5
    • 0
  • Source
  • Commits
  • Network (0)
  • Downloads (8)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (8)
    • v0.2.0
    • v0.1.2
    • v0.1.1
    • v0.1.0
    • v0.0.5
    • v0.0.4
    • v0.0.3
    • v0.0.2
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Adds automatic generation of predicate methods when defining attributes — Read more

  cancel

http://www.pluginaweek.org

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Add gemspec 
obrie (author)
Mon Jun 08 19:31:17 -0700 2009
commit  546d0cf09591d9a53a730a4c2785cf21dd5671ab
tree    bad20ee734fed4be750f463c1e765275a70557a4
parent  6e314c63c4248306b8b4f9a4f1c9ba900b0bd59a
attribute_predicates /
name age
history
message
file .gitignore Fri Jul 04 15:49:08 -0700 2008 Ignore test/app_root/script [obrie]
file CHANGELOG.rdoc Sun Dec 14 17:38:31 -0800 2008 Tag 0.2.0 release [obrie]
file LICENSE Wed Jun 25 20:23:39 -0700 2008 Rename MIT-LICENSE to LICENSE [obrie]
file README.rdoc Fri Jul 04 14:36:59 -0700 2008 Add more documentation [obrie]
file Rakefile Mon Jun 08 19:31:17 -0700 2009 Add gemspec [obrie]
file attribute_predicates.gemspec Mon Jun 08 19:31:17 -0700 2009 Add gemspec [obrie]
file init.rb Thu Jul 03 19:29:12 -0700 2008 Rename to attribute_predicates [obrie]
directory lib/ Sun Dec 14 17:38:17 -0800 2008 Remove the PluginAWeek namespace [obrie]
directory test/ Sun Jul 06 20:42:58 -0700 2008 Remove dependency on active_support for non-Act... [obrie]
README.rdoc

attribute_predicates

attribute_predicates adds automatic generation of predicate methods (truth accessors) when defining attributes using attr, attr_reader, attr_writer, and attr_accessor.

Resources

API

  • api.pluginaweek.org/attribute_predicates

Bugs

  • pluginaweek.lighthouseapp.com/projects/13777-attribute_predicates

Development

  • github.com/pluginaweek/attribute_predicates

Source

  • git://github.com/pluginaweek/attribute_predicates.git

Description

When you define attributes within your classes and want to use the predicate-style methods (i.e. "def foo?; end"), then you have to define these yourself. This is a repetitive task especially if you want to query attributes that may not necessarily contain just true/false. For example, an attribute may contain 0, or the string "false". In this case, you would need to do special checks to see whether or not the value is really false.

attribute_predicates makes it easy by automatically generating predicate-style methods for all attributes that are created using attr, attr_reader, attr_writer, and attr_accessor. In addition, there is support for ActiveRecord’s non-standard truth accessor implementation (see below).

All of these shortcuts have the same interface and meaning as you would normally find.

Usage

Ruby Attributes

attr

This method takes a symbol (the name of the attribute) and an optional argument for whether or not the attribute is writeable. For example,

  module Mod
    attr :is_okay, true
  end

is equivalent to:

  module Mod
    def is_okay
      @is_okay
    end

    def is_okay=(val)
      @is_okay = value
    end

    def is_okay?
      !is_okay.blank?
    end
  end

attr_reader

This method is equivalent to calling attr(symbol, false) on each symbol in turn. For example,

  module Mod
    attr_reader :is_good, :is_bad
  end

  Mod.instance_methods.sort   #=> ["is_bad", "is_bad?", "is_good", "is_good?"]

attr_writer

This method creates an accessor and predicate method for each symbol in turn. For example,

  module Mod
    attr_writer :is_good, :is_bad
  end

  Mod.instance_methods.sort   #=> ["is_bad=", "is_bad?", "is_good=", "is_good?"]

attr_accessor

This method is equivalent to calling attr(symbol, true) on each symbol in turn. For example,

  module Mod
    attr_accessor :is_good, :is_bad
  end

  Mod.instance_methods.sort   #=> ["is_bad", "is_bad=", "is_bad?", "is_good", "is_good=", "is_good?"]

ActiveRecord Attributes

The predicate method has a slightly more complex implementation for subclasses of ActiveRecord::Base. It is built from how ActiveRecord implemented querying attributes. The following lists show which values will return false/true:

For String, the following values return true:

  • "true"
  • "t"

For Integer, the following values return true:

  • 1

For all other types, the predicate will return false.

Testing

Before you can run any tests, the following gem must be installed:

  • plugin_test_helper

To run against a specific version of Rails:

  rake test RAILS_FRAMEWORK_ROOT=/path/to/rails

Dependencies

  • Rails 2.0 or later

References

  • Yurii Rashkovskii - Boolean Attributes in Ruby
  • Evan Weaver - truth accessors in rails
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server