Sha256: 6f62288f9a3a5ad19f2817711989db2592c3b86b9632979af7a2333ca69a5933

Contents?: true

Size: 835 Bytes

Versions: 5

Compression:

Stored size: 835 Bytes

Contents

require 'test_helper'

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

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/test/getter_setter_test.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/test/getter_setter_test.rb
representable-3.2.0 test/getter_setter_test.rb
representable-3.1.1 test/getter_setter_test.rb
representable-3.1.0 test/getter_setter_test.rb