Created
August 6, 2012 13:34
-
-
Save jmgarnier/3274498 to your computer and use it in GitHub Desktop.
Inner classes to implement SRP in ruby
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
class Statistics | |
def initialize | |
@configuration = Configuration.new(self) | |
end | |
delegate :for_caller, | |
:rtp_errors, | |
to: :@configuration | |
class Configuration | |
attr_accessor :test_campaign, :names | |
def initialize(statistics) | |
@statistics = statistics | |
end | |
def for_caller | |
@sip_call_type = CallerSipCall.name | |
@statistics | |
end | |
def rtp_errors | |
@stats_type = "rtp_errors" | |
@names = %w( late_packets lost_packets non_conforming_packets abandoned_packets ) | |
@statistics | |
end | |
def valid? | |
@test_campaign.present? && | |
@sip_call_type.present? && | |
@stats_type.present? | |
end | |
end | |
class Finder | |
# ... | |
end | |
class ValueObject | |
# ... | |
end | |
end |
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
# encoding: utf-8 | |
require 'spec_helper' | |
describe Statistics do | |
describe Statistics::Configuration do | |
before :each do | |
@statistics = Statistics.new | |
@configuration = Statistics::Configuration.new(@statistics) | |
end | |
it "test campaign must be set" do | |
@configuration.for_caller | |
@configuration.rtp_errors | |
expect(@configuration).not_to be_valid | |
end | |
it "caller or callee must be set" do | |
@configuration.test_campaign = @test_campaign | |
@configuration.rtp_errors | |
expect(@configuration).not_to be_valid | |
end | |
it "stats type must be set" do | |
@configuration.test_campaign = @test_campaign | |
@configuration.for_caller | |
expect(@configuration).not_to be_valid | |
end | |
it "is valid if the 3 previous parameters are set" do | |
@configuration.test_campaign = stub_model(TestCampaign) | |
@configuration.for_caller | |
@configuration.rtp_errors | |
expect(@configuration).to be_valid | |
end | |
it "choosing a stats type sets the list of stats which will be calculated" do | |
@configuration.rtp_errors | |
expect(@configuration.names). | |
to eq(%w( late_packets lost_packets non_conforming_packets abandoned_packets)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment