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
92 93 94 |
# File 'lib/cistern/service.rb', line 92 def collection(collection_name, ={}) collections << [collection_name, ] end |
.collection_path(collection_path) ⇒ Object
40 41 42 |
# File 'lib/cistern/service.rb', line 40 def collection_path(collection_path) @collection_path = collection_path end |
.collections ⇒ Object
52 53 54 |
# File 'lib/cistern/service.rb', line 52 def collections @collections ||= [] end |
.inherited(klass) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 EOS\n\n klass.send(:const_set, :Timeout, Class.new(Cistern::Error))\nend\n", __FILE__, __LINE__ |
.mock! ⇒ Object
3 |
# File 'lib/cistern/service.rb', line 3 def self.mock!; @mocking = true; end |
.mocked_requests ⇒ Object
84 85 86 |
# File 'lib/cistern/service.rb', line 84 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
80 81 82 |
# File 'lib/cistern/service.rb', line 80 def model(model_name, ={}) models << [model_name, ] end |
.model_path(model_path) ⇒ Object
44 45 46 |
# File 'lib/cistern/service.rb', line 44 def model_path(model_path) @model_path = model_path end |
.models ⇒ Object
56 57 58 |
# File 'lib/cistern/service.rb', line 56 def models @models ||= [] end |
.new(options = {}) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/cistern/service.rb', line 153 def new(={}) () setup_requirements klass = self.const_get(self.mocking? ? :Mock : :Real) klass.send(:include, service::Collections) klass.send(:extend, Cistern::WaitFor) klass.timeout_error = service::Timeout klass.new() end |
.recognized_arguments ⇒ Object
60 61 62 |
# File 'lib/cistern/service.rb', line 60 def recognized_arguments @recognized_arguments ||= [] end |
.recognizes(*args) ⇒ Object
76 77 78 |
# File 'lib/cistern/service.rb', line 76 def recognizes(*args) self.recognized_arguments.concat(args) end |
.request(request_name) ⇒ Object
88 89 90 |
# File 'lib/cistern/service.rb', line 88 def request(request_name) requests << request_name end |
.request_path(request_path) ⇒ Object
48 49 50 |
# File 'lib/cistern/service.rb', line 48 def request_path(request_path) @request_path = request_path end |
.requests ⇒ Object
68 69 70 |
# File 'lib/cistern/service.rb', line 68 def requests @requests ||= [] end |
.required_arguments ⇒ Object
64 65 66 |
# File 'lib/cistern/service.rb', line 64 def required_arguments @required_arguments ||= [] end |
.requires(*args) ⇒ Object
72 73 74 |
# File 'lib/cistern/service.rb', line 72 def requires(*args) self.required_arguments.concat(args) end |
.reset! ⇒ Object
165 166 167 |
# File 'lib/cistern/service.rb', line 165 def reset! self.const_get(:Mock).reset! end |
.setup_requirements ⇒ Object
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 |
# File 'lib/cistern/service.rb', line 109 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
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cistern/service.rb', line 96 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 |