Giter VIP home page Giter VIP logo

attribute_predicates's Introduction

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

Bugs

Development

Testing

Source

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

Mailing List

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:

To run against a specific version of Rails:

rake test RAILS_FRAMEWORK_ROOT=/path/to/rails

Dependencies

  • Rails 2.0 or later

References

attribute_predicates's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.