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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connector) ⇒ Resource

Constructor

Parameters
  • connector (Connector): Connector handling API calls



39
40
41
42
43
44
45
46
47
# File 'lib/databricks/resource.rb', line 39

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

Instance Attribute Details

#propertiesObject (readonly)

Get an accessor on all properties of this resource Hash< Symbol, Object >



20
21
22
# File 'lib/databricks/resource.rb', line 20

def properties
  @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



27
28
29
30
31
32
33
# File 'lib/databricks/resource.rb', line 27

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, replace: false) ⇒ Object

Add/replace properties for this resource. Properties will be deep-symbolized.

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

  • replace (Boolean): Should we replace properties instead of merging them? [default: false]



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/databricks/resource.rb', line 55

def add_properties(properties, replace: false)
  symbolized_properties = deep_symbolize(properties)
  # Define getters for properties
  (symbolized_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
  if replace
    @properties = symbolized_properties
  else
    @properties.merge!(symbolized_properties)
  end
end

#inspectObject

Return a simple string representation of this resource

Result
  • String: Default representation



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

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



99
100
101
102
103
104
# File 'lib/databricks/resource.rb', line 99

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)
  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



87
88
89
90
# File 'lib/databricks/resource.rb', line 87

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