-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change configuration from hash to configuration object
- Loading branch information
Showing
11 changed files
with
100 additions
and
74 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
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 |
---|---|---|
@@ -1,72 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
module Trailblazer | ||
class Finder | ||
class Configuration | ||
attr_accessor :entity, :paging, :properties, :sorting, | ||
:filters, :adapter, :paginator | ||
|
||
def initialize | ||
@paging = {} | ||
@properties = {} | ||
@sorting = {} | ||
@filters = {} | ||
@paginator = nil | ||
@adapter = "Basic" | ||
end | ||
|
||
def clone | ||
new_config = Configuration.new | ||
new_config.entity = entity | ||
new_config.paging = paging.clone | ||
new_config.properties = properties.clone | ||
new_config.sorting = sorting.clone | ||
new_config.filters = filters.clone | ||
new_config.adapter = adapter | ||
new_config.paginator = paginator | ||
new_config | ||
end | ||
end | ||
|
||
module Dsl | ||
def config | ||
@config ||= apply_config({}) | ||
@config ||= Configuration.new | ||
end | ||
|
||
|
||
def inherited(base) | ||
## We don't want to inherit the config from Trailblazer::Finder | ||
return if name == 'Trailblazer::Finder' | ||
|
||
base.apply_config(config) | ||
base.config = config.clone | ||
end | ||
|
||
def entity(&block) | ||
config[:entity] = block | ||
config.entity = block | ||
end | ||
|
||
def paging(**options) | ||
config[:paging][:per_page] = options[:per_page] || 25 | ||
config[:paging][:min_per_page] = options[:min_per_page] || 10 | ||
config[:paging][:max_per_page] = options[:max_per_page] || 100 | ||
def paging(per_page: 25, min_per_page: 10, max_per_page: 100) | ||
config.paging[:per_page] = per_page | ||
config.paging[:min_per_page] = min_per_page | ||
config.paging[:max_per_page] = max_per_page | ||
end | ||
|
||
def property(name, options = {}) | ||
config[:properties][name] = options | ||
config[:properties][name][:type] = options[:type] || Types::String | ||
config[:sorting][name] = options[:sort_direction] || :desc if options[:sortable] | ||
config.properties[name] = options | ||
config.properties[name][:type] = options[:type] || Types::String | ||
config.sorting[name] = options[:sort_direction] || :desc if options[:sortable] | ||
end | ||
|
||
def filter_by(name, options = {}, &block) | ||
filter_name = name.to_sym | ||
config[:filters][filter_name] = {} | ||
config[:filters][filter_name][:name] = name | ||
config[:filters][filter_name][:with] = options[:with] if options.include?(:with) | ||
config[:filters][filter_name][:block] = block || nil | ||
config.filters[filter_name] = {} | ||
config.filters[filter_name][:name] = name | ||
config.filters[filter_name][:with] = options[:with] if options.include?(:with) | ||
config.filters[filter_name][:block] = block || nil | ||
end | ||
|
||
def adapter(adapter) | ||
config[:adapter] = adapter.to_s | ||
def adapter(adapter_name) | ||
config.adapter = adapter_name.to_s | ||
end | ||
|
||
def paginator(paginator) | ||
config[:paginator] = paginator.to_s | ||
def paginator(paginator_name) | ||
config.paginator = paginator_name.to_s | ||
end | ||
|
||
def current_adapter | ||
config[:adapter] | ||
config.adapter | ||
end | ||
|
||
def current_paginator | ||
config[:paginator] | ||
config.paginator | ||
end | ||
|
||
def filters_count | ||
config.filters.count | ||
end | ||
|
||
def apply_config(options, **) | ||
return @config = options unless options.empty? | ||
|
||
@config = { | ||
actions: {}, | ||
entity: nil, | ||
properties: {}, | ||
filters: {}, | ||
paging: {}, | ||
sorting: {}, | ||
adapters: [] | ||
} | ||
def properties_count | ||
config.properties.count | ||
end | ||
|
||
protected | ||
|
||
attr_writer :config | ||
end | ||
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
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