class Macrow

Constants

VERSION

Public Class Methods

inherited(child) click to toggle source
# File lib/macrow.rb, line 6
def inherited(child)
  child.instance_variable_set(:@rule_words, rule_words.dup)
end
macro_prefix(prefix) click to toggle source
# File lib/macrow.rb, line 18
def macro_prefix(prefix)
  @macro_prefix = prefix
end
macro_suffix(suffix) click to toggle source
# File lib/macrow.rb, line 22
def macro_suffix(suffix)
  @macro_suffix = suffix
end
replace_string(keyword) click to toggle source
# File lib/macrow.rb, line 41
def replace_string(keyword)
  "#{_macro_prefix}#{keyword}#{_macro_suffix}"
end
rule(keyword, &block) click to toggle source
# File lib/macrow.rb, line 26
def rule(keyword, &block)
  rule_words << keyword

  define_method "apply_rule_for_#{keyword}!" do |str, object|
    if str.include? replace_string(keyword)
      begin
        str.gsub!(replace_string(keyword), block.call(object).to_s)
        str
      rescue NoMethodError => e
        fail Macrow::ReplaceError, "NoMethodError is raised on applying rule: '#{keyword}'. Please check your rule. Error message is '#{e.message}'"
      end
    end
  end
end
rule_words() click to toggle source
# File lib/macrow.rb, line 10
def rule_words
  @rule_words ||= []
end
rules() { || ... } click to toggle source
# File lib/macrow.rb, line 14
def rules
  yield
end

Private Class Methods

_macro_prefix() click to toggle source
# File lib/macrow.rb, line 47
def _macro_prefix
  @macro_prefix ||= '${'
end
_macro_suffix() click to toggle source
# File lib/macrow.rb, line 51
def _macro_suffix
  @macro_suffix ||= '}'
end

Public Instance Methods

apply(str, object = nil)
Alias for: apply_all_rules
apply_all_rules(str, object = nil) click to toggle source
# File lib/macrow.rb, line 56
def apply_all_rules(str, object = nil)
  return if str.nil? || !str.is_a?(String)

  dup_str = str.dup

  self.class.rule_words.each do |rule_word|
    apply_rule!(rule_word, dup_str, object)
  end

  dup_str
end
Also aliased as: apply
apply_rule!(rule, str, object) click to toggle source
# File lib/macrow.rb, line 69
def apply_rule!(rule, str, object)
  __send__("apply_rule_for_#{rule}!", str, object)
end

Private Instance Methods

replace_string(keyword) click to toggle source
# File lib/macrow.rb, line 75
def replace_string(keyword)
  self.class.replace_string(keyword)
end