Class: PuppetDB::Model::Base
- Inherits:
-
Object
- Object
- PuppetDB::Model::Base
- Defined in:
- lib/puppetdb/model/base.rb
Class Method Summary collapse
-
.client ⇒ PuppetDB::Client
Return the PuppetDB Client.
-
.client=(client) ⇒ Object
Saves the PuppetDB client into a class variable which is shared among all object created from this class.
-
.get(object:, regexp: false, **filters) ⇒ Object
Get object from Puppet DB.
-
.query(query) ⇒ Object
Query the Puppet DB an build new Objects from the result.
- .request(query) ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Base
constructor
Parses objects from PuppetDB.
Constructor Details
#initialize(attributes) ⇒ Base
Parses objects from PuppetDB
The parser expects an array of hashes where each item in the array is parsed into a separate object. The keys of the hashes are turned into getters of the resulting object returning the associated values.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/puppetdb/model/base.rb', line 9 def initialize(attributes) attributes.each do |attribute_name, attribute_value| # this will create an `attr_reader` for each attribute passed self.class.send(:define_method, attribute_name.to_sym) do instance_variable_get("@#{attribute_name}") end # set an instance variable for each attribute like `@certname` instance_variable_set("@#{attribute_name}", attribute_value) end end |
Class Method Details
.client ⇒ PuppetDB::Client
Return the PuppetDB Client
33 34 35 |
# File 'lib/puppetdb/model/base.rb', line 33 def self.client @@client end |
.client=(client) ⇒ Object
Saves the PuppetDB client into a class variable which is shared among all object created from this class
24 25 26 27 28 |
# File 'lib/puppetdb/model/base.rb', line 24 def self.client=(client) # rubocop:disable Style/ClassVars @@client = client # rubocop:enable Style/ClassVars end |
.get(object:, regexp: false, **filters) ⇒ Object
Get object from Puppet DB
Pass a list of key: val arguments as filters for the query.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/puppetdb/model/base.rb', line 59 def self.get(object:, regexp: false, **filters) # use strict comparator by default, because this is less performance intensive comparator = regexp ? '~' : '=' filters = filters.map do |key, value| # Boolean values doesn't support regexp operator(~) and must not be escaped if [TrueClass, FalseClass].include?(value.class) "#{key} = #{value}" else "#{key} #{comparator} #{value.to_s.dump}" end end query("#{object} {#{filters.join(' and ')}}") end |
.query(query) ⇒ Object
Query the Puppet DB an build new Objects from the result
40 41 42 |
# File 'lib/puppetdb/model/base.rb', line 40 def self.query(query) request(query).map { |object| new(object) } end |
.request(query) ⇒ Object
44 45 46 |
# File 'lib/puppetdb/model/base.rb', line 44 def self.request(query) client.request('', query).data end |