class Animator::FeyRelation

Attributes

klass[R]
transaction_uuid[R]
where_values_hash[R]

Public Class Methods

new(klass, transaction_uuid = nil, where_values_hash = {}, &block) click to toggle source
# File lib/animator/fey_relation.rb, line 7
def initialize(klass, transaction_uuid = nil, where_values_hash = {}, &block)
  @transaction_uuid = transaction_uuid
  @where_values_hash = where_values_hash
  @klass = klass

  instance_exec(&block) if block
end

Public Instance Methods

==(other) click to toggle source
# File lib/animator/fey_relation.rb, line 147
def ==(other)
  case other
  when FeyRelation
    eraminho_relation.to_sql == other.eraminho_relation.to_sql
  when Array
    to_a == other
  end
end
all() click to toggle source
# File lib/animator/fey_relation.rb, line 40
def all
  spawn
end
count() click to toggle source
# File lib/animator/fey_relation.rb, line 100
def count
  eraminho_relation.count
end
divine(options = {}, &block) click to toggle source
# File lib/animator/fey_relation.rb, line 194
def divine(options = {}, &block)
  options = { validate: false }.merge(options)

  result = nil

  @klass.transaction do
    reanimate_all!(options)
    result = to_active_record_relation.instance_exec(&block)
    raise ActiveRecord::Rollback
  end

  result
end
exists?(where_values_hash = nil) click to toggle source
# File lib/animator/fey_relation.rb, line 92
def exists?(where_values_hash = nil)
  if where_values_hash
    where(where_values_hash).exists?
  else
    eraminho_relation.exists?
  end
end
find(id) click to toggle source
# File lib/animator/fey_relation.rb, line 64
def find(id)
  find_by!(@klass.primary_key => id)
end
find_by(where_values_hash) click to toggle source
# File lib/animator/fey_relation.rb, line 56
def find_by(where_values_hash)
  where(where_values_hash).take
end
find_by!(where_values_hash) click to toggle source
# File lib/animator/fey_relation.rb, line 60
def find_by!(where_values_hash)
  where(where_values_hash).take!
end
find_each(options = {}, &block) click to toggle source
# File lib/animator/fey_relation.rb, line 128
def find_each(options = {}, &block)
  eraminho_relation.find_each(options) { |eraminho| block.call(eraminho.animable!) }
end
first(limit = nil) click to toggle source
# File lib/animator/fey_relation.rb, line 68
def first(limit = nil)
  if limit
    eraminho_relation.first(limit).map(&:animable)
  else
    eraminho_relation.first.try(:animable)
  end
end
first!() click to toggle source
# File lib/animator/fey_relation.rb, line 76
def first!
  first || raise(ActiveRecord::RecordNotFound)
end
ids() click to toggle source
# File lib/animator/fey_relation.rb, line 124
def ids
  eraminho_relation.pluck(:animable_id)
end
inspect() click to toggle source
# File lib/animator/fey_relation.rb, line 140
def inspect
  entries = to_a.take(11).map!(&:inspect)
  entries[10] = '...' if entries.size == 11

  "#<#{self.class.name} [#{entries.join(', ')}]>"
end
last(limit = nil) click to toggle source
# File lib/animator/fey_relation.rb, line 80
def last(limit = nil)
  if limit
    eraminho_relation.last(limit).map(&:animable)
  else
    eraminho_relation.last.try(:animable)
  end
end
last!() click to toggle source
# File lib/animator/fey_relation.rb, line 88
def last!
  last || raise(ActiveRecord::RecordNotFound)
end
limit(limit_value) click to toggle source
# File lib/animator/fey_relation.rb, line 112
def limit(limit_value)
  spawn do
    @limit_value = limit_value
  end
end
loaded?() click to toggle source
# File lib/animator/fey_relation.rb, line 136
def loaded?
  eraminho_relation.loaded?
end
merge(relation) click to toggle source
# File lib/animator/fey_relation.rb, line 23
def merge(relation)
  if relation.is_a?(ActiveRecord::Relation)
    raise(NotImplementedError, "#{self.class.name} does not support joins.") if relation.joins_values.any? || relation.includes_values.any? || relation.joined_includes_values.any?
    raise(NotImplementedError, "#{self.class.name} does not support custom select.") if relation.select_values.any?
    raise(NotImplementedError, "#{self.class.name} does not support aggragation.") if relation.group_values.any? || relation.having_values.any?
  end

  if relation.respond_to?(:klass, false)
    raise('Class mismatch while attempting to merge into #{self.class.name}') unless relation.klass == @klass
  end

  where(relation.where_values_hash) do
    @limit_value = relation.limit_value if relation.respond_to?(:limit_value, false)
    @offset_value = relation.offset_value if relation.respond_to?(:offset_value, false)
  end
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/animator/fey_relation.rb, line 212
def method_missing(method_name, *args, &block)
  if @klass.respond_to?(method_name, false)
    result = to_active_record_relation.public_send(method_name, *args, &block)

    if result.respond_to?(:where_values_hash)
      result = merge(result)
    else
      result
    end
  elsif [].respond_to?(method_name, false)
    to_a.public_send(method_name, *args, &block)
  else
    super
  end
