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

Small fix for gauge #30

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion lib/ruby-metrics/instruments/gauge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(&block)
end

def get
instance_exec(&@block)
@block.call
end

def as_json(*_)
Expand Down
44 changes: 33 additions & 11 deletions spec/instruments/gauge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,57 @@
callback = Proc.new {}
@gauge = Metrics::Instruments::Gauge.new &callback
end

it "should correctly callback the block given when we call Gauge#get" do
result = 42
callback = Proc.new do

callback = Proc.new do
{
:result => result
}
end

@gauge = Metrics::Instruments::Gauge.new &callback

@gauge.get[:result].should == 42

result += 1

@gauge.get[:result].should == 43
end


class Test
def initialize
@gauge = Metrics::Instruments::Gauge.new { @status.to_s }
@status = :new
end

def change_status
@status = :old
end

def get_gauge
@gauge.get
end
end

it "gauge block within another class should be able to reference other classes variables" do
test_class = Test.new
test_class.get_gauge.should == "new"
test_class.change_status
test_class.get_gauge.should == "old"
end

context "to_json" do
it "should serialize the current value" do
result = 0
gauge = Metrics::Instruments::Gauge.new{ result }

gauge.to_json.should == result.to_s

result = 2
gauge.to_json.should == result.to_s
end
end

end