Skip to content

Commit

Permalink
Filter by definition_group
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Apr 18, 2024
1 parent 026de21 commit 910c890
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/diver_down/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def call(env)
page: request.params['page']&.to_i || 1,
per: request.params['per']&.to_i || 100,
title: request.params['title'] || '',
source: request.params['source'] || ''
source: request.params['source'] || '',
definition_group: request.params['definition_group'] || ''
)
in ['GET', %r{\A/api/sources\.json\z}]
action.sources
Expand Down
5 changes: 3 additions & 2 deletions lib/diver_down/web/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ def module(module_names)
# @param per [Integer]
# @param title [String]
# @param source [String]
def definitions(page:, per:, title:, source:)
definition_enumerator = DiverDown::Web::DefinitionEnumerator.new(@store, title:, source:)
# @param definition_group [String]
def definitions(page:, per:, title:, source:, definition_group:)
definition_enumerator = DiverDown::Web::DefinitionEnumerator.new(@store, title:, source:, definition_group:)
definitions, pagination = paginate(definition_enumerator, page, per)

json(
Expand Down
8 changes: 7 additions & 1 deletion lib/diver_down/web/definition_enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class DefinitionEnumerator

# @param store [DiverDown::Definition::Store]
# @param query [String]
def initialize(store, title: '', source: '')
# @param definition_group [String]
def initialize(store, title: '', source: '', definition_group: '')
@store = store
@title = title
@source = source
@definition_group = definition_group
end

# @yield [parent_bit_id, bit_id, definition]
Expand Down Expand Up @@ -47,6 +49,10 @@ def match_definition?(definition)
matched &&= definition.sources.any? { _1.source_name.include?(@source) }
end

unless @definition_group.empty?
matched &&= definition.definition_group.to_s.include?(@definition_group)
end

matched
end
end
Expand Down
39 changes: 39 additions & 0 deletions spec/diver_down/web/definition_enumerator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,45 @@
)
end

describe 'with definition_group' do
def assert_query(store, definition_group, expected)
actual = described_class.new(store, definition_group:).each.to_a
expect(actual).to eq(expected), -> {
"definition_group: #{definition_group.inspect}\n" \
"expected: #{expected.inspect}\n" \
"actual: #{actual.inspect}"
}
end

it 'filters by definition_group' do
store = DiverDown::Web::DefinitionStore.new

definition_1 = DiverDown::Definition.new(
definition_group: 'group_1',
sources: [
DiverDown::Definition::Source.new(
source_name: 'a.rb'
),
]
)
definition_2 = DiverDown::Definition.new(
definition_group: 'group_2',
sources: [
DiverDown::Definition::Source.new(
source_name: 'b.rb'
),
]
)

store.set(definition_1, definition_2)

assert_query store, 'unknown', []
assert_query store, 'group', [definition_1, definition_2]
assert_query store, 'group_1', [definition_1]
assert_query store, 'group_2', [definition_2]
end
end

describe 'with title' do
def assert_query(store, title, expected)
actual = described_class.new(store, title:).each.to_a
Expand Down
41 changes: 41 additions & 0 deletions spec/diver_down/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,47 @@ def assert_source(source, expected_ids)
assert_source 'b.', [definition_2_id]
end
end

describe 'definition_group' do
def assert_definition_group(definition_group, expected_ids)
get "/api/definitions.json?definition_group=#{definition_group}"

definitions = JSON.parse(last_response.body)['definitions']
ids = definitions.map { _1['id'] }

expect(ids).to match_array(expected_ids), -> {
"definition_group: #{definition_group.inspect}\n" \
"expected_ids: #{expected_ids.inspect}\n" \
"actual_ids: #{ids.inspect}"
}
end

it 'filters definitions by definition_group=value' do
definition_1 = DiverDown::Definition.new(
definition_group: 'group_1',
sources: [
DiverDown::Definition::Source.new(
source_name: 'a.rb'
),
]
)
definition_2 = DiverDown::Definition.new(
definition_group: 'group_2',
sources: [
DiverDown::Definition::Source.new(
source_name: 'b.rb'
),
]
)

definition_1_id, definition_2_id = store.set(definition_1, definition_2)

assert_definition_group 'unknown', []
assert_definition_group 'group', [definition_1_id, definition_2_id]
assert_definition_group 'group_1', [definition_1_id]
assert_definition_group 'group_2', [definition_2_id]
end
end
end

describe 'GET /api/initialization_status.json' do
Expand Down

0 comments on commit 910c890

Please sign in to comment.