Sha256: 6aeb6ead6846eb0d19b0ea052e56ac22ae1d2e14eaad5c4805945bae1b9aa404

Contents?: true

Size: 739 Bytes

Versions: 5

Compression:

Stored size: 739 Bytes

Contents

module Disposable::Twin::Finders
  def find(*args)
    finders.find(*args)
  end

  # Use Song::Twin.finders.where(..) or whatever finder/scope is defined in the model.
  # It will return each model wrapped in a Twin.
  def finders
    FinderBuilder.new(self, _model)
  end

  class FinderBuilder
    def initialize(*args)
      @twin_class, @model_class = *args
    end

  private
    def method_missing(*args, &block)
      models = execute(*args, &block)

      return @twin_class.new(models) unless models.respond_to?(:each) # sorry for all the magic, but that's how ActiveRecord works.
      models.collect { |mdl| @twin_class.new(mdl) }
    end

    def execute(*args, &block)
      @model_class.send(*args, &block)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
disposable-0.0.9 lib/disposable/twin/finders.rb
disposable-0.0.8 lib/disposable/twin/finders.rb
disposable-0.0.7 lib/disposable/twin/finders.rb
disposable-0.0.6 lib/disposable/twin/finders.rb
disposable-0.0.5 lib/disposable/twin/finders.rb