Class: Cistern::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/cistern/service.rb

Defined Under Namespace

Modules: Collections

Class Method Summary collapse

Class Method Details

.collection(collection_name, options = {}) ⇒ Object



124
125
126
# File 'lib/cistern/service.rb', line 124

def collection(collection_name, options={})
  collections << [collection_name, options]
end

.collection_path(collection_path = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/cistern/service.rb', line 60

def collection_path(collection_path = nil)
  if collection_path
    @collection_path = collection_path
  else
    @collection_path
  end
end

.collectionsObject



84
85
86
# File 'lib/cistern/service.rb', line 84

def collections
  @collections ||= []
end

.inherited(klass) ⇒ Object



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
# File 'lib/cistern/service.rb', line 22

def inherited(klass)
  klass.class_eval <<-EOS, __FILE__, __LINE__
    module Collections
      include Cistern::Service::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
  EOS

  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

.mock!Object



3
# File 'lib/cistern/service.rb', line 3

def self.mock!;    @mocking = true; end

.mocked_requestsObject



116
117
118
# File 'lib/cistern/service.rb', line 116

def mocked_requests
  @mocked_requests ||= []
end

.mocking?Boolean

Returns:

  • (Boolean)


4
# File 'lib/cistern/service.rb', line 4

def self.mocking?; @mocking; end

.model(model_name, options = {}) ⇒ Object



112
113
114
# File 'lib/cistern/service.rb', line 112

def model(model_name, options={})
  models << [model_name, options]
end

.model_path(model_path = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/cistern/service.rb', line 68

def model_path(model_path = nil)
  if model_path
    @model_path = model_path
  else
    @model_path
  end
end

.modelsObject



88
89
90
# File 'lib/cistern/service.rb', line 88

def models
  @models ||= []
end

.new(options = {}) ⇒ Object



191
192
193
194
195
196
# File 'lib/cistern/service.rb', line 191

def new(options={})
  validate_options(options)
  setup_requirements

  self.const_get(self.mocking? ? :Mock : :Real).new(options)
end

.recognized_argumentsObject



92
93
94
# File 'lib/cistern/service.rb', line 92

def recognized_arguments
  @recognized_arguments ||= []
end

.recognizes(*args) ⇒ Object



108
109
110
# File 'lib/cistern/service.rb', line 108

def recognizes(*args)
  self.recognized_arguments.concat(args)
end

.request(request_name) ⇒ Object



120
121
122
# File 'lib/cistern/service.rb', line 120

def request(request_name)
  requests << request_name
end

.request_path(request_path = nil) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/cistern/service.rb', line 76

def request_path(request_path = nil)
  if request_path
    @request_path = request_path
  else
    @request_path
  end
end

.requestsObject



100
101
102
# File 'lib/cistern/service.rb', line 100

def requests
  @requests ||= []
end

.required_argumentsObject



96
97
98
# File 'lib/cistern/service.rb', line 96

def required_arguments
  @required_arguments ||= []
end

.requires(*args) ⇒ Object



104
105
106
# File 'lib/cistern/service.rb', line 104

def requires(*args)
  self.required_arguments.concat(args)
end

.reset!Object



198
199
200
# File 'lib/cistern/service.rb', line 198

def reset!
  self.const_get(:Mock).reset!
end

.setup_requirementsObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cistern/service.rb', line 144

def setup_requirements
  @required ||= false
  unless @required
    models.each do |model, options|
      require File.join(@model_path, model.to_s) unless options[:require] == false
      class_name = model.to_s.split("_").map(&:capitalize).join
      self.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__
        def #{model}(attributes={})
          #{service}::#{class_name}.new({connection: self}.merge(attributes))
        end
      EOS
    end
    requests.each do |request|
      unless service::Real.method_defined?(request.to_s)
        require File.join(@request_path, request.to_s)
      end

      if service::Mock.method_defined?(request)
        mocked_requests << request
      else
        service::Mock.module_eval <<-EOS, __FILE__, __LINE__
          def #{request}(*args)
            Cistern::Mock.not_implemented(request)
          end
        EOS
      end
    end
    collections.each do |collection, options|
      unless options[:require] == false
        if @collection_path
          require File.join(@collection_path, collection.to_s)
        else
          require File.join(@model_path, collection.to_s)
        end
      end

      class_name = collection.to_s.split("_").map(&:capitalize).join
      self.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__
        def #{collection}(attributes={})
          #{service}::#{class_name}.new({connection: self}.merge(attributes))
        end
      EOS
    end
    @required = true
  end
end

.unmock!Object



5
# File 'lib/cistern/service.rb', line 5

def self.unmock!;  @mocking = false; end

.validate_options(options = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cistern/service.rb', line 128

def validate_options(options={})
  required_options = Cistern::Hash.slice(options, *required_arguments)

  missing_required_options = required_arguments - required_options.keys

  unless missing_required_options.empty?
    raise "Missing required options: #{missing_required_options.inspect}"
  end

  unrecognized_options = options.keys - (required_arguments + recognized_arguments)

  unless unrecognized_options.empty?
    raise "Unrecognized options: #{unrecognized_options.inspect}"
  end
end