Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate to internal hash #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions lib/hashugar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,50 @@
class Hashugar
def initialize(hash)
@table = {}
@table_with_original_keys = {}
hash.each_pair do |key, value|
hashugar = value.to_hashugar
@table_with_original_keys[key] = hashugar
@table[stringify(key)] = hashugar
@table[key] = value.to_hashugar
end
end

def method_missing(method, *args, &block)
method = method.to_s
if method.chomp!('=')
@table[method] = args.first
self[method] = args.first
else
@table[method]
if @table.respond_to?(method)
@table.send(method, *args, &block)
else
self[method]
end
end
end

def [](key)
@table[stringify(key)]
@table[key.to_s] || @table[key.to_sym]
end

def []=(key, value)
@table[stringify(key)] = value
if @table.has_key?(key.to_s)
@table[key.to_s] = value
else
@table[key.to_sym] = value
end
end

def respond_to?(key, include_all=false)
super(key) || @table.has_key?(stringify(key))
end

def each(&block)
@table_with_original_keys.each(&block)
super || @table.respond_to?(key) || @table.has_key?(key.to_s) || @table.has_key?(key.to_sym)
end

def to_hash
hash = @table_with_original_keys.to_hash
hash = @table.to_hash
hash.each do |key, value|
hash[key] = value.to_hash if value.is_a?(Hashugar)
end
end

def empty?
@table.empty?
end

def inspect
"#<#{self.class} #{to_hash.inspect}>"
end

private

def stringify(key)
key.is_a?(Symbol) ? key.to_s : key
end
end

class Hash
Expand Down
18 changes: 17 additions & 1 deletion spec/hashugar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
hashugar[:a] = 3
expect(hashugar.a).to eq(3)
end

it 'should be writable with original keys' do
hashugar = {:a => 1, 'b' => 2}.to_hashugar
hashugar.a = 100
hashugar.b = 200
expect(hashugar.to_hash).to eq({:a => 100, 'b' => 200})
end
end

context 'when accessing nested hash' do
Expand Down Expand Up @@ -107,13 +114,22 @@
end
end

describe '#empty?' do
describe 'Delegate' do
it 'behaves like the original hash' do
empty_hashugar = Hashugar.new({})
hashugar = Hashugar.new({a: 1})
expect(empty_hashugar.empty?).to be true
expect(hashugar.empty?).to be false
end

it 'has methods of the original hash' do
hashugar = Hashugar.new({a: 1, b: 2})
expect(hashugar.size).to eq(2)
expect(hashugar.any?{|k,v| k == :a}).to eq(true)
expect(hashugar.all?{|k,v| k == :a}).to eq(false)
expect(hashugar.map{|k,v| [k, v*2]}).to eq([[:a, 2],[:b, 4]])
expect(hashugar.values).to eq([1, 2])
end
end

describe '#inspect' do
Expand Down