Class: EasyType::FlexibleFact
- Inherits:
-
Object
- Object
- EasyType::FlexibleFact
- Defined in:
- lib/easy_type/flexible_fact.rb
Class Method Summary collapse
- .config_data ⇒ Object
- .config_dir ⇒ Object
- .config_file ⇒ Object
- .define(basic_name, &definition) ⇒ Object
Instance Method Summary collapse
- #apply ⇒ Object
- #chuncked_implementation ⇒ Object
- #components(components) ⇒ Object
- #force_dot_enabled? ⇒ Boolean
- #force_dot_implementation ⇒ Object
- #global_config ⇒ Object
-
#initialize(basic_name) ⇒ FlexibleFact
constructor
A new instance of FlexibleFact.
- #no_caching_in_facter_yet? ⇒ Boolean
- #part(name, &implementation) ⇒ Object
Constructor Details
#initialize(basic_name) ⇒ FlexibleFact
Returns a new instance of FlexibleFact.
44 45 46 47 |
# File 'lib/easy_type/flexible_fact.rb', line 44 def initialize(basic_name) @basic_name = basic_name @parts = [] end |
Class Method Details
.config_data ⇒ Object
32 33 34 |
# File 'lib/easy_type/flexible_fact.rb', line 32 def self.config_data @config_data ||= Hocon::ConfigFactory.parse_file(config_file).resolve end |
.config_dir ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/easy_type/flexible_fact.rb', line 16 def self.config_dir kernel = Facter.value(:kernel) case kernel when 'Linux' '/etc/puppetlabs/facter' when 'windows' 'C:/ProgramData/PuppetLabs/facter/etc' else fail "'#{kernel}' not (yet) supported." end end |
.config_file ⇒ Object
28 29 30 |
# File 'lib/easy_type/flexible_fact.rb', line 28 def self.config_file "#{config_dir}/facter.conf" end |
.define(basic_name, &definition) ⇒ Object
10 11 12 13 14 |
# File 'lib/easy_type/flexible_fact.rb', line 10 def self.define(basic_name, &definition) obj = new(basic_name) obj.instance_eval(&definition) obj.apply() end |
Instance Method Details
#apply ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/easy_type/flexible_fact.rb', line 109 def apply() if force_dot_enabled? force_dot_implementation else chuncked_implementation end end |
#chuncked_implementation ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/easy_type/flexible_fact.rb', line 81 def chuncked_implementation # # Ensure correct variable are available as local variables in Facter closure scopes # parts = @parts components = @components Facter.add(@basic_name.to_sym, :type => :aggregate) do parts.each do | part| name = part.keys.first implementation = part[name] chunk(name.to_sym) do components.reduce({name.to_sym => {}}) do |data, dir| data[name.to_sym][dir] = implementation.call(dir) data end end end end end |
#components(components) ⇒ Object
101 102 103 |
# File 'lib/easy_type/flexible_fact.rb', line 101 def components(components) @components = components end |
#force_dot_enabled? ⇒ Boolean
53 54 55 56 57 |
# File 'lib/easy_type/flexible_fact.rb', line 53 def force_dot_enabled? return false if no_caching_in_facter_yet? global_config.dig('force-dot-resolution') end |
#force_dot_implementation ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/easy_type/flexible_fact.rb', line 59 def force_dot_implementation @parts.each do | part| # # Ensure correct variable are available as local variables in Facter closure scopes # name = part.keys.first implementation = part[name] components = @components # # Now define the facts in the correct way # Facter.add("#{@basic_name}.#{name}") do setcode do components.each_with_object({}) do |dir, data| data[dir] = implementation.call(dir) data end end end end end |
#global_config ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/easy_type/flexible_fact.rb', line 36 def global_config if self.class.config_data.has_path?('global') self.class.config_data.get_value('global').unwrapped else {} end end |
#no_caching_in_facter_yet? ⇒ Boolean
49 50 51 |
# File 'lib/easy_type/flexible_fact.rb', line 49 def no_caching_in_facter_yet? Gem::Version.new(Facter.version) < Gem::Version.new('4.1.0') end |
#part(name, &implementation) ⇒ Object
105 106 107 |
# File 'lib/easy_type/flexible_fact.rb', line 105 def part(name, &implementation) @parts << {name => implementation} end |