lib/redfish_client/resource.rb in redfish_client-0.2.4 vs lib/redfish_client/resource.rb in redfish_client-0.3.0

- old
+ new

@@ -14,11 +14,11 @@ # For example, if we have a root Redfish resource stored in `root`, # accessing `root.SessionService` will automatically fetch the appropriate # resource from the API. # # In order to reduce the amount of requests being sent to the service, - # resource also caches responses for later reuse. If we would like to get + # resource can also utilise caching connector. If we would like to get # fresh values from the service, {#reset} call will flush the cache, # causing next access to retrieve fresh data. class Resource # NoODataId error is raised when operation would need OpenData id of the # resource to accomplish the task a hand. @@ -42,11 +42,10 @@ # to fetch the resources # @param oid [String] OpenData id of the resource # @param content [Hash] content to populate resource with # @raise [NoResource] resource cannot be retrieved from the service def initialize(connector, oid: nil, content: nil) - @cache = {} @connector = connector if oid initialize_from_service(oid) else @@ -57,26 +56,14 @@ # Access resource content. # # This function offers a way of accessing resource data in the same way # that hash exposes its content. # - # In addition to accessing values associated with keys, this function can - # also be used to access members of collection by directly indexing into - # Members array. This means that `res["Members"][3]` can be shortened into - # `res[3]`. - # - # Indexing non-collection resource key will # raise `KeyError`. - # - # @param attr [String, Integer] key or index for accessing data + # @param attr [String] key for accessing data # @return associated value or `nil` if attr is missing def [](attr) - if attr.is_a?(Integer) - raise(KeyError, "Not a collection.") unless key?("Members") - cache("Members")[attr] - else - cache(attr) - end + build_resource(@content[attr]) end # Safely access nested resource content. # # This function is an equivalent of safe navigation operator that can be @@ -107,14 +94,15 @@ def respond_to_missing?(symbol, include_private = false) key?(symbol.to_s) || super end - # Clear the cached sub-resources. Next sub-resource access will repopulate - # the cache. + # Clear the cached sub-resources. + # + # This method is a no-op if connector in use does not support caching. def reset - @cache = {} + @connector.reset if @connector.respond_to?(:reset) end # Access raw JSON data that resource wraps. # # @return [Hash] wrapped data @@ -191,13 +179,9 @@ end def get_path(field, path) raise NoODataId if path.nil? && !key?(field) path || @content[field] - end - - def cache(name) - @cache[name] ||= build_resource(@content[name]) end def build_resource(data) return nil if data.nil?