Class: Cistern::Service
- Inherits:
-
Object
- Object
- Cistern::Service
- Defined in:
- lib/cistern/service.rb
Defined Under Namespace
Modules: Collections
Class Method Summary collapse
- .collection(collection_name, options = {}) ⇒ Object
- .collection_path(collection_path = nil) ⇒ Object
- .collections ⇒ Object
- .inherited(klass) ⇒ Object
- .mock! ⇒ Object
- .mocked_requests ⇒ Object
- .mocking? ⇒ Boolean
- .model(model_name, options = {}) ⇒ Object
- .model_path(model_path = nil) ⇒ Object
- .models ⇒ Object
- .new(options = {}) ⇒ Object
- .recognized_arguments ⇒ Object
- .recognizes(*args) ⇒ Object
- .request(request_name, options = {}) ⇒ Object
- .request_path(request_path = nil) ⇒ Object
- .requests ⇒ Object
- .required_arguments ⇒ Object
- .requires(*args) ⇒ Object
- .reset! ⇒ Object
- .setup_requirements ⇒ Object
- .unmock! ⇒ Object
- .validate_options(options = {}) ⇒ Object
Class Method Details
.collection(collection_name, options = {}) ⇒ Object
124 125 126 |
# File 'lib/cistern/service.rb', line 124 def collection(collection_name, ={}) collections << [collection_name, ] 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 |
.collections ⇒ Object
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_requests ⇒ Object
116 117 118 |
# File 'lib/cistern/service.rb', line 116 def mocked_requests @mocked_requests ||= [] end |
.mocking? ⇒ 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, ={}) models << [model_name, ] 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 |
.models ⇒ Object
88 89 90 |
# File 'lib/cistern/service.rb', line 88 def models @models ||= [] end |
.new(options = {}) ⇒ Object
226 227 228 229 230 231 |
# File 'lib/cistern/service.rb', line 226 def new(={}) () setup_requirements self.const_get(self.mocking? ? :Mock : :Real).new() end |
.recognized_arguments ⇒ Object
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, options = {}) ⇒ 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 |
.requests ⇒ Object
100 101 102 |
# File 'lib/cistern/service.rb', line 100 def requests @requests ||= [] end |
.required_arguments ⇒ Object
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
233 234 235 |
# File 'lib/cistern/service.rb', line 233 def reset! self.const_get(:Mock).reset! end |
.setup_requirements ⇒ Object
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/cistern/service.rb', line 144 def setup_requirements @required ||= false unless @required # setup models models.each do |model, | unless [:require] == false require([:require] || File.join(@model_path, model.to_s)) end class_name = [:class] || model.to_s.split("_").map(&:capitalize).join singular_name = [:model] || model.to_s.gsub("/", "_") self.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__ def #{singular_name}(attributes={}) #{service}::#{class_name}.new({connection: self}.merge(attributes)) end EOS end # setup requests requests.each do |request, | unless ([:require] == false) || [:new] || service::Real.method_defined?(request.to_s) require([:require] || File.join(@request_path, request.to_s)) end if [:new] klass = .fetch(:class) method = .fetch(:method) unless klass.instance_methods.include?(:mock) klass.class_eval <<-EOS, __FILE__, __LINE__ def #{method}(*args) Cistern::Mock.not_implemented(request) end EOS end service::Real.module_eval <<-EOS, __FILE__, __LINE__ def #{method}(*args) #{klass}.new(self)._real(*args) end EOS service::Mock.module_eval <<-EOS, __FILE__, __LINE__ def #{method}(*args) #{klass}.new(self)._mock(*args) end EOS elsif 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 # setup collections collections.each do |collection, | unless false == [:require] require([:require] || File.join(@collection_path || @model_path, collection.to_s)) end class_name = collection.to_s. split("/").map(&:capitalize).join("::").split("_"). map { |s| "#{s[0].upcase}#{s[1..-1]}" }.join plural_name = [:collection] || collection.to_s.gsub("/", "_") self.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__ def #{plural_name}(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 (={}) = Cistern::Hash.slice(, *required_arguments) = required_arguments - .keys unless .empty? raise "Missing required options: #{.inspect}" end = .keys - (required_arguments + recognized_arguments) unless .empty? raise "Unrecognized options: #{.inspect}" end end |