lib/chef-api/resources/base.rb in chef-api-0.2.0 vs lib/chef-api/resources/base.rb in chef-api-0.2.1
- old
+ new
@@ -18,11 +18,11 @@
#
# @todo doc
#
def from_url(url, prefix = {})
- from_json(ChefAPI.connection.get(url), prefix)
+ from_json(connection.get(url), prefix)
end
#
# Get or set the schema for the remote resource. You probably only want
# to call schema once with a block, because it will overwrite the
@@ -76,17 +76,17 @@
# @example Create an association with custom configuration
#
# has_many :environments, class_name: 'Environment'
#
def has_many(method, options = {})
- class_name = options[:class_name] || Util.camelize(method).sub(/s$/, '')
+ class_name = options[:class_name] || "Resource::#{Util.camelize(method).sub(/s$/, '')}"
rest_endpoint = options[:rest_endpoint] || method
class_eval <<-EOH, __FILE__, __LINE__ + 1
def #{method}
associations[:#{method}] ||=
- CollectionProxy.new(self, #{class_name}, '#{rest_endpoint}')
+ Resource::CollectionProxy.new(self, #{class_name}, '#{rest_endpoint}')
end
EOH
end
#
@@ -136,11 +136,11 @@
# @return [String]
# the JSON response from the server
#
def post(body, prefix = {})
path = expanded_collection_path(prefix)
- ChefAPI.connection.post(path, body)
+ connection.post(path, body)
end
#
# Perform a PUT request to the Chef Server against the given resource or
# resource identifier. The resource will be partially updated (this
@@ -157,11 +157,11 @@
# @return [String]
# the JSON response from the server
#
def put(id, body, prefix = {})
path = resource_path(id, prefix)
- ChefAPI.connection.put(path, body)
+ connection.put(path, body)
end
#
# Delete the remote resource from the Chef Sserver.
#
@@ -171,11 +171,11 @@
# the list of prefix options (for nested resources)
# @return [true]
#
def delete(id, prefix = {})
path = resource_path(id, prefix)
- ChefAPI.connection.delete(path)
+ connection.delete(path)
true
rescue Error::HTTPNotFound
true
end
@@ -193,11 +193,11 @@
#
# @return [Array<String>]
#
def list(prefix = {})
path = expanded_collection_path(prefix)
- ChefAPI.connection.get(path).keys.sort
+ connection.get(path).keys.sort
end
#
# Destroy a record with the given id.
#
@@ -247,11 +247,11 @@
#
def fetch(id, prefix = {})
return nil if id.nil?
path = resource_path(id, prefix)
- response = ChefAPI.connection.get(path)
+ response = connection.get(path)
from_json(response, prefix)
rescue Error::HTTPNotFound
nil
end
@@ -361,11 +361,11 @@
# @example get all the resource's names
# Bacon.map(&:name) #=> ["ham", "sausage", "turkey"]
#
def each(prefix = {}, &block)
collection(prefix).each do |resource, path|
- response = ChefAPI.connection.get(path)
+ response = connection.get(path)
result = from_json(response, prefix)
block.call(result) if block
end
end
@@ -381,13 +381,12 @@
alias_method :size, :count
#
# Return an array of all resources in the collection.
#
- # @warn
- # Unless you need the _entire_ collection, please consider using the
- # {size} and {each} methods instead as they are much more perforant.
+ # @note Unless you need the _entire_ collection, please consider using the
+ # {size} and {each} methods instead as they are much more perforant.
#
# @return [Array<Resource::Base>]
#
def all
entries
@@ -468,11 +467,11 @@
#
# @return [Array<Resource::Base>]
# a list of resources in the collection
#
def collection(prefix = {})
- ChefAPI.connection.get(expanded_collection_path(prefix))
+ connection.get(expanded_collection_path(prefix))
end
#
# The path to an individual resource.
#
@@ -514,10 +513,19 @@
end
URI.escape(value)
end.sub(/^\//, '') # Remove leading slash
end
+
+ #
+ # The current connection object.
+ #
+ # @return [ChefAPI::Connection]
+ #
+ def connection
+ Thread.current['chefapi.connection'] || ChefAPI.connection
+ end
end
#
# The list of associations.
#
@@ -544,10 +552,13 @@
# the list of initial attributes to set on the model
# @param [Hash] prefix
# the list of prefix options (for nested resources)
#
def initialize(attributes = {}, prefix = {})
+ @schema = self.class.schema.dup
+ @schema.load_flavor(self.class.connection.flavor)
+
@associations = {}
@_prefix = prefix
# Define a getter and setter method for each attribute in the schema
_attributes.each do |key, value|
@@ -569,11 +580,11 @@
#
# @return [Symbol]
# the primary key for this resource
#
def primary_key
- self.class.schema.primary_key
+ @schema.primary_key
end
#
# The unique id for this resource.
#
@@ -594,11 +605,11 @@
# The list of attributes on this resource.
#
# @return [Hash<Symbol, Object>]
#
def _attributes
- @_attributes ||= {}.merge(self.class.schema.attributes)
+ @_attributes ||= {}.merge(@schema.attributes)
end
#
# Determine if this resource has the given attribute.
#
@@ -634,12 +645,11 @@
# Reload (or reset) this object using the values currently stored on the
# remote server. This method will also clear any cached collection proxies
# so they will be reloaded the next time they are requested. If the remote
# record does not exist, no attributes are modified.
#
- # @warn
- # This will remove any custom values you have set on the resource!
+ # @note This will remove any custom values you have set on the resource!
#
# @return [self]
# the instance of the reloaded record
#
def reload!
@@ -746,11 +756,11 @@
#
# @return [Array<~Validator::Base>]
# the list of validators for this resource
#
def validators
- @validators ||= self.class.schema.validators
+ @validators ||= @schema.validators
end
#
# Run all of this resource's validations, raising an exception if any
# validations fail.
@@ -843,12 +853,11 @@
# @example when the local resource differs from the remote resource
# bacon = Bacon.first
# bacon.description = "My new description"
# bacon.diff #=> { :description => { :local => "My new description", :remote => "Old description" } }
#
- # @warn
- # This is a VERY expensive operation - use it sparringly!
+ # @note This is a VERY expensive operation - use it sparringly!
#
# @return [Hash]
#
def diff
diff = {}
@@ -885,10 +894,10 @@
# the attribute to check ignorance
#
# @return [Boolean]
#
def ignore_attribute?(key)
- self.class.schema.ignored_attributes.has_key?(key.to_sym)
+ @schema.ignored_attributes.has_key?(key.to_sym)
end
#
# The collection of errors on the resource.
#