Delegate is actually an alias
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia da JSONAPI::Resource e dalla gestione della delega dell'opzione attributes, quindi confronta il suo comportamento con gli esempi di delega di Ruby e Rails nell'issue. Il lavoro è completato quando l'opzione delegate inoltra il metodo all'oggetto delegato come descritto, oppure quando il suo comportamento simile a un alias è documentato chiaramente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Ruby
- Stelle
- 2.3k
- Fork
- 546
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di JSONAPI-Resources/jsonapi-resources
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
-
ArgumentError with Rails 8.1 Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 50/100
JSONAPI-Resources/jsonapi-resources#1488 · 1 commento ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
JSONAPI-Resources/jsonapi-resources#1479 · 2 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 30/100
JSONAPI-Resources/jsonapi-resources#1477 · 3 commenti · 1 reazione ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
Tutte le issue di JSONAPI-Resources/jsonapi-resources
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
riscv/riscv-unified-db#2626 ·
-
Component: GLib
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
ds-drift
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
we-promise/sure#3693 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
simp/pupmod-simp-simp#395 ·