forked from Empact/roxml
-
Notifications
You must be signed in to change notification settings - Fork 108
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
Implement Representable::Object#to_object #266
Open
elShiaLabeouf
wants to merge
4
commits into
trailblazer:master
Choose a base branch
from
elShiaLabeouf:feature/represent-to-object
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ee73fe0
Implement Representable::Object#to_object
elShiaLabeouf d81728a
fix tests according to expected wrap: behavior
elShiaLabeouf 061fa0f
rewrite tests according to a new assert_equal syntax
elShiaLabeouf fcf985d
rename to_object to to_struct
elShiaLabeouf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ Gemfile.lock | |
*.swp | ||
*.swo | ||
bin | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'representable/hash' | ||
|
||
module Representable::Object | ||
module Collection | ||
include Representable::Object | ||
|
||
def self.included(base) | ||
base.class_eval do | ||
include Representable::Object | ||
extend ClassMethods | ||
property(:_self, {:collection => true}) | ||
end | ||
end | ||
|
||
|
||
module ClassMethods | ||
def items(options={}, &block) | ||
collection(:_self, options.merge(:getter => lambda { |*| self }), &block) | ||
end | ||
end | ||
|
||
# TODO: revise lonely collection and build separate pipeline where we just use Serialize, etc. | ||
|
||
def create_representation_with(doc, options, format) | ||
options = normalize_options(**options) | ||
options[:_self] = options | ||
|
||
bin = representable_bindings_for(format, options).first | ||
|
||
Collect[*bin.default_render_fragment_functions]. | ||
(represented, {doc: doc, fragment: represented, options: options, binding: bin, represented: represented}) | ||
end | ||
|
||
def update_properties_from(doc, options, format) | ||
options = normalize_options(**options) | ||
options[:_self] = options | ||
|
||
bin = representable_bindings_for(format, options).first | ||
|
||
value = Collect[*bin.default_parse_fragment_functions]. | ||
(doc, fragment: doc, document: doc, options: options, binding: bin, represented: represented) | ||
|
||
represented.replace(value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
then it should be to_struct.
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.
What if we make the object type configurable and
Struct
is one of the possible targets?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.
I like this. But having a to_struct shortcut is good too.
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.
I agree,
module Struct
andto_struct
fit more, asto_object
is too vague. Moved the new code to a new module.Frankly, I'm kinda confused about
configurable object type
with Struct as one of the possible targets. What other possible target there could be..? OpenStruct / AnyCustomUserClassPassedAsArgument? I feel like Struct is sufficient and there's no need for other optionsThere 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.
Object can return the original object such as ActiveRecord model or a Poro with it attributes set.
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.
Imagine a project with
Domain::Song
,Domain::Song::Duration
,Domain::Artist
, etc. People might want to transform an object to another domain object?!