-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use return value of link block if any #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,29 +10,40 @@ def initialize(serializer) | |
|
||
def href(value) | ||
self._href = value | ||
nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason for an explicit |
||
end | ||
|
||
def meta(value) | ||
self._meta = value | ||
nil | ||
end | ||
|
||
def string(value) | ||
self._string = value | ||
def value(value) | ||
if value.respond_to?(:call) | ||
string instance_eval(&value) | ||
_string || to_hash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand those 2 lines though. This will always return def initialize(serializer, link_value)
@object = serializer.object
@scope = serializer.scope
if link_value.respond_to?(:call)
instance_eval(&link_value)
else
string link_value
end
end
def value
_string || to_hash
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh actually, your idea of using the return value of the block makes for quite a cool DSL. My suggestion of moving the actual computing of the value inside the initializer still stands, but other than this I think you're right, this is the way to go. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could go either way. Like I wrote in the description
This is more of an interface proposal than implementation. I considered the initializer, but thought this was more similar to our other implementations, e.g. Reflection. but I don't feel strongly about it. I'd prefer you to merge it (if you think it's an improvement), and then add a commit or two to handle the unresolved issue I just quoted, and where the logic should go internal to the 'Link' |
||
else | ||
value | ||
end | ||
end | ||
|
||
def to_hash | ||
return _string unless _string.nil? | ||
protected | ||
|
||
attr_accessor :_href, :_meta, :_string | ||
attr_reader :object, :scope | ||
|
||
private | ||
|
||
def to_hash | ||
hash = { href: _href } | ||
hash.merge!(meta: _meta) if _meta | ||
|
||
hash | ||
end | ||
|
||
protected | ||
|
||
attr_accessor :_href, :_meta, :_string | ||
attr_reader :object, :scope | ||
def string(value) | ||
self._string = value | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ class LinkAuthorSerializer < ActiveModel::Serializer | |
link :other, '//example.com/resource' | ||
|
||
link :yet_another do | ||
string "//example.com/resource/#{object.id}" | ||
"//example.com/resource/#{object.id}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the serializer API change |
||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to moving this logic inside the
Link
class, which is in line with what we've done withAttribute
/Reflection
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