Class: StringBuilder
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/string_builder.rb,
lib/string_builder/version.rb
Constant Summary
collapse
- DEFAULT_PARSER =
-> (buffer) {
buffer.map { |entry|
case entry
when :slash then "/"
when :dash then "-"
else
name, args = entry
args.empty? ? name : "#{name}(#{args.map(&:inspect).join(', ')})"
end
}.join(" ")
}
- VERSION =
"1.0.2"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(&custom_parser) ⇒ StringBuilder
Returns a new instance of StringBuilder.
23
24
25
26
27
|
# File 'lib/string_builder.rb', line 23
def initialize(&custom_parser)
@buffer = []
@parser = custom_parser || DEFAULT_PARSER
@operator_pos = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &_block) ⇒ Object
46
47
48
49
50
|
# File 'lib/string_builder.rb', line 46
def method_missing(name, *args, &_block)
tap do
@buffer << [name.to_s, args]
end
end
|
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
8
9
10
|
# File 'lib/string_builder.rb', line 8
def buffer
@buffer
end
|
#parser ⇒ Object
Returns the value of attribute parser.
9
10
11
|
# File 'lib/string_builder.rb', line 9
def parser
@parser
end
|
Instance Method Details
#-(other) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/string_builder.rb', line 66
def -(other)
tap do
case other
when MethodCallToken
@buffer << [other.base.to_s, []]
insert_pos = @operator_pos || (@buffer.length - 1)
@buffer.insert(insert_pos, :dash)
@buffer << [other.name.to_s, other.args]
@operator_pos = @buffer.length
else
unless other.is_a?(StringBuilder)
@buffer << [other.to_s, []]
end
insert_pos = @operator_pos || (@buffer.length - 1)
@buffer.insert(insert_pos, :dash)
@operator_pos = @buffer.length
end
end
end
|
#/(_other) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/string_builder.rb', line 58
def /(_other)
tap do
insert_pos = @operator_pos || (@buffer.length - 1)
@buffer.insert(insert_pos, :slash)
@operator_pos = @buffer.length
end
end
|
#call(token) ⇒ Object
52
53
54
55
56
|
# File 'lib/string_builder.rb', line 52
def call(token)
tap do
@buffer << [token.to_s, []]
end
end
|
#each {|@buffer| ... } ⇒ Object
38
39
40
|
# File 'lib/string_builder.rb', line 38
def each
yield @buffer
end
|
#respond_to_missing?(_name, _include_private = false) ⇒ Boolean
42
43
44
|
# File 'lib/string_builder.rb', line 42
def respond_to_missing?(_name, _include_private = false)
true
end
|
#to_s ⇒ Object
29
30
31
|
# File 'lib/string_builder.rb', line 29
def to_s
@parser.call(@buffer)
end
|
#wrap(&block) ⇒ Object
33
34
35
36
|
# File 'lib/string_builder.rb', line 33
def wrap(&block)
instance_eval(&block)
self
end
|