-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from pfeiffer/refactor-relations
Refactor `Relation` and conditions to fix mutating scopes issues
- Loading branch information
Showing
7 changed files
with
239 additions
and
124 deletions.
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
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,44 @@ | ||
class ActiveHash::Relation::Condition | ||
attr_reader :constraints, :inverted | ||
|
||
def initialize(constraints) | ||
@constraints = constraints | ||
@inverted = false | ||
end | ||
|
||
def invert! | ||
@inverted = !inverted | ||
|
||
self | ||
end | ||
|
||
def matches?(record) | ||
match = begin | ||
return true unless constraints | ||
|
||
expectation_method = inverted ? :any? : :all? | ||
|
||
constraints.send(expectation_method) do |attribute, expected| | ||
value = record.public_send(attribute) | ||
|
||
matches_value?(value, expected) | ||
end | ||
end | ||
|
||
inverted ? !match : match | ||
end | ||
|
||
private | ||
|
||
def matches_value?(value, comparison) | ||
return comparison.any? { |v| matches_value?(value, v) } if comparison.is_a?(Array) | ||
return comparison.cover?(value) if comparison.is_a?(Range) | ||
return comparison.match?(value) if comparison.is_a?(Regexp) | ||
|
||
normalize(value) == normalize(comparison) | ||
end | ||
|
||
def normalize(value) | ||
value.respond_to?(:to_s) ? value.to_s : value | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class ActiveHash::Relation::Conditions | ||
attr_reader :conditions | ||
|
||
delegate :<<, :map, to: :conditions | ||
|
||
def initialize(conditions = []) | ||
@conditions = conditions | ||
end | ||
|
||
def matches?(record) | ||
conditions.all? do |condition| | ||
condition.matches?(record) | ||
end | ||
end | ||
|
||
def self.wrap(conditions) | ||
return conditions if conditions.is_a?(self) | ||
|
||
new(conditions) | ||
end | ||
end |
Oops, something went wrong.