lib/dolos/result.rb in dolos-0.1.3 vs lib/dolos/result.rb in dolos-0.2.0
- old
+ new
@@ -8,24 +8,25 @@
attr_reader :value, :length, :captures
def initialize(value, length, captures = [])
@value = value
@length = length
- # @captures = captures || value
@captures = captures
end
- def capture!
- if value.is_a?(Array)
- value.each do |v|
- captures << v
- end
+ # can be some named capture, :street, {:street => capture }
+ # or an array, [], [capture]
+ def capture!(wrap_in = nil)
+ mapped_value = self.value # use the transformed value here
+
+ if wrap_in.is_a?(Array)
+ save_capture([mapped_value])
+ elsif wrap_in.is_a?(Symbol)
+ save_capture({ wrap_in => mapped_value })
else
- captures << value
+ save_capture(mapped_value)
end
-
- Success.new(value, length, captures)
end
def inspect
"Success(value: '#{value}',length: #{length}, capture: '#{captures}')"
end
@@ -34,9 +35,23 @@
true
end
def failure?
false
+ end
+
+ private
+
+ def save_capture(val)
+ if val.is_a?(Array)
+ val.each do |v|
+ captures << v
+ end
+ else
+ captures << val
+ end
+
+ Success.new(val, length, captures)
end
end
class Failure < Result
attr_reader :message, :error_position, :state