Class: Ec2Metadata::Base
- Inherits:
-
Object
- Object
- Ec2Metadata::Base
- Defined in:
- lib/ec2_metadata/base.rb
Instance Attribute Summary collapse
-
#default_child_key ⇒ Object
readonly
Returns the value of attribute default_child_key.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #child_keys ⇒ Object (also: #keys)
- #children ⇒ Object
- #default_child ⇒ Object
- #from_hash(hash) ⇒ Object
- #get(child_key) ⇒ Object (also: #[])
-
#initialize(path, default_child_key = nil) ⇒ Base
constructor
A new instance of Base.
- #is_child_key?(key) ⇒ Boolean
- #is_struct?(child_key) ⇒ Boolean
- #new_child(child_key) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(path, default_child_key = nil) ⇒ Base
Returns a new instance of Base.
9 10 11 12 |
# File 'lib/ec2_metadata/base.rb', line 9 def initialize(path, default_child_key = nil) @path = path @default_child_key = default_child_key end |
Instance Attribute Details
#default_child_key ⇒ Object (readonly)
Returns the value of attribute default_child_key.
7 8 9 |
# File 'lib/ec2_metadata/base.rb', line 7 def default_child_key @default_child_key end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/ec2_metadata/base.rb', line 6 def path @path end |
Instance Method Details
#child_keys ⇒ Object Also known as: keys
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ec2_metadata/base.rb', line 18 def child_keys unless defined?(@child_keys) lines = Ec2Metadata.get("#{path}").split(/$/).map(&:strip) if lines.all?{|line| line =~ /^[\w]+\=.*?/} @child_keys = [] @child_names = {} lines.each do |line| key, name = line.split(/\=/, 2) @child_keys << key @child_names[key] = name end else @child_keys = lines end end @child_keys end |
#children ⇒ Object
14 15 16 |
# File 'lib/ec2_metadata/base.rb', line 14 def children @children ||= {} end |
#default_child ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ec2_metadata/base.rb', line 83 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) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ec2_metadata/base.rb', line 106 def from_hash(hash) hash = hash.inject({}){|d, (k, v)| d[Ec2Metadata.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) ⇒ Object Also known as: []
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ec2_metadata/base.rb', line 37 def get(child_key) logging("#{self.class.name}.get(#{child_key.inspect})") do child_key = Ec2Metadata.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) : Ec2Metadata.get("#{path}#{child_key}") 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 |
#is_child_key?(key) ⇒ Boolean
59 60 61 62 |
# File 'lib/ec2_metadata/base.rb', line 59 def is_child_key?(key) exp = /^#{key}\/?$/ child_keys.any?{|child_key| child_key =~ exp} end |
#is_struct?(child_key) ⇒ Boolean
64 65 66 67 |
# File 'lib/ec2_metadata/base.rb', line 64 def is_struct?(child_key) k = Ec2Metadata.formalize_key(child_key) << '/' child_keys.include?(k) || (defined?(@child_names) && @child_names.keys.include?(child_key)) end |
#new_child(child_key) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ec2_metadata/base.rb', line 69 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 ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/ec2_metadata/base.rb', line 97 def to_hash keys.inject({}) do |dest, key| key = key.sub(/\/$/, '') value = get(key) dest[key] = value.respond_to?(:to_hash) ? value.to_hash : value dest end end |