Module: Kong::Base::ClassMethods
- Defined in:
- lib/kong/base.rb
Instance Method Summary collapse
-
#create(attributes = {}) ⇒ Object
Create resource.
-
#find(id) ⇒ Object
Find resource.
-
#list(params = {}) ⇒ Array
(also: #all)
List resources.
- #method_missing(method, *arguments, &block) ⇒ Object
- #respond_to?(method, include_private = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kong/base.rb', line 32 def method_missing(method, *arguments, &block) if method.to_s.start_with?('find_by_') attribute = method.to_s.sub('find_by_', '') if self.attribute_names.include?(attribute) self.list({ attribute => arguments[0] })[0] else super end elsif method.to_s.start_with?('find_all_by_') attribute = method.to_s.sub('find_all_by_', '') if self.attribute_names.include?(attribute) self.list({ attribute => arguments[0] }) else super end else super end end |
Instance Method Details
#create(attributes = {}) ⇒ Object
Create resource
22 23 24 |
# File 'lib/kong/base.rb', line 22 def create(attributes = {}) self.new(attributes).create end |
#find(id) ⇒ Object
Find resource
28 29 30 |
# File 'lib/kong/base.rb', line 28 def find(id) self.new.get(id) end |
#list(params = {}) ⇒ Array Also known as: all
List resources
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/kong/base.rb', line 7 def list(params = {}) result = [] json_data = Client.instance.get(self::API_END_POINT, params) if json_data['data'] json_data['data'].each do |instance| result << self.new(instance) end end result end |
#respond_to?(method, include_private = false) ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/kong/base.rb', line 52 def respond_to?(method, include_private = false) if method.to_s.start_with?('find_by_') attribute = method.to_s.sub('find_by_', '') if self.attribute_names.include?(attribute) return true else super end elsif method.to_s.start_with?('find_all_by_') attribute = method.to_s.sub('find_all_by_', '') if self.attribute_names.include?(attribute) return true else super end else super end end |