Class: Coco::Options::Group

Inherits:
Object
  • Object
show all
Includes:
Item
Defined in:
lib/coco/options/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Item

#alias, #html_alias, #private?, #public?

Constructor Details

#initialize(name, shorthand: nil, **kwargs) ⇒ Group

Returns a new instance of Group.



9
10
11
12
13
14
15
# File 'lib/coco/options/group.rb', line 9

def initialize(name, shorthand: nil, **kwargs)
  @name = name.to_sym
  @shorthand = shorthand&.to_sym
  @alias = kwargs.fetch(:alias, nil)
  @private = kwargs.fetch(:private, false)
  @items = []
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/coco/options/group.rb', line 7

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/coco/options/group.rb', line 7

def name
  @name
end

#shorthandObject

Returns the value of attribute shorthand.



6
7
8
# File 'lib/coco/options/group.rb', line 6

def shorthand
  @shorthand
end

Instance Method Details

#accepts_optionObject



17
18
19
20
21
22
23
24
25
# File 'lib/coco/options/group.rb', line 17

def accepts_option(...)
  if block_given?
    subgroup = Group.new(...)
    yield subgroup
    add_group(subgroup)
  else
    add_option(...)
  end
end

#add_groupObject



31
32
33
# File 'lib/coco/options/group.rb', line 31

def add_group(...)
  items << Group.new(...)
end

#add_optionObject



27
28
29
# File 'lib/coco/options/group.rb', line 27

def add_option(...)
  items << Option.new(...)
end

#cloneObject



133
134
135
# File 'lib/coco/options/group.rb', line 133

def clone
  Marshal.load(Marshal.dump(self))
end

#flattened_attribute_values(key_prefix = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/coco/options/group.rb', line 122

def flattened_attribute_values(key_prefix = nil)
  public_items.each_with_object([]) do |item, values|
    key = [key_prefix, item.html_alias].compact.join("-")
    if item.is_a?(Option)
      item.value.nil? ? values : values.push([key, item.value])
    else
      values.push(*item.flattened_attribute_values(key).to_a)
    end
  end.to_h
end

#get_group(*args) ⇒ Object

Like #dig… but for groups.



87
88
89
90
91
92
93
94
# File 'lib/coco/options/group.rb', line 87

def get_group(*args)
  current = args.any? ? nil : self
  args.each do |group_name|
    next_group = (current || self).groups.find { _1.name == group_name.to_sym }
    current = next_group || break
  end
  current
end

#get_group!(*args) ⇒ Object



96
97
98
# File 'lib/coco/options/group.rb', line 96

def get_group!(*args)
  get_group(*args) || raise("Unknown group: `#{args.join(", ")}`")
end

#get_option(*args) ⇒ Object

Like #dig… but for options.

get_option(:size) get_option(:dropdown, :placement)



39
40
41
42
43
# File 'lib/coco/options/group.rb', line 39

def get_option(*args)
  option_name = args.pop.to_sym
  group = get_group(*args)
  group.items.find { _1.name == option_name && _1.is_a?(Option) }
end

#get_option!(*args) ⇒ Object



45
46
47
# File 'lib/coco/options/group.rb', line 45

def get_option!(*args)
  get_option(*args) || raise("Unknown option: `#{args.join(", ")}`")
end

#get_option_valueObject



53
54
55
# File 'lib/coco/options/group.rb', line 53

def get_option_value(...)
  get_option!(...).value
end

#group?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/coco/options/group.rb', line 100

def group?(...)
  !!get_group(...)
end

#merge_option_values(values, overwrite: true) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/coco/options/group.rb', line 72

def merge_option_values(*, values, overwrite: true)
  get_group!(*).items.each do |item|
    if values.key?(item.name)
      value = values[item.name]
      if item.is_a?(Option)
        item.value = value if overwrite || item.undefined?
      elsif value.is_a?(Hash) || item.shorthand
        value = value.is_a?(Hash) ? value : [[item.shorthand, value]].to_h
        item.merge_option_values(value, overwrite: overwrite)
      end
    end
  end
end

#option?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/coco/options/group.rb', line 49

def option?(...)
  !!get_option(...)
end

#option_namesObject



108
109
110
# File 'lib/coco/options/group.rb', line 108

def option_names
  items.map(&:name)
end

#option_value_equals?(check) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/coco/options/group.rb', line 68

def option_value_equals?(*, check)
  get_option_value(*) == check
end

#public_itemsObject



118
119
120
# File 'lib/coco/options/group.rb', line 118

def public_items
  items.filter(&:public?)
end

#set_option_value(value) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/coco/options/group.rb', line 57

def set_option_value(*, value)
  if option?(*)
    get_option(*).value = value
  elsif group?(*)
    group = get_group(*)
    if group.shorthand
      group.set_option_value(group.shorthand, value)
    end
  end
end

#validate_required!Object



104
105
106
# File 'lib/coco/options/group.rb', line 104

def validate_required!
  items.map(&:validate_required!)
end

#valuesObject



112
113
114
115
116
# File 'lib/coco/options/group.rb', line 112

def values
  items.map do |item|
    [item.name, item.is_a?(Option) ? item.value : item.values]
  end.to_h
end