Class: Databricks::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/databricks/resource.rb

Overview

Encapsulate a resource identified in the API. A resource can have some properties, directly accessible, and also gives access to eventual sub-resources to get a hierarchical organization of the API.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connector) ⇒ Resource

Constructor

Parameters
  • connector (Connector): Connector handling API calls



35
36
37
38
39
40
41
42
43
# File 'lib/databricks/resource.rb', line 35

def initialize(connector)
  @connector = connector
  # Keep a map of sub-resources instantiated, per resource name.
  # Hash< Symbol, Resource >
  @sub_resources = {}
  # Properties linked to this resource
  # Hash< Symbol, Object >
  @properties = {}
end

Class Method Details

.sub_resources(*resource_names) ⇒ Object

Declare sub-resources accessors. This will make sure this resource has methods named after the sub-resources identifiers.

Parameters
  • resource_names (Array<Symbol>): Resource names to instantiate



23
24
25
26
27
28
29
# File 'lib/databricks/resource.rb', line 23

def self.sub_resources(*resource_names)
  resource_names.flatten.each do |resource_name|
    self.define_method(resource_name) do
      sub_resource(resource_name)
    end
  end
end

Instance Method Details

#add_properties(properties) ⇒ Object

Add/replace properties for this resource

Parameters
  • properties (Hash<Symbol,Object>): Properties for this resource



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/databricks/resource.rb', line 49

def add_properties(properties)
  # Define getters for properties
  (properties.keys - @properties.keys).each do |property_name|
    if self.respond_to?(property_name)
      raise "Can't define a property named #{property_name} - It's already used."
    else
      define_singleton_method(property_name) { @properties[property_name] }
    end
  end
  @properties.merge!(properties)
end

#inspectObject

Return a simple string representation of this resource

Result
  • String: Default representation



65
66
67
# File 'lib/databricks/resource.rb', line 65

def inspect
  "#<#{self.class.name.split('::').last} - #{@properties}>"
end

#new_resource(resource_name, properties = {}) ⇒ Object

Instantiate a new resource, with optional properties

Parameters
  • resource_name (Symbol): The resource’s name.

  • properties (Hash<Symbol or String,Object>): This resource’s initial properties [default = {}]

Result
  • Resource: The corresponding resource



88
89
90
91
92
93
# File 'lib/databricks/resource.rb', line 88

def new_resource(resource_name, properties = {})
  require "#{__dir__}/resources/#{resource_name}.rb"
  resource = Resources.const_get(resource_name.to_s.split('_').collect(&:capitalize).join.to_sym).new(@connector)
  resource.add_properties(properties.transform_keys(&:to_sym))
  resource
end

#sub_resource(resource_name) ⇒ Object

Instantiate a sub-resource. Keep a cache of it.

Parameters
  • resource_name (Symbol): Resource name.

Result
  • Resource: Corresponding sub-resource



76
77
78
79
# File 'lib/databricks/resource.rb', line 76

def sub_resource(resource_name)
  @sub_resources[resource_name] = new_resource(resource_name) unless @sub_resources.key?(resource_name)
  @sub_resources[resource_name]
end