Class: Argstring::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/argstring/config.rb

Constant Summary collapse

DEFAULT_ENCLOSERS =
[{ open: '"', close: '"' }, { open: "'", close: "'" }].freeze
WHITESPACE_SEPARATOR_SENTINEL =
/\s/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(separator: WHITESPACE_SEPARATOR_SENTINEL, escape: "\\", assignment: ["="], enclosers: DEFAULT_ENCLOSERS, duplicates: :last, flags: true, positional: true) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/argstring/config.rb', line 10

def initialize(separator: WHITESPACE_SEPARATOR_SENTINEL, escape: "\\", assignment: ["="], enclosers: DEFAULT_ENCLOSERS, duplicates: :last, flags: true, positional: true)
	@separator = separator
	@escape = escape
	@assignment = assignment
	@enclosers = enclosers
	@duplicates = duplicates
	@flags_enabled = flags
	@positional_enabled = positional

	validate!
end

Instance Attribute Details

#assignmentObject (readonly)

Returns the value of attribute assignment.



8
9
10
# File 'lib/argstring/config.rb', line 8

def assignment
  @assignment
end

#duplicatesObject (readonly)

Returns the value of attribute duplicates.



8
9
10
# File 'lib/argstring/config.rb', line 8

def duplicates
  @duplicates
end

#enclosersObject (readonly)

Returns the value of attribute enclosers.



8
9
10
# File 'lib/argstring/config.rb', line 8

def enclosers
  @enclosers
end

#escapeObject (readonly)

Returns the value of attribute escape.



8
9
10
# File 'lib/argstring/config.rb', line 8

def escape
  @escape
end

#flags_enabledObject (readonly)

Returns the value of attribute flags_enabled.



8
9
10
# File 'lib/argstring/config.rb', line 8

def flags_enabled
  @flags_enabled
end

#positional_enabledObject (readonly)

Returns the value of attribute positional_enabled.



8
9
10
# File 'lib/argstring/config.rb', line 8

def positional_enabled
  @positional_enabled
end

#separatorObject (readonly)

Returns the value of attribute separator.



8
9
10
# File 'lib/argstring/config.rb', line 8

def separator
  @separator
end

Instance Method Details

#encloser_open_pair_for(ch) ⇒ Object



34
35
36
# File 'lib/argstring/config.rb', line 34

def encloser_open_pair_for(ch)
	@enclosers.find { |pair| pair[:open] == ch }
end

#escape_enabled?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/argstring/config.rb', line 26

def escape_enabled?
	@escape && @escape != ""
end

#separator_boundary_at?(string, index) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/argstring/config.rb', line 38

def separator_boundary_at?(string, index)
	ch = string[index]
	return whitespace_char?(ch) if whitespace_separator?
	ch == @separator
end

#whitespace_char?(ch) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/argstring/config.rb', line 30

def whitespace_char?(ch)
	!!(ch =~ /\s/)
end

#whitespace_separator?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/argstring/config.rb', line 22

def whitespace_separator?
	@separator.is_a?(Regexp) && @separator == WHITESPACE_SEPARATOR_SENTINEL
end