Created
March 28, 2009 06:38
-
-
Save auser/87035 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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