Sha256: e85d6cc2580cccddbcd46606e872b72a49cb62cc7c11a934c2d324328024c480
Contents?: true
Size: 1.5 KB
Versions: 2
Compression:
Stored size: 1.5 KB
Contents
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}")) == true end end end end end ActiveRecord::Base.class_eval do extend AttributePredicates::Extensions::ActiveRecord end if defined?(ActiveRecord)
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
attribute_predicates-0.2.1 | lib/attribute_predicates/extensions/active_record.rb |
attribute_predicates-0.2.0 | lib/attribute_predicates/extensions/active_record.rb |