Module: WrapIt::Arguments::ClassMethods
- Included in:
- WrapIt::Arguments, Base
- Defined in:
- lib/wrap_it/arguments.rb
Overview
WrapIt::Arguments Class methods to include
Instance Method Summary collapse
-
#argument(name, opts = {}) {|name, value| ... } ⇒ void
Desclares argument for capturing on initialization process.
-
#capture_arguments!(args, opts = {}, &block) ⇒ Array<Object>
Capture arguments for class and it's ancestors.
-
#option(name, opts = {}) {|name, value| ... } ⇒ void
Desclares option for capturing on initialization process.
Instance Method Details
#argument(name, opts = {}) {|name, value| ... } ⇒ void
This method returns an undefined value.
Desclares argument for capturing on initialization process.
Inside initialization process, all arguments (except options hash),
passed to constructor will be inspected to satisfy conditions,
specified in :if and :and options. If this happens, and block
given, it evaluated in context of component instance. If no block
given, setter with name will be attempted to set value. In any way
if conditions satisfied, argument removed from future processing.
If no conditions specified, the name of attribute taked as only
condition.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/wrap_it/arguments.rb', line 94 def argument(name, first_only: false, after_options: false, **opts, &block) name.is_a?(String) && name = name.to_sym fail ArgumentError, 'Wrong name' unless name.is_a?(Symbol) arguments[name] = { name: name, conditions: Arguments.make_conditions(name, **opts), block: block, first_only: first_only == true, after_options: == true } end |
#capture_arguments!(args, opts = {}, &block) ⇒ Array<Object>
Capture arguments for class and it's ancestors. All captured arguments
and options will be extracted from original args argument.
Actually you rare needs to call this method directly. For example you can call it in instance capture_arguments! override to capture arguments for some child components.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/wrap_it/arguments.rb', line 200 def capture_arguments!(args, inherited = true, instance = nil, &block) opts = args. if inherited arg_parents.select { |a| a.protected_methods.include?(:extract_for_class) } .each { |a| a.extract_for_class(args, opts, instance, &block) } result_args = collect_derived(:@provided_arguments, {}, :merge) .values .flatten result_opts = collect_derived(:@provided_options, {}, :merge) .values .reduce({}) { |a, e| a.merge!(e) } else extract_for_class(args, opts, instance, &block) result_args = @provided_arguments.values.flatten result_opts = .values .reduce({}) { |a, e| a.merge!(e) } end opts.empty? || args << opts result_opts.empty? || result_args << result_opts result_args end |
#option(name, opts = {}) {|name, value| ... } ⇒ void
This method returns an undefined value.
Desclares option for capturing on initialization process.
Provides same manner as #argument but for hash of options, passed to constructor. Specified conditions are applied to options keys, not to values.
Hint: you can specify argument and options with same name to call same setter.
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/wrap_it/arguments.rb', line 154 def option(name, after: nil, **opts, &block) name.is_a?(String) && name = name.to_sym fail ArgumentError, 'Wrong name' unless name.is_a?(Symbol) @dependencies = !after.nil? [name] = { name: name, conditions: Arguments.make_conditions(name, **opts), block: block } end |