Skip to content

Commit

Permalink
explicitly apply query blocks in definition order
Browse files Browse the repository at this point in the history
Description:
- during scope resolution, if query blocks have the same index,
explicitly apply them in definition order.
- the above is intended to be default behavior, but it appears that
under mac os when sorting array of objects by criteria, and this
criteria is same among objects (i.e. array is already sorted), their
order gets messed compared to initial array

References:
- #4
  • Loading branch information
akuzko committed Jul 9, 2017
1 parent 7de86a0 commit 7feda68
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/parascope/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def sifted_instance

def resolved_scope!
guard_all
klass.query_blocks.sort{ |a, b| a.index <=> b.index }.reduce(scope) do |scope, block|
klass.sorted_query_blocks.reduce(scope) do |scope, block|
clone_with_scope(scope, block).apply_block!.scope
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/parascope/query/api_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,13 @@ def query_blocks
def guard_blocks
@guard_blocks ||= []
end

def sorted_query_blocks
query_blocks.sort do |a, b|
a.index == b.index \
? query_blocks.index(a) <=> query_blocks.index(b)
: a.index <=> b.index
end
end
end
end
12 changes: 7 additions & 5 deletions spec/parascope/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,18 @@ def foo_scope

describe 'query application order control' do
feature do
base_scope { OpenStruct.new(foo: 'value') }
query_by(:foo) { scope.tap{ scope.foo << '-foo' } }
query_by(:bar, index: -1) { scope.tap{ scope.foo.replace('bar-' + scope.foo) } }
base_scope { OpenStruct.new(value: []) }

query { scope.tap{ scope.value << 'bar' } }
query(index: -1) { scope.tap{ scope.value << 'foo' } }
query { scope.tap{ scope.value << 'baz' } }
end

let(:query) { query_klass.new(foo: true, bar: true) }
let(:query) { query_klass.build }

subject { query.resolved_scope.to_h }

it { is_expected.to eq(foo: 'bar-value-foo') }
it { is_expected.to eq(value: ['foo', 'bar', 'baz']) }
end
end

Expand Down

0 comments on commit 7feda68

Please sign in to comment.