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

Class Method 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)'


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

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

Instance Attribute Details

#macro_stringObject

string including MACRO_SYMBOL and DELIMITERS



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

def macro_string
  @macro_string
end

Class Method Details

.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)


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

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

Instance Method Details

#<=>(obj) ⇒ Object

compares #demacro’d @macro_string to obj



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

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

#demacroObject

returns string without MACRO_SYMBOL and DELIMITERS



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

def demacro
  macro_string[2..-2]
end

#each(&block) ⇒ Object

just yields each character of #demacro’d @macro_string



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

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

#parameterized?Boolean

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

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/ruby_ext/macro.rb', line 52

def parameterized?
  return false if macro_string[2] == '"' && macro_string[-2] == '"'
  macro_string.match Regexp.identifier
end

#to_sObject



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

def to_s
  macro_string
end