Skip to content

Instantly share code, notes, and snippets.

@koichiro
Created July 24, 2009 01:33
Show Gist options
  • Save koichiro/153791 to your computer and use it in GitHub Desktop.
Save koichiro/153791 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
gem 'rubytter', '>= 0.6.5'
require 'rubytter'
module Termtter
class ActiveRubytter
def initialize(data)
self.attributes = data
end
def method_missing(name, *args)
if @data.key?(name)
return @data[name]
else
super
end
end
def attributes=(raw_hash)
@data = {}
raw_hash.each do |key, value|
key_symbol = key.to_s.to_sym
if value.kind_of? Hash
@data[key_symbol] = ActiveRubytter.new(raw_hash[key])
else
@data[key_symbol] = raw_hash[key]
end
end
end
end
end
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/../spec_helper'
require 'termtter/active_rubytter'
module Termtter
describe ActiveRubytter do
before(:each) do
@data = {
:hoge => "hogehoge",
:fuga => "fugafuga",
:hage => {
:foo => "foofoo",
:bar => {
:nest => "nestnest"
}
}
}
end
it "入れ子のHashをクラス化できる" do
d = ActiveRubytter.import @data
p d
# array.map{ |elem| ActiveRubytter.new(elem)})}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment