app/cyclid/monkey_patches.rb in cyclid-0.2.5 vs app/cyclid/monkey_patches.rb in cyclid-0.3.0
- old
+ new
@@ -7,31 +7,58 @@
hash.merge(yield(k, v))
end
end
# Interpolate the data in the ctx hash into any String values
- def interpolate(ctx)
+ def %(other)
hmap do |key, value|
if value.is_a? String
- { key => value % ctx }
+ { key => value ** other }
else
{ key => value }
end
end
end
end
# Add a method to Array
class Array
# Interpolate the data in the ctx hash for each String & Hash item
- def interpolate(ctx)
+ def %(other)
map do |entry|
if entry.is_a? Hash
- entry.interpolate ctx
+ entry % other
elsif entry.is_a? String
- entry % @ctx
+ entry ** other
else
entry
end
end
+ end
+end
+
+# Add a method to String
+class String
+ # Provide a "safe" version of the % (interpolation) operator; if a key does
+ # not exist in arg, catch the KeyError and insert it as a nil value, and
+ # continue.
+ #
+ # We're using the ** operator as it's one that isn't already used by String,
+ # and it's abusing % already so hey, why not. FTP
+ def **(other)
+ res = nil
+ arg = other ? other.dup : {}
+
+ begin
+ res = self % arg
+ rescue KeyError => ex
+ # Extract the key name from the exception message (sigh)
+ match = ex.message.match(/\Akey{(.*)} not found\Z/)
+ key = match[1]
+
+ # Inject key with a default value and try again
+ arg[key.to_sym] = nil
+ end while res.nil? # rubocop:disable Lint/Loop
+
+ return res
end
end