Skip to content

Instantly share code, notes, and snippets.

@JoelQ
Last active September 23, 2024 03:02
Show Gist options
  • Select an option

  • Save JoelQ/8d78e8960e3d7cd60402f1f2de648bfa to your computer and use it in GitHub Desktop.

Select an option

Save JoelQ/8d78e8960e3d7cd60402f1f2de648bfa to your computer and use it in GitHub Desktop.

Revisions

  1. JoelQ revised this gist Apr 6, 2022. 1 changed file with 41 additions and 0 deletions.
    41 changes: 41 additions & 0 deletions value_object_shared_examples.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    RSpec.shared_examples_for "value object" do
    describe "#hash" do
    it "is the same as another instance of the same value" do
    expect(item.hash).to eq same.hash
    end

    it "is the different from another instance of a different value" do
    expect(item.hash).not_to eq different.hash
    end
    end

    describe "#eql?" do
    it "two instances are eql if they have the same value" do
    expect(item).to be_eql same
    end

    it "two instances are not eql if they have a different value" do
    expect(item).not_to be_eql different
    end
    end

    describe "#==" do
    it "two instances are == if they have the same value" do
    expect(item).to eq same
    end

    it "two instances are not == if they have a different value" do
    expect(item).not_to eq different
    end
    end

    describe "#equal?" do
    it "is equal if two object share the same identity" do
    expect(item).to be_equal item
    end

    it "is not equal if two different objects share the same value" do
    expect(item).not_to be_equal same
    end
    end
    end
  2. JoelQ created this gist Apr 6, 2022.
    16 changes: 16 additions & 0 deletions dollar.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    class Dollar
    attr_reader :cents

    def initialize(cents:)
    @cents = cents
    end

    def hash
    [self.class, cents].hash
    end

    def ==(other)
    cents == other.cents
    end
    alias_method :eql?, :==
    end
    10 changes: 10 additions & 0 deletions dollar_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    require_relative "dollar"
    require_relative "value_object_shared_examples"

    RSpec.describe Dollar do
    it_behaves_like "value object" do
    let(:item) { Dollar.new(cents: 5) }
    let(:same) { Dollar.new(cents: 5) }
    let(:different) { Dollar.new(cents: 10) }
    end
    end
    16 changes: 16 additions & 0 deletions duration.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    class Duration
    attr_reader :milliseconds

    def initialize(milliseconds:)
    @milliseconds = milliseconds
    end

    def hash
    [self.class, milliseconds].hash
    end

    def ==(other)
    milliseconds == other.milliseconds
    end
    alias_method :eql?, :==
    end
    10 changes: 10 additions & 0 deletions duration_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    require_relative "duration"
    require_relative "value_object_shared_examples"

    RSpec.describe Duration do
    it_behaves_like "value object" do
    let(:item) { Duration.new(milliseconds: 1000) }
    let(:same) { Duration.new(milliseconds: 1000) }
    let(:different) { Duration.new(milliseconds: 1234) }
    end
    end
    38 changes: 38 additions & 0 deletions shell_output.log
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    % rspec duration_spec.rb dollar_spec.rb --format documentation

    Randomized with seed 9567

    Dollar
    behaves like value object
    #==
    two instances are not == if they have a different value
    two instances are == if they have the same value
    #hash
    is the same as another instance of the same value
    is the different from another instance of a different value
    #equal?
    is equal if two object share the same identity
    is not equal if two different objects share the same value
    #eql?
    two instances are not eql if they have a different value
    two instances are eql if they have the same value

    Duration
    behaves like value object
    #==
    two instances are == if they have the same value
    two instances are not == if they have a different value
    #hash
    is the same as another instance of the same value
    is the different from another instance of a different value
    #eql?
    two instances are eql if they have the same value
    two instances are not eql if they have a different value
    #equal?
    is not equal if two different objects share the same value
    is equal if two object share the same identity

    Finished in 0.00453 seconds (files took 0.11736 seconds to load)
    16 examples, 0 failures

    Randomized with seed 9567