-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/mango #143
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
Merged
Merged
Feature/mango #143
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aa660a4
Fix config file namespace access
ErickFabian 7c614ea
Add couch base model and read me for migrating from couch 1.x to couc…
ErickFabian 2b4618d
Add base mango querys
ErickFabian 7aea9a5
Compact pr
ErickFabian c8e9a84
Refactors and improvements
ErickFabian 16336c1
Support deeply nested operator combinations
ErickFabian af875df
Better error handling
ErickFabian fe79694
Add exception
ErickFabian 16e3060
Add mango index management
ErickFabian 4af2bbe
Separate mango indexes for another pr
ErickFabian 821304a
Add tests for mango querys
ErickFabian c1f554a
fix bundler merge conflict
ErickFabian af03c18
Remove unrelated space change
ErickFabian ad07fba
Attend comments
ErickFabian 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 hidden or 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,58 @@ | ||
| PATH | ||
| remote: . | ||
| specs: | ||
| dolly (3.0.0) | ||
| oj | ||
|
|
||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| addressable (2.6.0) | ||
| public_suffix (>= 2.0.2, < 4.0) | ||
| crack (0.4.3) | ||
| safe_yaml (~> 1.0.0) | ||
| hashdiff (0.4.0) | ||
| metaclass (0.0.4) | ||
| mocha (1.9.0) | ||
| metaclass (~> 0.0.1) | ||
| oj (3.7.12) | ||
| power_assert (1.1.4) | ||
| public_suffix (3.1.0) | ||
| rake (10.5.0) | ||
| rr (1.2.1) | ||
| safe_yaml (1.0.5) | ||
| test-unit (3.3.3) | ||
| power_assert | ||
| test-unit-context (0.5.1) | ||
| test-unit (>= 2.4.0) | ||
| test-unit-full (0.0.5) | ||
| test-unit | ||
| test-unit-context | ||
| test-unit-notify | ||
| test-unit-rr | ||
| test-unit-runner-tap | ||
| test-unit-notify (1.0.4) | ||
| test-unit (>= 2.4.9) | ||
| test-unit-rr (1.0.5) | ||
| rr (>= 1.1.1) | ||
| test-unit (>= 2.5.2) | ||
| test-unit-runner-tap (1.1.2) | ||
| test-unit | ||
| webmock (3.6.0) | ||
| addressable (>= 2.3.6) | ||
| crack (>= 0.3.2) | ||
| hashdiff (>= 0.4.0, < 2.0.0) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| bundler (~> 2.0) | ||
| dolly! | ||
| mocha | ||
| rake (~> 10.0) | ||
| test-unit-full | ||
| webmock | ||
|
|
||
| BUNDLED WITH | ||
| 2.0.2 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'refinements/hash_refinements' | ||
|
|
||
| module Dolly | ||
| module Mango | ||
| using HashRefinements | ||
|
|
||
| SELECTOR_SYMBOL = '$' | ||
|
|
||
| COMBINATION_OPERATORS = %I[ | ||
| and | ||
| or | ||
| not | ||
| nor | ||
| all | ||
| elemMatch | ||
| allMath | ||
| ].freeze | ||
|
|
||
| CONDITION_OPERATORS = %I[ | ||
| lt | ||
| lte | ||
| eq | ||
| ne | ||
| gte | ||
| gt | ||
| exists | ||
| type | ||
| in | ||
| nin | ||
| size | ||
| mod | ||
| regex | ||
| ].freeze | ||
|
|
||
| ALL_OPERATORS = COMBINATION_OPERATORS + CONDITION_OPERATORS | ||
|
|
||
| DESIGN = '_find' | ||
|
|
||
| def find_by(query, opts = {}) | ||
| build_model_from_doc(find_doc_by(query, opts)) | ||
| end | ||
|
|
||
| def find_doc_by(query, opts = {}) | ||
| opts.merge!(limit: 1) | ||
| perform_query(build_query(query, opts))[:docs].first | ||
| end | ||
|
|
||
| def where(query, opts = {}) | ||
| docs_where(query, opts).map do |doc| | ||
| build_model_from_doc(doc) | ||
| end | ||
| end | ||
|
|
||
| def docs_where(query, opts = {}) | ||
| perform_query(build_query(query, opts))[:docs] | ||
|
javierg marked this conversation as resolved.
|
||
| end | ||
|
|
||
| private | ||
|
|
||
| def build_model_from_doc(doc) | ||
| return nil if doc.nil? | ||
| new(doc.slice(*all_property_keys)) | ||
| end | ||
|
|
||
| def perform_query(structured_query) | ||
| connection.post(DESIGN, structured_query) | ||
| end | ||
|
|
||
| def build_query(query, opts) | ||
| { 'selector' => build_selectors(query) }.merge(opts) | ||
| end | ||
|
|
||
| def build_selectors(query) | ||
| query.deep_transform_keys do |key| | ||
| next build_key(key) if is_operator?(key) | ||
| raise InvalidMangoOperatorError.new(key) unless self.all_property_keys.include?(key) | ||
| key | ||
| end | ||
| end | ||
|
|
||
| def build_key(key) | ||
| "#{SELECTOR_SYMBOL}#{key}" | ||
| end | ||
|
|
||
| def is_operator?(key) | ||
| ALL_OPERATORS.include?(key) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,27 @@ | ||
| module HashRefinements | ||
| refine Hash do | ||
| # File activesupport/lib/active_support/core_ext/hash/keys.rb, line 82 | ||
| def deep_transform_keys(&block) | ||
| _deep_transform_keys_in_object(self, &block) | ||
| end | ||
|
|
||
| def slice(*keys) | ||
| keys.each_with_object(Hash.new) { |k, hash| hash[k] = self[k] if has_key?(k) } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def _deep_transform_keys_in_object(object, &block) | ||
| case object | ||
| when Hash | ||
| object.each_with_object({}) do |(key, value), result| | ||
| result[yield(key)] = _deep_transform_keys_in_object(value, &block) | ||
| end | ||
| when Array | ||
| object.map { |e| _deep_transform_keys_in_object(e, &block) } | ||
| else | ||
| object | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.