Skip to content

Instantly share code, notes, and snippets.

@auser
Created March 28, 2009 06:38
Show Gist options
  • Select an option

  • Save auser/87035 to your computer and use it in GitHub Desktop.

Select an option

Save auser/87035 to your computer and use it in GitHub Desktop.
require "rubygems"
require "json"
class Schema
attr_accessor :hsh
def initialize(h={})
@hsh = {}
case h
when Hash
h.each {|k,v| self[k] = v}
when String
JSON.parse(h).each {|k,v| self[k.to_sym] = v}
end
end
def [](k)
hsh[k]
end
def []=(k,v)
if v.is_a?(Hash)
hsh[k.to_sym] = self.class.new(v)
else
hsh[k.to_sym] = v
end
end
def to_hash
@hsh
end
def save!
::File.open("#{Default.base_config_directory}/#{Default.properties_hash_filename}", "w") {|f| f << self.to_json }
end
def method_missing(sym, *args, &block)
if @hsh.has_key?(sym.to_sym)
@hsh.fetch(sym)
elsif @hsh.has_key?(sym.to_s)
@hsh.fetch(sym.to_s)
else
@hsh.send(sym, *args, &block)
end
end
end
class Instance
attr_reader :ip, :name
def initialize(ip, name)
@ip = ip
@name = name
end
def to_s
"#{@ip}\t#{@name}"
end
end
a = Instance.new("10.0.1.1", "node1")
b = Instance.new("10.0.1.2", "node2")
c = Instance.new("10.0.1.3", "node3")
hsh = {:instances => [a,b,c]}
j = hsh.to_json
s = Schema.new(j)
p s.instances[0].split("\t") # ["10.0.1.1", "node1"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment