Method: Musa::GenerativeGrammar::Implementation::Node#options

Defined in:
lib/musa-dsl/generative/generative-grammar.rb

#options(attribute = nil, after_collect_operation = nil, comparison_method = nil, comparison_value = nil, raw: nil, content: nil) {|option| ... } ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates all options from this grammar node.

Produces all valid combinations matching the grammar definition. Can filter with condition and control output format.

Examples:

Basic usage

grammar = (a | b).repeat(2)
grammar.options
# => [["a", "a"], ["a", "b"], ["b", "a"], ["b", "b"]]

With content formatting

grammar.options(content: :join)
# => ["aa", "ab", "ba", "bb"]

With filtering

grammar.options { |o| o.size == 2 }

Simplified filtering

grammar.options(:size, :sum, :<=, 4, content: :join)

Raw OptionElements

grammar.options(raw: true)

Parameters:

  • attribute (Symbol, nil) (defaults to: nil)

    attribute for filtering (simplified)

  • after_collect_operation (Symbol, nil) (defaults to: nil)

    operation on collected values

  • comparison_method (Symbol, nil) (defaults to: nil)

    comparison method

  • comparison_value (Object, nil) (defaults to: nil)

    value to compare against

  • raw (Boolean) (defaults to: nil)

    if true, return raw OptionElement arrays

  • content (Symbol) (defaults to: nil)

    how to extract content (:itself, :join, etc.)

Yield Parameters:

Yield Returns:

  • (Boolean)

    true if option should be included

Returns:

  • (Array)

    generated options

Raises:

  • (ArgumentError)

    if simplified arguments and block both given

  • (ArgumentError)

    if raw and content both specified



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 333

def options(attribute = nil,
            after_collect_operation = nil,
            comparison_method = nil,
            comparison_value = nil,
            raw: nil,
            content: nil,
            &condition)

  raise ArgumentError, 'Cannot use simplified arguments and yield block at the same time' if (attribute || after_collect_operation || comparison_method || comparison_value) && @condition
  raise ArgumentError, 'Cannot use raw: true and content: option at the same time' if raw && content

  raw ||= false
  content ||= :itself

  condition ||= generate_simple_condition_block(attribute, after_collect_operation, comparison_method, comparison_value)

  if raw
    _options(&condition)
  else
    _options(&condition).collect { |o| o.collect(&:content).send(content) }
  end
end