Class: Artifactory::Resource::Base
- Inherits:
-
Object
- Object
- Artifactory::Resource::Base
- Defined in:
- lib/artifactory/resources/base.rb
Class Method Summary collapse
- .attribute(key, default = nil) ⇒ Object
- .attributes ⇒ Object
-
.extract_client!(options) ⇒ Object
Get the client (connection) object from the given options.
-
.format_repos!(options) ⇒ Object
Format the repos list from the given options.
-
.url_safe(value) ⇒ String
Generate a URL-safe string from the given value.
Instance Method Summary collapse
-
#attributes ⇒ hash
The list of attributes for this resource.
-
#client ⇒ Object
Return this object’s
client
. -
#client=(value) ⇒ Object
Set this object’s
client
. -
#client? ⇒ Boolean
Determines if the
client
value exists and is truthy. - #extract_client!(options) ⇒ Object
- #format_repos!(options) ⇒ Object
-
#initialize(attributes = {}) ⇒ Base
constructor
Create a new instance.
- #inspect ⇒ Object
-
#set(key, value) ⇒ Object
Set a given attribute on this resource.
- #to_s ⇒ Object
- #url_safe(value) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Base
Create a new instance
117 118 119 120 121 |
# File 'lib/artifactory/resources/base.rb', line 117 def initialize(attributes = {}) attributes.each do |key, value| set(key, value) end end |
Class Method Details
.attribute(key, default = nil) ⇒ Object
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 |
# File 'lib/artifactory/resources/base.rb', line 27 def attribute(key, default = nil) key = key.to_sym unless key.is_a?(Symbol) # Set the key on the top attributes, mostly for asthetic purposes attributes[key] = nil define_method(key) do value = attributes[key] return value unless value.nil? if default.nil? value elsif default.is_a?(Proc) default.call else default end end define_method("#{key}?") do !!attributes[key] end define_method("#{key}=") do |value| set(key, value) end end |
.attributes ⇒ Object
107 108 109 |
# File 'lib/artifactory/resources/base.rb', line 107 def attributes @attributes ||= {} end |
.extract_client!(options) ⇒ Object
Get the client (connection) object from the given options. If the :client
key is preset in the hash, it is assumed to contain the connection object to use for the request. If the :client
key is not present, the default Artifactory.client is used.
Warning, the value of Artifactory.client is not threadsafe! If multiple threads or processes are modifying the connection information, the same request could use a different client object. If you use the Client proxy methods, this is handled for you.
Warning, this method will remove the :client
key from the hash if it exists.
75 76 77 |
# File 'lib/artifactory/resources/base.rb', line 75 def extract_client!() .delete(:client) || Artifactory.client end |
.format_repos!(options) ⇒ Object
Format the repos list from the given options. This method will modify the given Hash parameter!
Warning, this method will modify the given hash if it exists.
88 89 90 91 92 |
# File 'lib/artifactory/resources/base.rb', line 88 def format_repos!() return if [:repos].nil? || [:repos].empty? [:repos] = Array([:repos]).compact.join(',') end |
.url_safe(value) ⇒ String
Generate a URL-safe string from the given value.
103 104 105 |
# File 'lib/artifactory/resources/base.rb', line 103 def url_safe(value) URI.escape(value.to_s) end |
Instance Method Details
#attributes ⇒ hash
The list of attributes for this resource.
128 129 130 |
# File 'lib/artifactory/resources/base.rb', line 128 def attributes @attributes ||= self.class.attributes.dup end |
#client ⇒ Object
Return this object’s client
112 |
# File 'lib/artifactory/resources/base.rb', line 112 attribute :client, ->{ Artifactory.client } |
#client=(value) ⇒ Object
Set this object’s client
112 |
# File 'lib/artifactory/resources/base.rb', line 112 attribute :client, ->{ Artifactory.client } |
#client? ⇒ Boolean
Determines if the client
value exists and is truthy
112 |
# File 'lib/artifactory/resources/base.rb', line 112 attribute :client, ->{ Artifactory.client } |
#extract_client!(options) ⇒ Object
148 149 150 |
# File 'lib/artifactory/resources/base.rb', line 148 def extract_client!() self.class.extract_client!() end |
#format_repos!(options) ⇒ Object
153 154 155 |
# File 'lib/artifactory/resources/base.rb', line 153 def format_repos!() self.class.format_repos!() end |
#inspect ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/artifactory/resources/base.rb', line 168 def inspect list = attributes.collect do |key, value| "#{key}: #{value.inspect}" unless key == :client end.compact "#<#{short_classname} #{list.join(', ')}>" end |
#set(key, value) ⇒ Object
Set a given attribute on this resource.
143 144 145 |
# File 'lib/artifactory/resources/base.rb', line 143 def set(key, value) attributes[key.to_sym] = value end |
#to_s ⇒ Object
163 164 165 |
# File 'lib/artifactory/resources/base.rb', line 163 def to_s "#<#{short_classname}>" end |
#url_safe(value) ⇒ Object
158 159 160 |
# File 'lib/artifactory/resources/base.rb', line 158 def url_safe(value) self.class.url_safe(value) end |