Class: ActiveRecord::Relation::QueryAttribute

Inherits:
ActiveModel::Attribute show all
Defined in:
activerecord/lib/active_record/relation/query_attribute.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from ActiveModel::Attribute

#name, #type, #value_before_type_cast

Instance Method Summary collapse

Methods inherited from ActiveModel::Attribute

#came_from_user?, #changed?, #changed_in_place?, #dup_or_share, #encode_with, #forgetting_assignment, from_database, from_user, #has_been_read?, #init_with, #initialized?, null, #original_value, #original_value_for_database, #serializable?, uninitialized, #value, with_cast_value, #with_type, #with_user_default, #with_value_from_database, #with_value_from_user

Constructor Details

#initializeQueryAttribute

Returns a new instance of QueryAttribute.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 8

def initialize(...)
  super

  # The query attribute value may be mutated before we actually "compile" the query.
  # To avoid that if the type uses a serializer we eagerly compute the value for database
  if value_before_type_cast.is_a?(StatementCache::Substitute)
    # we don't need to serialize StatementCache::Substitute
  elsif @type.serialized?
    value_for_database
  elsif @type.mutable? # If the type is simply mutable, we deep_dup it.
    unless @value_before_type_cast.frozen?
      @value_before_type_cast = @value_before_type_cast.deep_dup
    end
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



55
56
57
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 55

def ==(other)
  super && value_for_database == other.value_for_database
end

#hashObject



60
61
62
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 60

def hash
  [self.class, name, value_for_database, type].hash
end

#infinite?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 44

def infinite?
  infinity?(value_before_type_cast) || serializable? && infinity?(value_for_database)
end

#nil?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 37

def nil?
  unless value_before_type_cast.is_a?(StatementCache::Substitute)
    value_before_type_cast.nil? ||
      (type.respond_to?(:subtype) || type.respond_to?(:normalizer)) && serializable? && value_for_database.nil?
  end
end

#type_cast(value) ⇒ Object



24
25
26
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 24

def type_cast(value)
  value
end

#unboundable?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 48

def unboundable?
  unless defined?(@_unboundable)
    serializable? { |value| @_unboundable = value <=> 0 } && @_unboundable = nil
  end
  @_unboundable
end

#value_for_databaseObject



28
29
30
31
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 28

def value_for_database
  @value_for_database = _value_for_database unless defined?(@value_for_database)
  @value_for_database
end

#with_cast_value(value) ⇒ Object



33
34
35
# File 'activerecord/lib/active_record/relation/query_attribute.rb', line 33

def with_cast_value(value)
  QueryAttribute.new(name, value, type)
end