Class: Junos::Ez::Facts::Keeper

Inherits:
Object
  • Object
show all
Defined in:
lib/junos-ez/facts.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ndev) ⇒ Keeper

Returns a new instance of Keeper.



28
29
30
31
# File 'lib/junos-ez/facts.rb', line 28

def initialize( ndev )
  @ndev = ndev
  @known = Hash.new
end

Instance Attribute Details

#knownObject

Returns the value of attribute known.



26
27
28
# File 'lib/junos-ez/facts.rb', line 26

def known
  @known
end

Class Method Details

.define(fact, &block) ⇒ Object



49
50
51
# File 'lib/junos-ez/facts.rb', line 49

def self.define( fact, &block )
  define_method( "fact_read_#{fact}".to_sym, block )
end

Instance Method Details

#[](key) ⇒ Object



57
58
59
# File 'lib/junos-ez/facts.rb', line 57

def [](key)
  @known[key]
end

#[]=(key, value) ⇒ Object



53
54
55
# File 'lib/junos-ez/facts.rb', line 53

def []=(key,value)
  @known[key] = value
end

#catalogObject



38
# File 'lib/junos-ez/facts.rb', line 38

def catalog; @known end

#catalog!Object



39
# File 'lib/junos-ez/facts.rb', line 39

def catalog!; read!; catalog end

#clearObject



33
# File 'lib/junos-ez/facts.rb', line 33

def clear; @known.clear end

#listObject



35
# File 'lib/junos-ez/facts.rb', line 35

def list; @known.keys end

#list!Object



36
# File 'lib/junos-ez/facts.rb', line 36

def list!; read!; list; end

#read!Object



61
62
63
64
65
66
67
68
69
# File 'lib/junos-ez/facts.rb', line 61

def read!
  @known.clear
  fact_readers = self.methods.grep /^fact_read_/
  fact_readers.each do |getter| 
    getter =~ /^fact_read_(\w+)/
    fact = $1.to_sym
    self.send( getter, @ndev, @known ) unless @known[fact]
  end
end

#uses(*facts) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/junos-ez/facts.rb', line 41

def uses( *facts )
  values = facts.collect do |f|
    self.send( "fact_read_#{f}", @ndev, @known ) unless @known[f]
    self[f]
  end      
  (values.count == 1) ? values[0] : values      
end