Class: Kobana::Resources::Base
Constant Summary
Constants included
from Connection
Connection::BASE_URI
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Operations
#delete, #find_command, #handle_error_response, included, #list_commands, #save, #update
Methods included from Connection
included
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
71
72
73
74
|
# File 'lib/kobana/resources/base.rb', line 71
def initialize(attributes = {})
@attributes = attributes.deep_symbolize_keys
@errors = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args) ⇒ Object
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/kobana/resources/base.rb', line 85
def method_missing(key, *args, &)
if key.to_s.end_with?("=")
key = key.to_s.chomp("=").to_sym
attributes[key] = args.first
else
return unless attributes.key?(key.to_sym)
attributes[key]
end
end
|
Class Attribute Details
.api_version ⇒ Object
Returns the value of attribute api_version.
13
14
15
|
# File 'lib/kobana/resources/base.rb', line 13
def api_version
@api_version
end
|
.client ⇒ Object
Returns the value of attribute client.
13
14
15
|
# File 'lib/kobana/resources/base.rb', line 13
def client
@client
end
|
.default_attributes ⇒ Object
Returns the value of attribute default_attributes.
13
14
15
|
# File 'lib/kobana/resources/base.rb', line 13
def default_attributes
@default_attributes
end
|
.errors ⇒ Object
Returns the value of attribute errors.
13
14
15
|
# File 'lib/kobana/resources/base.rb', line 13
def errors
@errors
end
|
.primary_key ⇒ Object
Returns the value of attribute primary_key.
13
14
15
|
# File 'lib/kobana/resources/base.rb', line 13
def primary_key
@primary_key
end
|
.resource_endpoint ⇒ Object
Returns the value of attribute resource_endpoint.
13
14
15
|
# File 'lib/kobana/resources/base.rb', line 13
def resource_endpoint
@resource_endpoint
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
69
70
71
|
# File 'lib/kobana/resources/base.rb', line 69
def attributes
@attributes
end
|
#errors ⇒ Object
Returns the value of attribute errors.
69
70
71
|
# File 'lib/kobana/resources/base.rb', line 69
def errors
@errors
end
|
Class Method Details
.infer_resource_endpoint(klass) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/kobana/resources/base.rb', line 35
def infer_resource_endpoint(klass)
return resource_endpoint if resource_endpoint
return unless klass.name =~ /Kobana::Resources::(.*)$/
::Regexp.last_match(1).underscore.pluralize
end
|
.inherited(subclass) ⇒ Object
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/kobana/resources/base.rb', line 24
def inherited(subclass)
super
subclass.resource_endpoint ||= infer_resource_endpoint(subclass)
subclass.primary_key ||= :uid
subclass.api_version = :v2 unless subclass.instance_variable_defined?(:@api_version)
subclass.errors ||= []
subclass.default_attributes ||= {}
end
|
.interpolate(template, attributes) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/kobana/resources/base.rb', line 47
def interpolate(template, attributes)
template.gsub(/\{([^}]+)\}/) do
key = Regexp.last_match(1)
begin
if key.include?(".")
value = attributes[key.tr(".", "_").to_sym].to_s
if value.empty?
keys = key.split(".").map(&:to_sym)
keys.reduce(attributes) { |acc, k| acc[k] }
else
value
end
else
attributes[key.to_sym].to_s
end
rescue NameError
""
end
end
end
|
.uri(attributes = {}) ⇒ Object
43
44
45
|
# File 'lib/kobana/resources/base.rb', line 43
def uri(attributes = {})
"#{base_url}/#{interpolate(resource_endpoint, default_attributes.merge(attributes))}"
end
|
.with_client(client) ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/kobana/resources/base.rb', line 15
def with_client(client)
@client_classes ||= {}.compare_by_identity
@client_classes[client] ||= begin
klass = Class.new(self)
klass.client = client
klass
end
end
|
Instance Method Details
#[](key) ⇒ Object
81
82
83
|
# File 'lib/kobana/resources/base.rb', line 81
def [](key)
attributes[key.to_sym]
end
|
#client ⇒ Object
Access to client configuration through class
77
78
79
|
# File 'lib/kobana/resources/base.rb', line 77
def client
self.class.client
end
|
#created? ⇒ Boolean
112
113
114
|
# File 'lib/kobana/resources/base.rb', line 112
def created?
attributes[:created] || false
end
|
#new_record? ⇒ Boolean
120
121
122
|
# File 'lib/kobana/resources/base.rb', line 120
def new_record?
primary_key.nil? || primary_key.to_s.empty?
end
|
#primary_key ⇒ Object
124
125
126
|
# File 'lib/kobana/resources/base.rb', line 124
def primary_key
attributes[self.class.primary_key]
end
|
#request ⇒ Object
104
105
106
|
# File 'lib/kobana/resources/base.rb', line 104
def request(*)
self.class.request(*)
end
|
#respond_to_missing?(key, include_private = false) ⇒ Boolean
96
97
98
|
# File 'lib/kobana/resources/base.rb', line 96
def respond_to_missing?(key, include_private = false)
attributes.key?(key.to_sym) || super
end
|
#updated? ⇒ Boolean
116
117
118
|
# File 'lib/kobana/resources/base.rb', line 116
def updated?
attributes[:updated] || false
end
|
#uri ⇒ Object
100
101
102
|
# File 'lib/kobana/resources/base.rb', line 100
def uri
"#{self.class.uri(attributes)}/#{attributes[self.class.primary_key]}"
end
|
#valid? ⇒ Boolean
108
109
110
|
# File 'lib/kobana/resources/base.rb', line 108
def valid?
errors.empty?
end
|