Class: StringBuilder
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/string_builder/core.rb,
lib/string_builder/version.rb
Constant Summary
collapse
- DEFAULT_SERIALIZER =
proc { |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.1"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(serializer: DEFAULT_SERIALIZER) ⇒ StringBuilder
Returns a new instance of StringBuilder.
36
37
38
39
40
|
# File 'lib/string_builder/core.rb', line 36
def initialize(serializer: DEFAULT_SERIALIZER)
@buffer = []
@serializer = serializer
@operator_pos = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &_block) ⇒ Object
59
60
61
62
63
|
# File 'lib/string_builder/core.rb', line 59
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.
22
23
24
|
# File 'lib/string_builder/core.rb', line 22
def buffer
@buffer
end
|
Instance Method Details
#-(other) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/string_builder/core.rb', line 79
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
71
72
73
74
75
76
77
|
# File 'lib/string_builder/core.rb', line 71
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
65
66
67
68
69
|
# File 'lib/string_builder/core.rb', line 65
def call(token)
tap do
@buffer << [token.to_s, []]
end
end
|
#each {|@buffer| ... } ⇒ Object
51
52
53
|
# File 'lib/string_builder/core.rb', line 51
def each
yield @buffer
end
|
#respond_to_missing?(_name, _include_private = false) ⇒ Boolean
55
56
57
|
# File 'lib/string_builder/core.rb', line 55
def respond_to_missing?(_name, _include_private = false)
true
end
|
#to_s ⇒ Object
42
43
44
|
# File 'lib/string_builder/core.rb', line 42
def to_s
@serializer.call(@buffer)
end
|
#wrap(&block) ⇒ Object
46
47
48
49
|
# File 'lib/string_builder/core.rb', line 46
def wrap(&block)
instance_eval(&block)
self
end
|