Class: Artifactory::Resource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/artifactory/resources/base.rb

Direct Known Subclasses

Artifact, Build, Group, Plugin, Repository, System, User

Class Method Summary collapse

Instance Method Summary collapse

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

.attributesObject



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.

Parameters:

  • options (Hash)

    the list of options passed to the method

Options Hash (options):



75
76
77
# File 'lib/artifactory/resources/base.rb', line 75

def extract_client!(options)
  options.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.

Parameters:

  • options (Hash)

    the list of options to extract the repos from



88
89
90
91
92
# File 'lib/artifactory/resources/base.rb', line 88

def format_repos!(options)
  return options if options[:repos].nil? || options[:repos].empty?
  options[:repos] = Array(options[:repos]).compact.join(',')
  options
end

.url_safe(value) ⇒ String

Generate a URL-safe string from the given value.

Parameters:

  • value (#to_s)

    the value to sanitize

Returns:

  • (String)

    the URL-safe version of the string



103
104
105
# File 'lib/artifactory/resources/base.rb', line 103

def url_safe(value)
  URI.escape(value.to_s)
end

Instance Method Details

#attributeshash

The list of attributes for this resource.

Returns:

  • (hash)


128
129
130
# File 'lib/artifactory/resources/base.rb', line 128

def attributes
  @attributes ||= self.class.attributes.dup
end

#clientObject

Return this object’s client

Returns:

  • (Object)


112
# File 'lib/artifactory/resources/base.rb', line 112

attribute :client, ->{ Artifactory.client }

#client=(value) ⇒ Object

Set this object’s client

Parameters:

  • value (Object)

    the value to set for client

  • default (Object)

    the default value for this attribute



112
# File 'lib/artifactory/resources/base.rb', line 112

attribute :client, ->{ Artifactory.client }

#client?Boolean

Determines if the client value exists and is truthy

Returns:

  • (Boolean)


112
# File 'lib/artifactory/resources/base.rb', line 112

attribute :client, ->{ Artifactory.client }

#extract_client!(options) ⇒ Object

See Also:



148
149
150
# File 'lib/artifactory/resources/base.rb', line 148

def extract_client!(options)
  self.class.extract_client!(options)
end

#format_repos!(options) ⇒ Object

See Also:



153
154
155
# File 'lib/artifactory/resources/base.rb', line 153

def format_repos!(options)
  self.class.format_repos!(options)
end

#inspectObject



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.

Parameters:

  • key (#to_sym)

    the attribute to set

  • value (Object)

    the value to set

Returns:

  • (Object)

    the set value



143
144
145
# File 'lib/artifactory/resources/base.rb', line 143

def set(key, value)
  attributes[key.to_sym] = value
end

#to_sObject



163
164
165
# File 'lib/artifactory/resources/base.rb', line 163

def to_s
  "#<#{short_classname}>"
end

#url_safe(value) ⇒ Object

See Also:



158
159
160
# File 'lib/artifactory/resources/base.rb', line 158

def url_safe(value)
  self.class.url_safe(value)
end