Module: Tap::Support::Schema::Utils

Included in:
Tap::Support::Schema
Defined in:
lib/tap/support/schema.rb

Class Method Summary collapse

Class Method Details

.format_fork(source_index, target_indicies, options) ⇒ Object

Formats a fork string.

format_fork(1, [2,3],{})            # => "1[2,3]"


61
62
63
# File 'lib/tap/support/schema.rb', line 61

def format_fork(source_index, target_indicies, options)
  "#{source_index}[#{target_indicies.join(',')}]#{format_options(options)}"
end

.format_instance(index) ⇒ Object

Formats a global instance string.

format_instance(1)                  # => "*1"


53
54
55
# File 'lib/tap/support/schema.rb', line 53

def format_instance(index)
  "*#{index}"
end

.format_merge(target_index, source_indicies, options) ⇒ Object

Formats a merge string (note the target index is provided first).

format_merge(1, [2,3],{})           # => "1{2,3}"


70
71
72
# File 'lib/tap/support/schema.rb', line 70

def format_merge(target_index, source_indicies, options)
  "#{target_index}{#{source_indicies.join(',')}}#{format_options(options)}"
end

.format_options(options) ⇒ Object

Formats an options hash into a string. Raises an error for unknown options.

format_options({:iterate => true})  # => "i"


88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tap/support/schema.rb', line 88

def format_options(options)
  options_str = []
  options.each_pair do |key, value|
    unless index = Join::FLAGS.index(key)
      raise "unknown key in: #{options} (#{key})"
    end
    
    if value
      options_str << Join::SHORT_FLAGS[index]
    end
  end
  options_str.sort.join
end

.format_round(round, indicies) ⇒ Object

Formats a round string.

format_round(1, [1,2,3])            # => "+1[1,2,3]"


37
38
39
# File 'lib/tap/support/schema.rb', line 37

def format_round(round, indicies)
  "+#{round}[#{indicies.join(',')}]"
end

.format_sequence(source_index, target_indicies, options) ⇒ Object

Formats a sequence string.

format_sequence(1, [2,3], {})       # => "1:2:3"


45
46
47
# File 'lib/tap/support/schema.rb', line 45

def format_sequence(source_index, target_indicies, options)
  ([source_index] + target_indicies).join(":") + format_options(options)
end

.format_sync_merge(target_index, source_indicies, options) ⇒ Object

Formats a sync_merge string (note the target index is provided first).

format_sync_merge(1, [2,3],{})      # => "1(2,3)"


79
80
81
# File 'lib/tap/support/schema.rb', line 79

def format_sync_merge(target_index, source_indicies, options)
  "#{target_index}(#{source_indicies.join(',')})#{format_options(options)}"
end

.shell_quote(str) ⇒ Object

Shell quotes the input string by enclosing in quotes if str has no quotes, or double quotes if str has no double quotes. Returns the str if it has not whitespace, quotes or double quotes.

Raises an ArgumentError if str has both quotes and double quotes.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tap/support/schema.rb', line 20

def shell_quote(str)
  return str unless str =~ /[\s'"]/

  quote = str.include?("'")
  double_quote = str.include?('"')

  case
  when !quote then "'#{str}'"
  when !double_quote then "\"#{str}\""
  else raise ArgumentError, "cannot shell quote: #{str}"
  end
end