JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Delegate is actually an alias
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 2.3k
- Forks
- 546
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at JSONAPI::Resource and the attributes option's delegate handling, then compare its behavior with the Ruby and Rails delegation examples in the issue. Done means the delegate option either forwards the method to the delegated object as described, or its alias-like behavior is clearly documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100