Module: ConvenientService::Config

Included in:
Examples::Dry::Gemfile::DryService::Config, Examples::Dry::V1::Gemfile::DryService::Config, Examples::Rails::Gemfile::RailsService::Config, Examples::Rails::V1::Gemfile::RailsService::Config, Feature::Configs::Standard, Service::Configs::Standard, Service::Configs::Standard::V1
Defined in:
lib/convenient_service/config.rb,
lib/convenient_service/config/exceptions.rb,
lib/convenient_service/config/entities/option.rb,
lib/convenient_service/config/entities/options.rb,
lib/convenient_service/config/commands/normalize_options.rb

Defined Under Namespace

Modules: Commands, Entities, Exceptions

Class Method Summary collapse

Class Method Details

.included(mod) ⇒ void

This method returns an undefined value.

Parameters:

  • mod (Module)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/convenient_service/config.rb', line 18

def self.included(mod)
  mod.extend Dependencies::Extractions::ActiveSupportConcern::Concern

  mod.module_exec do
    class << self
      ##
      # @params klass [Class]
      # @return [void]
      #
      def eval_included_block(klass)
        klass.include ::ConvenientService::Core

        previous_options = klass.options.dup

        klass.options.replace(options)

        super
      ensure
        klass.options.replace(previous_options)
      end

      ##
      # @param options [Array<Symbol>]
      # @return [ConvenientService::Config]
      #
      def with(*options)
        dup.tap do |mod|
          mod.module_exec(base, self.options.dup.merge(options)) do |base, options|
            ##
            # @return [ConvenientService::Config]
            #
            define_singleton_method(:base) { base }

            ##
            # @return [Set]
            #
            define_singleton_method(:options) { options }
          end
        end
      end

      ##
      # @param options [Array<Symbol>]
      # @return [ConvenientService::Config]
      #
      def without(*options)
        dup.tap do |mod|
          mod.module_exec(base, self.options.dup.subtract(options)) do |base, options|
            ##
            # @return [ConvenientService::Config]
            #
            define_singleton_method(:base) { base }

            ##
            # @return [Set]
            #
            define_singleton_method(:options) { options }
          end
        end
      end

      ##
      # @return [ConvenientService::Config]
      #
      def with_defaults
        dup.tap do |mod|
          mod.module_exec(base, base.default_options) do |base, options|
            ##
            # @return [ConvenientService::Config]
            #
            define_singleton_method(:base) { base }

            ##
            # @return [Set]
            #
            define_singleton_method(:options) { options }
          end
        end
      end

      ##
      # @return [ConvenientService::Config]
      #
      def without_defaults
        dup.tap do |mod|
          mod.module_exec(base, Entities::Options.new) do |base, options|
            ##
            # @return [ConvenientService::Config]
            #
            define_singleton_method(:base) { base }

            ##
            # @return [Set]
            #
            define_singleton_method(:options) { options }
          end
        end
      end

      ##
      # @return [ConvenientService::Config]
      #
      def base
        self
      end

      ##
      # @return [ConvenientService::Config::Entities::Options]
      #
      def options
        @options ||= Entities::Options.new(options: default_options.dup)
      end

      ##
      # @param block [Proc, nil]
      # @return [ConvenientService::Config::Entities::Options]
      #
      def default_options(&block)
        block ? @default_options = Entities::Options.new(options: yield) : @default_options ||= Entities::Options.new
      end

      ##
      # @param other [Object] Can be any type.
      # @return [Boolean, nil]
      #
      def ==(other)
        return unless other.instance_of?(self.class)
        return false unless other.include?(::ConvenientService::Config)

        return false unless base.equal?(other.base)
        return false if options != other.options

        true
      end

      ##
      # @return [String]
      #
      def inspect
        name = base.name || "AnonymousConfig"

        options.any? ? "#{name}.with(#{options.keys.map(&:inspect).join(", ")})" : name
      end
    end
  end
end