/
macrow.rb
78 lines (60 loc) · 1.56 KB
/
macrow.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'macrow/version'
require 'macrow/error'
class Macrow
class << self
def inherited(child)
child.instance_variable_set(:@rule_words, rule_words.dup)
end
def rule_words
@rule_words ||= []
end
def rules
yield
end
def macro_prefix(prefix)
@macro_prefix = prefix
end
def macro_suffix(suffix)
@macro_suffix = suffix
end
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
def replace_string(keyword)
"#{_macro_prefix}#{keyword}#{_macro_suffix}"
end
private
def _macro_prefix
@macro_prefix ||= '${'
end
def _macro_suffix
@macro_suffix ||= '}'
end
end
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
alias_method :apply, :apply_all_rules
def apply_rule!(rule, str, object)
__send__("apply_rule_for_#{rule}!", str, object)
end
private
def replace_string(keyword)
self.class.replace_string(keyword)
end
end