Delegate is actually an alias
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
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
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của JSONAPI-Resources/jsonapi-resources
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
ArgumentError with Rails 8.1 Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 50/100
JSONAPI-Resources/jsonapi-resources#1488 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
JSONAPI-Resources/jsonapi-resources#1479 · 2 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
JSONAPI-Resources/jsonapi-resources#1477 · 3 bình luận · 1 reaction ·
-
Fields filtering with include Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
Tất cả issue của JSONAPI-Resources/jsonapi-resources
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
riscv/riscv-unified-db#2626 ·
-
Component: GLib
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
ds-drift
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
we-promise/sure#3693 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
simp/pupmod-simp-simp#395 ·