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) ⇒ 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
191 192 193 194 195 196 |
# File 'lib/cistern/service.rb', line 191 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) ⇒ 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
198 199 200 |
# File 'lib/cistern/service.rb', line 198 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 |
# File 'lib/cistern/service.rb', line 144 def setup_requirements @required ||= false unless @required models.each do |model, | require File.join(@model_path, model.to_s) unless [: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, | unless [: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 (={}) = 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 |