Skip to content

Commit

Permalink
chore: ensure Options are initialised with defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
pbernays committed Dec 31, 2024
1 parent 7746f54 commit 546a7d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 112 deletions.
135 changes: 29 additions & 106 deletions lib/annotate_rb/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Options

class << self
def from(options = {}, state = {})
new(options, state).load_defaults
new(options, state)
end
end

Expand All @@ -36,7 +36,6 @@ def from(options = {}, state = {})
exclude_sti_subclasses: false, # ModelAnnotator
exclude_tests: false, # ModelAnnotator
force: false, # ModelAnnotator, but should be used by both
format_bare: true, # Unused
format_markdown: false, # ModelAnnotator, RouteAnnotator
format_rdoc: false, # ModelAnnotator
format_yard: false, # ModelAnnotator
Expand Down Expand Up @@ -92,102 +91,61 @@ def from(options = {}, state = {})

DEFAULT_OPTIONS = {}.merge(POSITION_OPTIONS, FLAG_OPTIONS, OTHER_OPTIONS, PATH_OPTIONS).freeze

FLAG_OPTION_KEYS = [
:classified_sort,
:exclude_controllers,
:exclude_factories,
:exclude_fixtures,
:exclude_helpers,
:exclude_scaffolds,
:exclude_serializers,
:exclude_sti_subclasses,
:exclude_tests,
:force,
:format_markdown,
:format_rdoc,
:format_yard,
:frozen,
:ignore_model_sub_dir,
:ignore_unknown_models,
:include_version,
:show_check_constraints,
:show_complete_foreign_keys,
:show_foreign_keys,
:show_indexes,
:simple_indexes,
:sort,
:timestamp,
:trace,
:with_comment,
:with_column_comments,
:with_table_comments
].freeze

OTHER_OPTION_KEYS = [
:active_admin,
:command,
:debug,
:hide_default_column_types,
:hide_limit_column_types,
:ignore_columns,
:ignore_routes,
:ignore_unknown_models,
:models,
:routes,
:skip_on_db_migrate,
:target_action,
:wrapper,
:wrapper_close,
:wrapper_open,
:classes_default_to_s
].freeze

PATH_OPTION_KEYS = [
:additional_file_patterns,
:model_dir,
:require,
:root_dir
].freeze

ALL_OPTION_KEYS = [
POSITION_OPTIONS.keys, FLAG_OPTION_KEYS, OTHER_OPTION_KEYS, PATH_OPTION_KEYS
].flatten.freeze

POSITION_DEFAULT = "before"

# Want this to be read only after initializing
def_delegator :@options, :[]
def_delegators :@options, :[], :to_h

def initialize(options = {}, state = {})
@options = options

# For now, state is a hash to store state that we need but is not a configuration option
@state = state

symbolize_exclude_tests
load_defaults
@options.freeze
end

def set_state(key, value, overwrite = false)
if @state.key?(key) && !overwrite
val = @state[key]
raise ArgumentError, "Attempting to write '#{value}' to state with key '#{key}', but it already exists with '#{val}'."
end

@state[key] = value
end

def get_state(key)
@state[key]
end

def to_h
@options.to_h
def print
# TODO: prints options and state
end

private

def load_defaults
ALL_OPTION_KEYS.each do |key|
@options[key] = DEFAULT_OPTIONS[key] unless @options.key?(key)
@options = DEFAULT_OPTIONS.merge(@options)

# `:exclude_tests` option is being expanded to function as a boolean OR an array of symbols
# https://github.com/drwl/annotaterb/issues/103
if @options[:exclude_tests].is_a?(Array)
@options[:exclude_tests].map! { |item| item.to_s.strip.to_sym }
end

# Set all of the position options in the following order:
# 1) Use the value if it's defined
# 2) Use value from :position if it's defined
# 3) Use default
POSITION_OPTIONS.keys.each do |key|
POSITION_OPTIONS.each_key do |key|
@options[key] = Helper.fallback(
@options[key], @options[:position], POSITION_DEFAULT
)
end

# Unpack path options if we're passed in a String
PATH_OPTION_KEYS.each do |key|
PATH_OPTIONS.each_key do |key|
if @options[key].is_a?(String)
@options[key] = @options[key].split(",").map(&:strip).reject(&:empty?)
end
Expand All @@ -200,41 +158,6 @@ def load_defaults
# Set column and table comments to default to :with_comment, if not set
@options[:with_column_comments] = @options[:with_comment] if @options[:with_column_comments].nil?
@options[:with_table_comments] = @options[:with_comment] if @options[:with_table_comments].nil?

self
end

def set_state(key, value, overwrite = false)
if @state.key?(key) && !overwrite
val = @state[key]
raise ArgumentError, "Attempting to write '#{value}' to state with key '#{key}', but it already exists with '#{val}'."
end

@state[key] = value
end

def get_state(key)
@state[key]
end

def print
# TODO: prints options and state
end

private

# Guard against user inputting strings instead of symbols
def symbolize_exclude_tests
# `:exclude_tests` option is being expanded to function as a boolean OR an array of symbols
# https://github.com/drwl/annotaterb/issues/103

if @options[:exclude_tests].is_a?(Array)
@options[:exclude_tests].map! do |item|
item = item.strip if item.respond_to?(:strip)

item.to_sym
end
end
end
end
end
6 changes: 0 additions & 6 deletions spec/lib/annotate_rb/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@

it { expect(subject[:exclude_tests]).to eq(false) }
end
end

describe ".load_defaults" do
subject { described_class.new(options, state).load_defaults }

let(:state) { {} }

context 'when default value of "show_complete_foreign_keys" is not set' do
let(:key) { :show_complete_foreign_keys }
Expand Down

0 comments on commit 546a7d9

Please sign in to comment.