File: param_value.rb

package info (click to toggle)
puppet 3.7.2-4+deb8u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 18,912 kB
  • ctags: 13,168
  • sloc: ruby: 210,410; sh: 2,050; xml: 1,554; lisp: 300; makefile: 142; python: 108; sql: 103; yacc: 72
file content (74 lines) | stat: -rw-r--r-- 2,507 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'puppet/util/rails/reference_serializer'

class Puppet::Rails::ParamValue < ActiveRecord::Base
  include Puppet::Util::ReferenceSerializer
  extend Puppet::Util::ReferenceSerializer

  belongs_to :param_name
  belongs_to :resource

  # Store a new parameter in a Rails db.
  def self.from_parser_param(param, values)
    values = munge_parser_values(values)

    param_name = Puppet::Rails::ParamName.find_or_create_by_name(param.to_s)
    return values.collect do |v|
      {:value => v, :param_name => param_name}
    end
  end

  # Make sure an array (or possibly not an array) of values is correctly
  # set up for Rails.  The main thing is that Resource::Reference objects
  # should stay objects, so they just get serialized.
  def self.munge_parser_values(value)
    values = value.is_a?(Array) ? value : [value]
    values.map do |v|
      if v.is_a?(Puppet::Resource)
        v
      else
        v.to_s
      end
    end
  end


  def value
    unserialize_value(self[:value])
  end

  # I could not find a cleaner way to handle making sure that resource references
  # were consistently serialized and deserialized.
  def value=(val)
    self[:value] = serialize_value(val)
  end

  def to_label
    "#{self.param_name.name}"
  end

  # returns an array of hash containing all the parameters of a given resource
  def self.find_all_params_from_resource(db_resource)
    params = ActiveRecord::Base.connection.select_all("SELECT v.id, v.value, v.line, v.resource_id, v.param_name_id, n.name FROM param_values v INNER JOIN param_names n ON v.param_name_id=n.id WHERE v.resource_id=#{db_resource.id}")
    params.each do |val|
      val['value'] = unserialize_value(val['value'])
      val['line'] = val['line'] ? Integer(val['line']) : nil
      val['resource_id'] = Integer(val['resource_id'])
    end
    params
  end

  # returns an array of hash containing all the parameters of a given host
  def self.find_all_params_from_host(db_host)
    params = ActiveRecord::Base.connection.select_all("SELECT v.id, v.value,  v.line, v.resource_id, v.param_name_id, n.name FROM param_values v INNER JOIN resources r ON v.resource_id=r.id INNER JOIN param_names n ON v.param_name_id=n.id WHERE r.host_id=#{db_host.id}")
    params.each do |val|
      val['value'] = unserialize_value(val['value'])
      val['line'] = val['line'] ? Integer(val['line']) : nil
      val['resource_id'] = Integer(val['resource_id'])
    end
    params
  end

  def to_s
    "#{self.name} => #{self.value}"
  end
end