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) ⇒ Object
- .collections ⇒ Object
- .inherited(klass) ⇒ Object
- .mock! ⇒ Object
- .mocked_requests ⇒ Object
- .mocking? ⇒ Boolean
- .model(model_name, options = {}) ⇒ Object
- .model_path(model_path) ⇒ Object
- .models ⇒ Object
- .new(options = {}) ⇒ Object
- .recognized_arguments ⇒ Object
- .recognizes(*args) ⇒ Object
- .request(request_name) ⇒ Object
- .request_path(request_path) ⇒ 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
105 106 107 |
# File 'lib/cistern/service.rb', line 105 def collection(collection_name, ={}) collections << [collection_name, ] end |
.collection_path(collection_path) ⇒ Object
53 54 55 |
# File 'lib/cistern/service.rb', line 53 def collection_path(collection_path) @collection_path = collection_path end |
.collections ⇒ Object
65 66 67 |
# File 'lib/cistern/service.rb', line 65 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 |
# File 'lib/cistern/service.rb', line 22 def inherited(klass) klass.class_eval " module Collections\n include Cistern::Service::Collections\n\n def service\n \#{klass.name}\n end\n end\n\n def self.service\n \#{klass.name}\n end\n\n class Real\n end\n\n class Mock\n end\n EOS\n\n klass.send(:const_set, :Timeout, Class.new(Cistern::Error))\n\n klass::Mock.send(:include, klass::Collections)\n klass::Mock.send(:extend, Cistern::WaitFor)\n klass::Mock.timeout_error = klass::Timeout\n klass::Real.send(:include, klass::Collections)\n klass::Real.send(:extend, Cistern::WaitFor)\n klass::Real.timeout_error = klass::Timeout\nend\n", __FILE__, __LINE__ |
.mock! ⇒ Object
3 |
# File 'lib/cistern/service.rb', line 3 def self.mock!; @mocking = true; end |
.mocked_requests ⇒ Object
97 98 99 |
# File 'lib/cistern/service.rb', line 97 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
93 94 95 |
# File 'lib/cistern/service.rb', line 93 def model(model_name, ={}) models << [model_name, ] end |
.model_path(model_path) ⇒ Object
57 58 59 |
# File 'lib/cistern/service.rb', line 57 def model_path(model_path) @model_path = model_path end |
.models ⇒ Object
69 70 71 |
# File 'lib/cistern/service.rb', line 69 def models @models ||= [] end |
.new(options = {}) ⇒ Object
166 167 168 169 170 171 |
# File 'lib/cistern/service.rb', line 166 def new(={}) () setup_requirements self.const_get(self.mocking? ? :Mock : :Real).new() end |
.recognized_arguments ⇒ Object
73 74 75 |
# File 'lib/cistern/service.rb', line 73 def recognized_arguments @recognized_arguments ||= [] end |
.recognizes(*args) ⇒ Object
89 90 91 |
# File 'lib/cistern/service.rb', line 89 def recognizes(*args) self.recognized_arguments.concat(args) end |
.request(request_name) ⇒ Object
101 102 103 |
# File 'lib/cistern/service.rb', line 101 def request(request_name) requests << request_name end |
.request_path(request_path) ⇒ Object
61 62 63 |
# File 'lib/cistern/service.rb', line 61 def request_path(request_path) @request_path = request_path end |
.requests ⇒ Object
81 82 83 |
# File 'lib/cistern/service.rb', line 81 def requests @requests ||= [] end |
.required_arguments ⇒ Object
77 78 79 |
# File 'lib/cistern/service.rb', line 77 def required_arguments @required_arguments ||= [] end |
.requires(*args) ⇒ Object
85 86 87 |
# File 'lib/cistern/service.rb', line 85 def requires(*args) self.required_arguments.concat(args) end |
.reset! ⇒ Object
173 174 175 |
# File 'lib/cistern/service.rb', line 173 def reset! self.const_get(:Mock).reset! end |
.setup_requirements ⇒ Object
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 156 157 158 159 160 161 162 163 164 |
# File 'lib/cistern/service.rb', line 122 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 " def \#{model}(attributes={})\n \#{service}::\#{class_name}.new({connection: self}.merge(attributes))\n end\n EOS\n end\n requests.each do |request|\n require File.join(@request_path, request.to_s)\n if service::Mock.method_defined?(request)\n mocked_requests << request\n else\n service::Mock.module_eval <<-EOS, __FILE__, __LINE__\n def \#{request}(*args)\n Cistern::Mock.not_implemented(request)\n end\n EOS\n end\n end\n collections.each do |collection, options|\n unless options[:require] == false\n if @collection_path\n require File.join(@collection_path, collection.to_s)\n else\n require File.join(@model_path, collection.to_s)\n end\n end\n\n class_name = collection.to_s.split(\"_\").map(&:capitalize).join\n self.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__\n def \#{collection}(attributes={})\n \#{service}::\#{class_name}.new({connection: self}.merge(attributes))\n end\n EOS\n end\n @required = true\n end\nend\n", __FILE__, __LINE__ |
.unmock! ⇒ Object
5 |
# File 'lib/cistern/service.rb', line 5 def self.unmock!; @mocking = false; end |
.validate_options(options = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cistern/service.rb', line 109 def (={}) = Cistern::Hash.slice(, *required_arguments) = required_arguments - .keys unless .empty? raise "Missing required options: #{missing_required_options.inspect}" end = Cistern::Hash.slice(, *(required_arguments + recognized_arguments)) = .keys - (required_arguments + recognized_arguments) unless .empty? raise "Unrecognized options: #{unrecognized_options.inspect}" end end |