JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Delegate is actually an alias
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Ruby
- Estrellas
- 2.3k
- Forks
- 546
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza en JSONAPI::Resource y en el manejo de delegación de la opción attributes; después, compara su comportamiento con los ejemplos de delegación de Ruby y Rails incluidos en el issue. Se considera terminado cuando la opción delegate reenvía el método al objeto delegado tal como se describe, o cuando su comportamiento similar a un alias está claramente documentado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- rails, ruby
- Área
- api
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100