Sha256: fd0caf5e2da57c9cc1c1f2a04004d88fd70f231289a9bb22f9a327727ee3e627

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module PluginAWeek #:nodoc:
  module AttributePredicates
    module Extensions
      # Adds support for automatically defining predicate methods using +attr_predicate+
      # when defining attributes using +attr+, +attr_reader+, +attr_reader+, and
      # +attr_accessor+.  In comparison to normal Ruby attributes, ActiveRecord
      # predicates use a different system for defining true/false.
      # 
      # == Examples
      # 
      # The predicate methods for attributes use ActiveRecord's type conversion
      # for booleans for determing whether to return true or false.  For example,
      # 
      #   class Person < ActiveRecord::Base
      #     attr_accessor :value
      #   end
      #   
      #   p = Person.new
      #   p.value = false
      #   p.value?    # => false
      #   
      #   p.value = 'false'
      #   p.value?    # => false
      #   
      #   p.value = 'true'
      #   p.value?    # => true
      #   
      #   p.value = 't'
      #   p.value?    # => true
      #   
      #   p.value = 1
      #   p.value?    # => true
      module ActiveRecord
        private
          # For Strings, returns true when value is:
          # * "true"
          # * "t"
          # 
          # For Integers, returns true when value is:
          # * 1
          def attr_predicate(symbol)
            define_method("#{symbol}?") do
              ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(instance_variable_get("@#{symbol}"))
            end
          end
      end
    end
  end
end

ActiveRecord::Base.class_eval do
  extend PluginAWeek::AttributePredicates::Extensions::ActiveRecord
end if defined?(ActiveRecord)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attribute_predicates-0.1.1 lib/attribute_predicates/extensions/active_record.rb