aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support/model.rb
blob: 45fe254d6ff201449e08edd4a25cfbd3ada33df8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Arel
  module Testing
    class Engine < Arel::Memory::Engine
      attr_reader :rows

      def initialize
        @rows = []
      end

      def supports(operation)
        false
      end

      def read(relation)
        case relation
        when Arel::Take, Arel::Order, Arel::Skip, Arel::Where
          relation.eval
        else
          @rows.dup.map { |r| Row.new(relation, r) }
        end
      end

      def create(insert)
        @rows << insert.record.tuple
        insert
      end
    end
  end

  class Model
    include Relation

    attr_reader :engine

    def self.build
      relation = new
      yield relation
      relation
    end

    def initialize
      @attributes = []
    end

    def engine(engine = nil)
      @engine = engine if engine
      @engine
    end

    def attribute(name, type)
      @attributes << type.new(self, name)
    end

    def attributes
      Header.new(@attributes)
    end

    def format(attribute, value)
      value
    end

    def insert(row)
      insert = super Arel::Row.new(self, row)
      insert.record
    end
  end
end