123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Key::Single

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Key
Instance Chain:
self, Key
Inherits: Key
  • ::Object
Defined in: activerecord/lib/active_record/key.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name) ⇒ Single

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 126

def initialize(name)
  @name = name.is_a?(Symbol) ? name.name : -name.to_s
  @columns = [@name].freeze
end

Instance Attribute Details

#composite? ⇒ Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 172

def composite?
  false
end

Instance Method Details

#_value_of(record, normalizers = nil)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 199

def _value_of(record, normalizers = nil)
  value = record._read_attribute(@name)
  normalizer = normalizers&.first
  normalizer ? normalizer.call(value) : value
end

#all?(pattern = (no_pattern = true)) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 137

def all?(pattern = (no_pattern = true))
  return super(pattern) unless no_pattern
  return super() unless block_given?
  yield(@name) ? true : false
end

#any?(pattern = (no_pattern = true)) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 143

def any?(pattern = (no_pattern = true))
  return super(pattern) unless no_pattern
  return super() unless block_given?
  yield(@name) ? true : false
end

#arel_columns(table)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 180

def arel_columns(table)
  table[@name]
end

#cast(value, model)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 184

def cast(value, model)
  model.type_for_attribute(@name).cast(value)
end

#each {|@name| ... }

Yields:

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 131

def each
  return super unless block_given?
  yield @name
  self
end

#each_with_index {|@name, 0| ... }

Yields:

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 155

def each_with_index
  return super unless block_given?
  yield @name, 0
  @columns
end

#expects_multiple_ids?(value) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 205

def expects_multiple_ids?(value)
  value.is_a?(Array)
end

#include?(column) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 222

def include?(column)
  @name == column
end

#inferred_id

Only composite keys have a single id to infer.

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 210

def inferred_id
  nil
end

#inferred_key

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 214

def inferred_key
  nil
end

#map

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 161

def map
  return super unless block_given?
  [yield(@name)]
end

#map_value {|@name| ... }

Yields:

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 188

def map_value
  return enum_for(:map_value) unless block_given?
  yield @name
end

#none?(pattern = (no_pattern = true)) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 149

def none?(pattern = (no_pattern = true))
  return super(pattern) unless no_pattern
  return super() unless block_given?
  yield(@name) ? false : true
end

#resolve_aliases(owner)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 230

def resolve_aliases(owner)
  aliaz = owner.resolve_alias(@name)

  if aliaz == @name
    self
  else
    Key.for(aliaz)
  end
end

#subset?(other) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 226

def subset?(other)
  other.include?(@name)
end

#value_of(record, normalizers = nil)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 193

def value_of(record, normalizers = nil)
  value = record.read_attr(@name)
  normalizer = normalizers&.first
  normalizer ? normalizer.call(value) : value
end

#where_clauses(values)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 218

def where_clauses(values)
  [where_hash(values)]
end

#where_hash(values)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 176

def where_hash(values)
  { @name => values }
end

#zip(other) {|@name, | ... }

Yields:

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 166

def zip(other)
  return [[@name, other[0]]] unless block_given?
  yield @name, other[0]
  nil
end