class GCEMetadata::Base
Attributes
default_child_key[R]
path[R]
Public Class Methods
new(path, default_child_key = nil)
click to toggle source
# File lib/gce_metadata/base.rb, line 9 def initialize(path, default_child_key = nil) @path = path @default_child_key = default_child_key end
Public Instance Methods
child_keys()
click to toggle source
# File lib/gce_metadata/base.rb, line 18 def child_keys unless defined?(@child_keys) lines = GCEMetadata.get("#{path}").split(/$/).map(&:strip).select{|l| not (l.nil? or l.empty?)} # .map{|l| l.gsub!(%r'/$','')} leaves = lines.select{|line| line =~ %r'[^/]$'} @child_keys = lines if leaves.size > 0 @child_names = {} leaves.each do |key| name = get(key) name = name.match(/^\[.*?\]$/) ? (eval name rescue name) : name @child_names[key] = name end end end @child_keys end
Also aliased as: keys
children()
click to toggle source
# File lib/gce_metadata/base.rb, line 14 def children @children ||= {} end
default_child()
click to toggle source
# File lib/gce_metadata/base.rb, line 91 def default_child return @default_child if @default_child logging("default_child") do if default_child_key @getting_default_child = true begin @default_child = get(default_child_key) ensure @getting_default_child = false end end end end
from_hash(hash)
click to toggle source
# File lib/gce_metadata/base.rb, line 114 def from_hash(hash) hash = hash.inject({}){|d, (k, v)| d[GCEMetadata.formalize_key(k)] = v; d} @child_keys = hash.keys @children = {} hash.each do |key, value| if value.is_a?(Array) idx = 0 value = value.inject({}){|d, v| d[idx] = v; idx += 1; d} end if value.is_a?(Hash) child = new_child(key) @children[key] = child child.from_hash(value) else @children[key] = value end end end
get(child_key)
click to toggle source
# File lib/gce_metadata/base.rb, line 36 def get(child_key) logging("#{self.class.name}.get(#{child_key.inspect})") do child_key = GCEMetadata.formalize_key(child_key) if children.has_key?(child_key) result = children[child_key] else if is_child_key?(child_key) result = is_struct?(child_key) ? new_child(child_key) : GCEMetadata.get("#{path}#{child_key}") unless result if is_child_key?("#{child_key}/") child_key = "#{child_key}/" result = is_struct?(child_key) ? new_child(child_key) : GCEMetadata.get("#{path}#{child_key}") end end else raise NotFoundError, "#{path}#{child_key} not found as default key" if @getting_default_child raise NotFoundError, "#{path}#{child_key} not found" unless default_child result = default_child.get(child_key) end children[child_key] = result end result end end
Also aliased as: [], []
is_child_key?(key)
click to toggle source
# File lib/gce_metadata/base.rb, line 66 def is_child_key?(key) exp = /^#{key.gsub(%r'([^/])$', '\1/')}?$/ child_keys.any?{|child_key| child_key =~ exp} end
is_struct?(child_key)
click to toggle source
# File lib/gce_metadata/base.rb, line 71 def is_struct?(child_key) k = GCEMetadata.formalize_key(child_key) #k << '/' unless k.match(%r'/$') k.match(%r'/$') || (defined?(@child_names) && @child_names.keys.include?(child_key)) end
new_child(child_key)
click to toggle source
# File lib/gce_metadata/base.rb, line 77 def new_child(child_key) if defined?(@child_names) && (name = @child_names[child_key]) grandchild = Base.new("#{path}#{child_key}/") child = Base.new("#{path}#{child_key}/") child.instance_variable_set(:@children, {name => grandchild}) child.instance_variable_set(:@child_keys, [name]) child else Base.new("#{path}#{child_key}") end end
to_hash()
click to toggle source
# File lib/gce_metadata/base.rb, line 105 def to_hash keys.inject({}) do |dest, key| value = get(key) key = key.sub(/\/$/, '') dest[key] = value.respond_to?(:to_hash) ? value.to_hash : value dest end end
Private Instance Methods
logging(msg, &block)
click to toggle source
# File lib/gce_metadata/base.rb, line 135 def logging(msg, &block) GCEMetadata.logging(msg, &block) end