Sha256: 04ea6b3eca2ee23a94312416d4f1ca95b083316f53e9a715a3bb4c311d45f81f

Contents?: true

Size: 830 Bytes

Versions: 9

Compression:

Stored size: 830 Bytes

Contents

require 'test_helper'

class GetterSetterTest < BaseTest
  representer! do
    property :name, # key under :name.
      :getter => lambda { |options| "#{options[:user_options][:welcome]} #{song_name}" },
      :setter => lambda { |options| self.song_name = "#{options[:user_options][:welcome]} #{options[:input]}" }
  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(user_options: {welcome: "Hi"}).must_equal({"name" => "Hi Mony Mony"})
  end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
representable-3.0.4 test/getter_setter_test.rb
representable-3.0.3 test/getter_setter_test.rb
representable-3.0.2 test/getter_setter_test.rb
representable-3.0.1 test/getter_setter_test.rb
representable-3.0.0 test/getter_setter_test.rb
representable-2.4.1 test/getter_setter_test.rb
representable-2.4.0 test/getter_setter_test.rb
representable-2.4.0.rc5 test/getter_setter_test.rb
representable-2.4.0.rc4 test/getter_setter_test.rb