Module: Cistern::Client

Defined in:
lib/cistern/client.rb

Defined Under Namespace

Modules: ClassMethods, Collections

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

vanilla include



30
31
32
33
34
# File 'lib/cistern/client.rb', line 30

def self.included(klass)
  setup(klass)

  super
end

.setup(klass, options = {}) ⇒ Object



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cistern/client.rb', line 36

def self.setup(klass, options = {})
  request_class    = options[:request] || 'Request'
  collection_class = options[:collection] || 'Collection'
  model_class      = options[:model] || 'Model'
  singular_class   = options[:singular] || 'Singular'

  interface = options[:interface] || :class
  interface_callback = (:class == interface) ? :inherited : :included

  unless klass.name
    fail ArgumentError, "can't turn anonymous class into a Cistern service"
  end

  klass.class_eval <<-EOS, __FILE__, __LINE__
    module Collections
      include ::Cistern::Client::Collections

      def service
        #{klass.name}
      end
    end

    def self.service
      #{klass.name}
    end

    class Real
      def initialize(options={})
      end
    end

    class Mock
      def initialize(options={})
      end
    end

    #{interface} #{model_class}
      def self.#{interface_callback}(klass)
        service.models << klass

        klass.send(:include, ::Cistern::Model)

        super
      end

      def self.service
        #{klass.name}
      end
    end

    #{interface} #{singular_class}
      def self.#{interface_callback}(klass)
        service.singularities << klass

        klass.send(:include, ::Cistern::Singular)

        super
      end

      def self.service
        #{klass.name}
      end
    end

    #{interface} #{collection_class}
      include ::Cistern::Collection

      def self.#{interface_callback}(klass)
        klass.send(:extend, Cistern::Attributes::ClassMethods)
        klass.send(:extend, Cistern::Collection::ClassMethods)
        klass.send(:include, Cistern::Attributes::InstanceMethods)

        service.collections << klass

        super
      end

      def self.service
        #{klass.name}
      end
    end

    #{interface} #{request_class}
      include ::Cistern::Request

      def self.service
        #{klass.name}
      end

      def self.#{interface_callback}(klass)
        klass.extend(::Cistern::Request::ClassMethods)

        service.requests << klass

        super
      end

      def _mock(*args)
        mock(*args)
      end

      def _real(*args)
        real(*args)
      end
    end
  EOS

  klass.send(:extend, Cistern::Client::ClassMethods)
  klass.send(:const_set, :Timeout, Class.new(Cistern::Error))

  klass::Mock.send(:include, klass::Collections)
  klass::Mock.send(:extend, Cistern::WaitFor)
  klass::Mock.timeout_error = klass::Timeout

  klass::Mock.send(:extend, Cistern::Data)

  klass::Real.send(:include, klass::Collections)
  klass::Real.send(:extend, Cistern::WaitFor)
  klass::Real.timeout_error = klass::Timeout
end

.with(options = {}) ⇒ Object

custom include



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cistern/client.rb', line 13

def self.with(options = {})
  client_module = Module.new

  custom_include = <<-EOS
    def self.included(klass)
      Cistern::Client.setup(klass, #{options.inspect})

      super
    end
  EOS

  client_module.class_eval(custom_include, __FILE__, __LINE__)

  client_module
end