# frozen_string_literal: true require_relative 'ruby-rails-extensions/errors' module RubyRailsExtensions class Configuration # @type [Array] All supported "gems" will be placed here for # configuration for enabling/disabling. BOOLEAN_GEMS = %i[ all_keys all_values any_key any_value assign_nil boolean_scope clean_string compact_map compact_blank_join current_day current_month current_week cutoff day_of_week display_military display_user find_bang find_dupes first_dupe flatten_compact google_format hash_only humanize_symbol in_utc input invert_with_dups left_deep_merge month_and_year month_year nmerge no_keys no_values only_some overlaps pipe presence_bang quarter_dates quarter_str range_add range_comparisons range_multiply range_round remove_reserved_keywords safe_parse select_present select_present_join set_to_sentence to_bool to_d to_dec to_delimited to_i to_local to_money to_negative_i to_new_date to_new_time to_nonzero_i to_or_sentence to_percentage to_positive_i to_sort_i to_x todays_date uniq_map usd_to_f usd_to_i utf8_encode weighted_sum yesno zero_range ].freeze attr_accessor :default_time_zone # @return [Symbol] the flag name def self.flag_name(gem_base) :"include_#{gem_base}" end BOOLEAN_GEMS.each do |extension_name| include_extension = flag_name(extension_name) attr_accessor include_extension define_method(:"#{extension_name}?") do public_send(include_extension) != false end end # Sets all flags to true by default def initialize super BOOLEAN_GEMS.each do |extension_name| include_extension = flag_name(extension_name) public_send(:"#{include_extension}=", true) end end # @return [Array] def flags BOOLEAN_GEMS.map { |f| flag_name(f) } end private # @return [Symbol] the flag name def flag_name(gem_base) self.class.flag_name(gem_base) end end module_function # @return [Configuration] def configuration @configuration ||= Configuration.new end # @yield [Configuration] def configure yield(configuration) Configuration::BOOLEAN_GEMS.each do |extension| next unless configuration.public_send(:"#{extension}?") require_relative("ruby-rails-extensions/extensions/#{extension}") end end # @return [Array] def configuration_flags configuration.flags end # @see Object#presence from active_support/core_ext/object/blank.rb def presence(item) return item if present?(item) nil end # @see Object#blank? from active_support/core_ext/object/blank.rb def blank?(item) if item.respond_to?(:blank?) return item.blank? end item.respond_to?(:empty?) ? !!item.empty? : !item end # @see Object#present? from active_support/core_ext/object/blank.rb def present?(item) if item.respond_to?(:present?) return item.present? end !blank?(item) end end