Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Delegate is actually an alias

オープン
#1,089 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
rails, ruby
領域
api

調査の方向性

JSONAPI::Resource と attributes オプションの委譲処理から始め、issue にある Ruby と Rails の委譲例とその動作を比較します。delegate オプションが説明どおりにメソッドを委譲先オブジェクトへ転送するか、またはエイリアスのような動作が明確に文書化されていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

When working with the gem and trying to look at how to hide underlying models I came across the delegate option which is available on a resource.

Using this option I expected the same result as with the definitions provided in both ruby and rails. When I delegate a method to something, it calls the method being delegated on that something.

An example:

class Bar
  attr_accessor :name

  def initialize
    @name = "Test"
  end
end

class Foo
  extend Forwardable
  attr_accessor :bar

  def initialize
    @bar = Bar.new
  end

  def_delegator :@bar, :name
end

Foo.new.name            # => "Test"
foo = Foo.new
foo.bar.name = "Demo"
foo.name                # => "Demo"

This passes the delegate method along to the object @bar.

Now looking at the way it is defined in JSONAPI::Resource we have the following option:

class Bar
  attr_accessor :name

  def initialize
    @name = "Test"
  end
end

class Foo
  attr_accessor :bar

  def initialize
    @bar = Bar.new
  end
end

class FooResource < JSONAPI::Resource
  attributes :name, delegate: :bar
end

FooResource.new(Foo.new, {}).name  # => <Bar:0x007fd288c69708 @name="Test">

As you can see when calling name on the resource it gives me back the object Bar instead of calling the method name on the Bar object. This is more in line with how alias works instead of how delegate works.

It might be an idea to make this clearer in the documentation to prevent any confusion, and it maybe an idea in the future to modify this behaviour to be the same as how ruby and rails handle this.

主要言語
Ruby
スター
2.3k
フォーク
546
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

JSONAPI-Resources/jsonapi-resources のほかの issue

JSONAPI-Resources/jsonapi-resources の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。