Module: Pindo::Options::OptionGroup

Included in:
BuildOptions, GitOptions, JPSOptions, UnityOptions
Defined in:
lib/pindo/options/groups/option_group.rb

Overview

参数组工具模块提供参数组的定义、筛选和合并功能合并了原 OptionGroupBase 和 OptionRegistry 的功能

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.merge(*option_groups) ⇒ Array<OptionItem>

合并多个参数组

Parameters:

  • option_groups (Array<Array<OptionItem>>)

    参数组列表

Returns:

  • (Array<OptionItem>)

    合并后的参数列表(去重)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pindo/options/groups/option_group.rb', line 72

def self.merge(*option_groups)
  merged_map = {}

  option_groups.flatten.compact.each do |item|
    unless item.is_a?(OptionItem)
      raise ArgumentError, "Expected OptionItem, got #{item.class}"
    end

    # 如果键已存在,后面的覆盖前面的
    merged_map[item.key] = item
  end

  merged_map.values
end

Instance Method Details

#allArray<OptionItem>

获取所有参数(返回数组)

Returns:



57
58
59
# File 'lib/pindo/options/groups/option_group.rb', line 57

def all
  all_options.values
end

#all_optionsHash{Symbol => OptionItem}

子类必须实现此方法

Returns:

  • (Hash{Symbol => OptionItem})

    所有参数的 Hash

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/pindo/options/groups/option_group.rb', line 63

def all_options
  raise NotImplementedError, "子类必须实现 all_options 方法"
end

#except(*keys) ⇒ Array<OptionItem>

排除指定的参数,返回剩余参数

Parameters:

  • keys (Array<Symbol>)

    要排除的参数键名

Returns:



33
34
35
36
37
38
# File 'lib/pindo/options/groups/option_group.rb', line 33

def except(*keys)
  keys = keys.flatten.map(&:to_sym)
  all_options_hash = all_options

  all_options_hash.reject { |k, v| keys.include?(k) }.values
end

#select(*keys) ⇒ Array<OptionItem>

选择指定的参数

Parameters:

  • keys (Array<Symbol>)

    要选择的参数键名

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pindo/options/groups/option_group.rb', line 15

def select(*keys)
  keys = keys.flatten.map(&:to_sym)
  all_options_hash = all_options

  selected = keys.map do |key|
    option = all_options_hash[key]
    if option.nil?
      raise ArgumentError, "参数组 #{self.name} 中不存在参数: #{key}"
    end
    option
  end

  selected
end

#select_with_defaults(**defaults) ⇒ Array<OptionItem>

选择指定的参数并覆盖默认值

Examples:

UnityOptions.select_with_defaults(skipconfig: true, skiplib: true)

Parameters:

  • defaults (Hash{Symbol => Any})

    key => default_value 的映射

Returns:

  • (Array<OptionItem>)

    修改后的参数列表



45
46
47
48
49
50
51
52
53
# File 'lib/pindo/options/groups/option_group.rb', line 45

def select_with_defaults(**defaults)
  defaults.map do |key, default_value|
    option = all_options[key]
    if option.nil?
      raise ArgumentError, "参数组 #{self.name} 中不存在参数: #{key}"
    end
    option.with(default_value: default_value)
  end
end