Sha256: 82a73f6af823d6540dcde22a88372476a318b357121c089e90857326b8364674

Contents?: true

Size: 992 Bytes

Versions: 26

Compression:

Stored size: 992 Bytes

Contents

require 'test_helper'

class GetterSetterTest < BaseTest
  representer! do
    property :name, # key under :name.
      :getter => lambda { |args| "#{args[:welcome]} #{song_name}" },
      :setter => lambda { |val, args| self.song_name = "#{args[:welcome]} #{val}" }
  end

  subject { Struct.new(:song_name).new("Mony Mony").extend(representer) }

  it "uses :getter when rendering" do
    subject.instance_eval { def name; raise; end }
    subject.to_hash(:welcome => "Hi").must_equal({"name" => "Hi Mony Mony"})
  end

  it "does not call original reader when rendering" do
    subject.instance_eval { def name; raise; end; self }.to_hash({})
  end

  it "uses :setter when parsing" do
    subject.from_hash({"name" => "Eyes Without A Face"}, :welcome => "Hello").song_name.must_equal "Hello Eyes Without A Face"
  end

  it "does not call original writer when parsing" do
    subject.instance_eval { def name=(*); raise; end; self }.from_hash({"name"=>"Dana D And Talle T"}, {})
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
representable-2.3.0 test/getter_setter_test.rb
representable-2.2.3 test/getter_setter_test.rb
representable-2.2.2 test/getter_setter_test.rb
representable-2.2.1 test/getter_setter_test.rb
representable-2.2.0 test/getter_setter_test.rb
representable-2.1.8 test/getter_setter_test.rb
representable-2.1.7 test/getter_setter_test.rb
representable-2.1.6 test/getter_setter_test.rb
representable-2.1.5 test/getter_setter_test.rb
representable-2.1.4 test/getter_setter_test.rb
representable-2.1.3 test/getter_setter_test.rb
representable-2.1.1 test/getter_setter_test.rb
representable-2.1.0 test/getter_setter_test.rb
representable-2.0.4 test/getter_setter_test.rb
representable-2.0.3 test/getter_setter_test.rb
representable-2.0.2 test/getter_setter_test.rb
representable-2.0.1 test/getter_setter_test.rb
representable-2.0.0 test/getter_setter_test.rb
representable-2.0.0.rc2 test/getter_setter_test.rb
representable-2.0.0.rc1 test/getter_setter_test.rb