Class: CMDx::Inputs

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdx/inputs.rb

Overview

Registry of declared task inputs. Each registration creates an Input and defines a reader method on the task class. #resolve walks every input (and nested children) to populate the task’s instance variables before ‘work`.

Defined Under Namespace

Classes: ChildBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInputs

Returns a new instance of Inputs.



11
12
13
# File 'lib/cmdx/inputs.rb', line 11

def initialize
  @registry = {}
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



9
10
11
# File 'lib/cmdx/inputs.rb', line 9

def registry
  @registry
end

Instance Method Details

#deregister(klass, *names) ⇒ Inputs

Removes inputs and their accessor readers from ‘klass`.

Parameters:

  • klass (Class)
  • names (Array<Symbol>)

Returns:

  • (Inputs)

    self for chaining



59
60
61
62
63
64
65
66
# File 'lib/cmdx/inputs.rb', line 59

def deregister(klass, *names)
  names.each do |name|
    input = registry.delete(name.to_sym)
    klass.send(:undefine_input_reader, input)
  end

  self
end

#empty?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/cmdx/inputs.rb', line 69

def empty?
  registry.empty?
end

#initialize_copy(source) ⇒ void

This method returns an undefined value.

Parameters:

  • source (Inputs)

    registry to duplicate



17
18
19
# File 'lib/cmdx/inputs.rb', line 17

def initialize_copy(source)
  @registry = source.registry.dup
end

#register(klass, *names, **options, &block) { ... } ⇒ Inputs

Declares one or more inputs and defines accessor readers on ‘klass`. A block nests child inputs under each declared name (see ChildBuilder).

Parameters:

  • klass (Class)

    the task class to define readers on

  • names (Array<Symbol>)

    input names

  • block (#call, nil)

    nested-input DSL (see ChildBuilder)

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :description (String) — default: also accepts `:desc`
  • :as (Symbol)

    overrides the accessor name

  • :prefix (Boolean, String)

    prefix for the accessor name

  • :suffix (Boolean, String)

    suffix for the accessor name

  • :source (Symbol, Proc, #call) — default: `:context`

    where to fetch from

  • :default (Object, Symbol, Proc, #call)
  • :transform (Symbol, Proc, #call)

    mutator applied after coercion

  • :if (Symbol, Proc, #call)
  • :unless (Symbol, Proc, #call)
  • :required (Boolean)
  • :coerce (Object)

    forwarded with declaration (see Coercions#extract)

  • :validate (Object)

    forwarded with declaration (see Validators#extract)

Yields:

Returns:

  • (Inputs)

    self for chaining



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cmdx/inputs.rb', line 42

def register(klass, *names, **options, &block)
  children = block ? ChildBuilder.build(&block) : EMPTY_ARRAY

  names.each do |name|
    input = Input.new(name, children:, **options)
    registry[input.name] = input
    klass.send(:define_input_reader, input)
  end

  self
end

#resolve(task) ⇒ void

This method returns an undefined value.

Resolves every input (with children) for ‘task`, setting each input’s computed value into its backing ivar so the generated readers return it.

Parameters:



83
84
85
86
87
88
89
# File 'lib/cmdx/inputs.rb', line 83

def resolve(task)
  registry.each_value do |input|
    value = input.resolve(task)
    task.instance_variable_set(input.ivar_name, value)
    resolve_children(input, value, task)
  end
end

#sizeInteger

Returns:

  • (Integer)


74
75
76
# File 'lib/cmdx/inputs.rb', line 74

def size
  registry.size
end