Module: ActiveAttr::QueryAttributes

Extended by:
ActiveSupport::Concern
Includes:
Attributes
Included in:
Model
Defined in:
lib/active_attr/query_attributes.rb

Overview

QueryAttributes provides instance methods for querying attributes.

Examples:

Usage

class Person
  include ActiveAttr::QueryAttributes
  attribute :name
end

person = Person.new
person.name? #=> false
person.name = "Chris Griego"
person.name? #=> true

Since:

  • 0.3.0

Instance Method Summary collapse

Methods included from Attributes

#==, #attributes, filter_attributes, filter_attributes=, #inspect, #read_attribute, #write_attribute

Instance Method Details

#query_attribute(name) ⇒ true, false

Test the presence of an attribute

See Typecasting::BooleanTypecaster#call for more details.

Examples:

Query an attribute

person.query_attribute(:name)

Parameters:

  • name (String, Symbol, #to_s)

    The name of the attribute to query

Returns:

  • (true, false)

    The presence of the attribute

Raises:

Since:

  • 0.3.0



44
45
46
47
48
49
50
# File 'lib/active_attr/query_attributes.rb', line 44

def query_attribute(name)
  if respond_to? "#{name}?"
    send "#{name}?"
  else
    raise UnknownAttributeError, "unknown attribute: #{name}"
  end
end