lib/psychic/util.rb in psychic-runner-0.0.3 vs lib/psychic/util.rb in psychic-runner-0.0.4
- old
+ new
@@ -2,12 +2,12 @@
autoload :RegexpTokenHandler, 'psychic/tokens'
autoload :MustacheTokenHandler, 'psychic/tokens'
class Util
module Hashable
def to_hash
- instance_variables.each_with_object({}) do |var,hash|
- hash[var.to_s.delete("@")] = instance_variable_get(var)
+ instance_variables.each_with_object({}) do |var, hash|
+ hash[var.to_s.delete('@')] = instance_variable_get(var)
end
end
end
# Returns a new Hash with all key values coerced to strings. All keys
# within a Hash are coerced by calling #to_s and hashes with arrays
@@ -28,10 +28,24 @@
else
obj
end
end
+ def self.symbolized_hash(obj)
+ if obj.is_a?(Hash)
+ obj.each_with_object({}) do |(k, v), h|
+ h[k.to_sym] = symbolized_hash(v)
+ end
+ elsif obj.is_a?(Array)
+ obj.each_with_object([]) do |e, a|
+ a << symbolized_hash(e)
+ end
+ else
+ obj
+ end
+ end
+
def self.relativize(file, base_path)
absolute_file = File.absolute_path(file)
absolute_base_path = File.absolute_path(base_path)
Pathname.new(absolute_file).relative_path_from Pathname.new(absolute_base_path)
end
@@ -46,12 +60,12 @@
FileFinder.new(search_path, ignored_patterns).find_file(file_alias)
end
def self.replace_tokens(template, variables, token_regexp = nil, token_replacement = nil)
if token_regexp.nil?
- MustacheTokenHandler.new(template).replace(variables)
+ MustacheTokenHandler.new(template).render(variables)
else
- RegexpTokenHandler.new(template, token_regexp, token_replacement).replace(variables)
+ RegexpTokenHandler.new(template, token_regexp, token_replacement).render(variables)
end
end
end
class FileFinder