Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Jul 26, 2023
1 parent a83a37f commit 971db60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
15 changes: 10 additions & 5 deletions spec/hashr_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ describe "Hashr" do
}
value = Hashr.new({"foo" => JSON.parse(h.to_json)})

value.is_a?(Hashr).should be_true
value.foo.should eq h
value.foo.is_a?(Hashr).should be_true
value.foo.bar.should eq "bar"
value.foo.helloWorld.should eq "baz"
value.foo.bar.is_a?(Hashr).should be_true
value.foo.array.should eq ["1", "2", "3"]
value.foo.literalValue.integer.should eq 100
value.foo.literalValue.float.should eq 1.23
Expand All @@ -33,14 +35,17 @@ describe "Hashr" do

it "works with JSON::Any" do
h = {
"bar" => "bar",
"helloWorld" => "baz",
"array" => ["1", "2", "3"],
"bar" => "bar",
"baz" => {"foo" => "hello"},
"array" => ["1", "2", "3"],
}
value = Hashr.new(JSON.parse(h.to_json))

value.bar.should eq "bar"
value.helloWorld.should eq "baz"
value.bar.to_s.is_a?(String).should be_true
value.bar.inspect.is_a?(String).should be_true
value.baz.should eq({"foo" => "hello"})
typeof(value.baz.obj).should eq(Hash(String, JSON::Any) | JSON::Any)
value.array.should eq ["1", "2", "3"]
end

Expand Down
18 changes: 15 additions & 3 deletions src/hashr.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ class Hashr

macro method_missing(key)
def {{ key.id }}
value = obj[{{ key.id.stringify }}]
value = @obj[{{ key.id.stringify }}]

Hashr.new(value)
end

# def {{ key.id }}=(value)
# @obj[{{ key.id.stringify }}] = value
# end
end

def ==(other)
obj == other
@obj == other
end

def to_s(io)
@obj.to_s(io)
end

def inspect(io)
@obj.inspect(io)
end

# Can't redefine nil?
# can't redefine nil?
# def nil?
# obj.nil?
# end
Expand Down

0 comments on commit 971db60

Please sign in to comment.