Class: Macro

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/ruby_ext/macro.rb

Overview

string wrapped in parameter expression macro symbol and delimiters, indicating content is to be parsed and resolved when building and validating XML design

Constant Summary collapse

MACRO_SYMBOL =

is ‘@’ by default

'@'
DELIMITERS =

are parentheses by default e.g. ‘()’

%w{( )}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Macro

takes given string and wraps in MACRO_SYMBOL and DELIMITERS if not already wrapped e.g. str => ‘asdf’

Macro.new str => '@(asdf)'


22
23
24
# File 'lib/ruby_ext/macro.rb', line 22

def initialize(str)
  @macro_string = is_macro?(str) ? str : "#{MACRO_SYMBOL}#{DELIMITERS.first}#{str}#{DELIMITERS.last}"
end

Instance Attribute Details

#macro_stringObject

string including MACRO_SYMBOL and DELIMITERS



17
18
19
# File 'lib/ruby_ext/macro.rb', line 17

def macro_string
  @macro_string
end

Instance Method Details

#<=>(obj) ⇒ Object

compares #demacro’d @macro_string to obj



32
33
34
# File 'lib/ruby_ext/macro.rb', line 32

def <=>(obj)
  demacro <=> obj
end

#demacroObject

returns string without MACRO_SYMBOL and DELIMITERS



42
43
44
# File 'lib/ruby_ext/macro.rb', line 42

def demacro
  macro_string[2..-2]
end

#each(&block) ⇒ Object

just yields each character of #demacro’d @macro_string



37
38
39
# File 'lib/ruby_ext/macro.rb', line 37

def each(&block)
  demacro.split(//).each do |char| yield char end
end

#is_macro?(str) ⇒ Boolean

checks a string to see if it’s a valid macro expression without leading or trailing non-expression or delimiter text

Returns:

  • (Boolean)


27
28
29
# File 'lib/ruby_ext/macro.rb', line 27

def is_macro?(str)
  str[0,2] == MACRO_SYMBOL+DELIMITERS.first && str[-1] == DELIMITERS.last && str.balanced_parens?
end

#parameterized?Boolean

returns nil if not, and match data for any parameter names found

Returns:

  • (Boolean)


47
48
49
# File 'lib/ruby_ext/macro.rb', line 47

def parameterized?
  macro_string.match Regexp.identifier
end