end
offset(offset_value) click to toggle source
# File lib/animator/fey_relation.rb, line 118
def offset(offset_value)
  spawn do
    @offset_value = offset_value
  end
end
pluck(*columns) click to toggle source
# File lib/animator/fey_relation.rb, line 104
def pluck(*columns)
  if columns.count > 1
    eraminho_relation.map { |eraminho| columns.map { |column| eraminho.animable!.public_send(column) } }
  else
    eraminho_relation.map { |eraminho| eraminho.animable!.public_send(columns.first) }
  end
end
reanimate_all(options = {}) click to toggle source
# File lib/animator/fey_relation.rb, line 186
def reanimate_all(options = {})
  reanimate_all! rescue false
end
reanimate_all!(options = {}) click to toggle source
# File lib/animator/fey_relation.rb, line 156
def reanimate_all!(options = {})
  options = { force: false, transactional: true, validate: true }.merge(options)

  validation_queue = []

  Eraminho.transaction do
    if options[:transactional]
      Eraminho.with_transaction_uuid(eraminho_relation.pluck(:transaction_uuid))
    else
      eraminho_relation
    end.find_each do |eraminho|
      if options[:force]
        eraminho.animable!.reanimate(options.merge(transactional: false, force: false), validation_queue)
      else
        eraminho.animable!.reanimate!(options.merge(transactional: false, force: false), validation_queue)
      end
    end

    validation_queue.each do |animable| 
      unless animable.valid?(:reanimate)
        raise(ActiveRecord::RecordInvalid.new(animable))
      end
    end

    reset
  end

  true
end
reset() click to toggle source
# File lib/animator/fey_relation.rb, line 132
def reset
  eraminho_relation.reset
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/animator/fey_relation.rb, line 208
def respond_to_missing?(method_name, include_private = false)
  @klass.respond_to?(method_name, false) || [].respond_to?(method_name, false) || super
end
spawn(&block) click to toggle source
# File lib/animator/fey_relation.rb, line 15
def spawn(&block)
  self.class.new(@klass, @transaction_uuid, @where_values_hash, &block)
end
take() click to toggle source
# File lib/animator/fey_relation.rb, line 48
def take
  eraminho_relation.take.try(:animable)
end
take!() click to toggle source
# File lib/animator/fey_relation.rb, line 52
def take!
  take || raise(ActiveRecord::RecordNotFound)
end
to_a() click to toggle source
# File lib/animator/fey_relation.rb, line 44
def to_a
  eraminho_relation.map(&:animable)
end
to_active_record_relation() click to toggle source
# File lib/animator/fey_relation.rb, line 190
def to_active_record_relation
  @_active_record_relation ||= @klass.where(@where_values_hash)
end
where(where_values_hash, &block) click to toggle source
# File lib/animator/fey_relation.rb, line 19
def where(where_values_hash, &block)
  self.class.new(@klass, @transaction_uuid, @where_values_hash.merge(where_values_hash), &block)
end

Protected Instance Methods

eraminho_relation() click to toggle source
# File lib/animator/fey_relation.rb, line 230
def eraminho_relation
  unless @eraminho_relation
    @eraminho_relation = Eraminho.with_animable_class(@klass.name)

    @eraminho_relation = @eraminho_relation.with_transaction_uuid(@transaction_uuid) if @transaction_uuid

    animable_id = @where_values_hash[@klass.primary_key] || @where_values_hash[@klass.primary_key.to_sym]
    animable_attributes = @where_values_hash.reject { |key| key == @klass.primary_key || key == @klass.primary_key.to_sym }

    @eraminho_relation = @eraminho_relation.with_animable_id(animable_id) if animable_id
    @eraminho_relation = @eraminho_relation.with_animable_attributes(animable_attributes)

    @eraminho_relation = @eraminho_relation.limit(@limit_value) if @limit_value
    @eraminho_relation = @eraminho_relation.offset(@offset_value) if @offset_value
  end

  @eraminho_relation
end