From 8b9f43ab62891a3a4200b5c42df29e0bd3fc9641 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Fri, 12 Jan 2024 20:41:54 +0100 Subject: [PATCH] feat: use zeitwrek as autoloader --- lib/trailblazer-finder.rb | 1 + lib/trailblazer/finder.rb | 47 +++----- test/test_helper.rb | 3 +- test/trailblazer/finder/dsl_test.rb | 173 ---------------------------- trailblazer-finder.gemspec | 2 + 5 files changed, 19 insertions(+), 207 deletions(-) create mode 100644 lib/trailblazer-finder.rb delete mode 100644 test/trailblazer/finder/dsl_test.rb diff --git a/lib/trailblazer-finder.rb b/lib/trailblazer-finder.rb new file mode 100644 index 0000000..30427ca --- /dev/null +++ b/lib/trailblazer-finder.rb @@ -0,0 +1 @@ +require_relative 'trailblazer/finder' \ No newline at end of file diff --git a/lib/trailblazer/finder.rb b/lib/trailblazer/finder.rb index 9fa519c..78b6037 100644 --- a/lib/trailblazer/finder.rb +++ b/lib/trailblazer/finder.rb @@ -1,45 +1,28 @@ # frozen_string_literal: true -require "forwardable" -require "trailblazer" -require "dry-types" -require "ostruct" +require 'forwardable' +require 'dry-types' +require 'ostruct' +require 'zeitwerk' +require 'trailblazer/operation' +require 'trailblazer/macro' -require_relative "finder/utils/array" -require_relative "finder/utils/hash" -require_relative "finder/utils/string" -require_relative "finder/utils/splitter" -require_relative "finder/utils/extra" -require_relative "finder/adapters/active_record/predicates" -require_relative "finder/adapters/active_record/paging" -require_relative "finder/adapters/active_record/sorting" -require_relative "finder/adapters/kaminari/paging" -require_relative "finder/adapters/will_paginate/paging" -require_relative "finder/adapters/sequel/predicates" -require_relative "finder/adapters/sequel/paging" -require_relative "finder/adapters/sequel/sorting" -require_relative "finder/adapters/basic/predicates" -require_relative "finder/adapters/basic/paging" -require_relative "finder/adapters/basic/sorting" -require_relative "finder/activities/prepare" -require_relative "finder/activities/process" -require_relative "finder/activities/find" -require_relative "finder/helpers/basic" -require_relative "finder/helpers/sorting" -require_relative "finder/dsl" -require_relative "finder/find" -require_relative "finder/base" -require_relative "operation/finder" +loader = Zeitwerk::Loader.for_gem_extension(Trailblazer) +adapters = "#{__dir__}/finder/adapters" +loader.do_not_eager_load(adapters) +loader.setup module Trailblazer class Finder module Types include Dry.Types(default: :nominal) end - EXT_ORM_ADAPTERS = Types::Strict::String.enum("ActiveRecord", "Sequel") - ORM_ADAPTERS = EXT_ORM_ADAPTERS | Types::Strict::String.enum("Basic") - PAGING_ADAPTERS = Types::Strict::String.enum("Kaminari", "WillPaginate") + EXT_ORM_ADAPTERS = Types::Strict::String.enum('ActiveRecord', 'Sequel') + ORM_ADAPTERS = EXT_ORM_ADAPTERS | Types::Strict::String.enum('Basic') + PAGING_ADAPTERS = Types::Strict::String.enum('Kaminari', 'WillPaginate') include Base end end + +loader.eager_load diff --git a/test/test_helper.rb b/test/test_helper.rb index bd2191e..ffc8b5c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,9 +2,8 @@ require 'bundler/setup' require 'trailblazer/developer' -require 'trailblazer/activity' require 'trailblazer/activity/testing' -require 'trailblazer/finder' +require 'trailblazer-finder' require 'minitest-trailblazer' require 'minitest/trailblazer_spec' diff --git a/test/trailblazer/finder/dsl_test.rb b/test/trailblazer/finder/dsl_test.rb deleted file mode 100644 index e3aea5f..0000000 --- a/test/trailblazer/finder/dsl_test.rb +++ /dev/null @@ -1,173 +0,0 @@ -# frozen_string_literal: true - -require 'test_helper' -require 'support/operations' - -module Trailblazer - class Finder - class DslTest < Minitest::TrailblazerSpec - def define_finder_class(&block) - Class.new(Trailblazer::Finder) do - class_eval(&block) - end - end - - def finder_class(default_entity = [], &block) - define_finder_class do - entity { default_entity } - - class_eval(&block) unless block.nil? - end - end - - def new_finder(default_entity = [], filter = {}, &block) - finder_class(default_entity, &block).new params: filter - end - - describe '#adapters' do - it 'checks for a valid adapter and returns an error' do - entity = [{ id: 1, value: 'Test 1' }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity, value_eq: 'Test 1' do - adapter :NonExisting - end - - # expect(finder.errors).to eq [{adapter: "The specified adapter are invalid"}] - assert_equal finder.errors, [{ adapter: 'The specified adapter are invalid' }] - end - - it 'sets the adapter in the config' do - entity = [{ id: 1, value: 'Test 1' }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity, value_eq: 'Test 1' do - adapter 'ActiveRecord' - property :value, type: Base - end - - assert_equal finder.class.config.adapter, 'ActiveRecord' - end - end - - describe '#property' do - it 'checks for a valid property type and returns an error' do - entity = [{ id: 1, value: 'Test 1' }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity, value_eq: 'Test 1' do - property :value, type: Base - end - - assert_equal finder.errors, [{ properties: 'One or more properties are missing a valid type' }] - end - - it "sets the property and it's type properly in the config" do - entity = [{ id: 1, value: 'Test 1' }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity, value_eq: 'Test 1' do - property :value, type: Base - end - - assert_equal finder.class.config.properties, value: { type: Trailblazer::Finder::Base } - end - end - - describe '#paging' do - it 'sets the paging values' do - entity = [{ id: 1, value: 'Test 1' }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity do - paging per_page: 2, min_per_page: 1, max_per_page: 5 - property :value, type: Types::String - end - - assert_equal finder.class.config.paging[:per_page], 2 - assert_equal finder.class.config.paging[:min_per_page], 1 - assert_equal finder.class.config.paging[:max_per_page], 5 - end - - it "does not load paging stuff if paging isn't called in the finder class" do - entity = [{ id: 1, value: 'Test 1' }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity do - property :value, type: Types::String - end - - assert_empty finder.class.config.paging - end - end - - describe '#filter_by' do - it 'returns an error when supplied with a non symbol' do - entity = [1, 2, 3] - finder = new_finder entity, value_test: 'some' do - filter_by :value_test, with: 'test' - end - - assert_equal finder.result, errors: [{ filters: 'One or more filters are missing a with method definition' }] - end - - it 'has a default filter' do - entity = [{ id: 1, value: 'Test 1' }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity, value: 'Test 1' do - filter_by :value - end - - assert_equal finder.result.map { |n| n[:value] }, ['Test 1'] - end - - it "has a default filter working when it's nested" do - entity = [{ id: 1, value: [id: 4, value: 'Test 1'] }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity, value: 'Test 1' do - filter_by :value - end - - assert_equal finder.result.map { |n| n[:value] }, ['Test 1'] - assert_equal finder.result.map { |n| n[:id] }, [4] - end - - it 'has another default filter' do - entity = [{ id: 1, value: 'Test 1' }, { id: 2, value: 'Test 2' }, { id: 3, value: 'Test 3' }] - finder = new_finder entity, id: 2 do - filter_by :id - end - - assert_equal finder.result.map { |n| n[:value] }, ['Test 2'] - end - - it 'returns the entity if nil is returned' do - entity = [1, 2, 3] - finder = new_finder entity, value_test: 'some' do - filter_by :value_test do - nil - end - end - - assert_equal finder.result, entity - end - - it 'can use methods from the object' do - finder1 = new_finder [1, 2, 3], filter: 1 do - filter_by :filter do |entity, attribute, value| - some_instance_method(entity, attribute, value) - end - - private - - def some_instance_method(entity, _attribute, _value) - entity - [2, 3] - end - end - - assert_equal finder1.result, [1] - end - - it 'can dispatch with instance methods' do - finder = new_finder [1, 2, 3], filter: 1 do - filter_by :filter, with: :some_instance_method - - private - - def some_instance_method(entity, _attribute, value) - entity.select { |v| v == value } - end - end - - assert_equal finder.result, [1] - end - end - end - end -end diff --git a/trailblazer-finder.gemspec b/trailblazer-finder.gemspec index a4f8251..e0b06f0 100644 --- a/trailblazer-finder.gemspec +++ b/trailblazer-finder.gemspec @@ -18,7 +18,9 @@ Gem::Specification.new do |spec| spec.metadata['changelog_uri'] = 'https://github.com/trailblazer/trailblazer-finder/blob/master/CHANGELOG.md' spec.add_dependency "dry-types", ">= 1.0.0" + spec.add_dependency "trailblazer-macro", ">= 2.1.15" spec.add_dependency "trailblazer-activity", ">= 0.13.0" + spec.add_dependency "zeitwerk", ">= 2.6.0" spec.add_development_dependency "activerecord" spec.add_development_dependency "bundler"