Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Delegate is actually an alias

Đang mở
#1,089 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rails, ruby
Lĩnh vực
api

Hướng nghiên cứu

Bắt đầu từ JSONAPI::Resource và cách xử lý ủy quyền của tùy chọn attributes, sau đó so sánh hành vi của nó với các ví dụ về ủy quyền trong Ruby và Rails ở issue. Hoàn tất khi tùy chọn delegate chuyển tiếp phương thức đến đối tượng được ủy quyền như mô tả, hoặc khi hành vi giống alias của nó được tài liệu hóa rõ ràng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Ruby
Star
2.3k
Fork
546
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của JSONAPI-Resources/jsonapi-resources

Tất cả issue của JSONAPI-Resources/jsonapi-resources

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.