lib/to_pass/converter.rb in to_pass-0.2.4 vs lib/to_pass/converter.rb in to_pass-0.4.0
- old
+ new
@@ -8,13 +8,16 @@
# contains whitespace is considered a sentence
#
# a more complete description of the algorithm capabilities
# is still pending.
class Converter
+ attr_accessor :converters, :rules
# create a new converter, based on a set of conversion-rules
def initialize( rules )
@rules = rules
+ @reader = ConverterReader.new
+ @converters = @reader.discover
end
# convert a string into a password
def convert( string )
process(string, rules_for(string))
@@ -29,14 +32,21 @@
# def convert(string, rules)
# new(rules).convert(string)
# end
# end
- protected
+ # proxy to converters
+ def respond_to?(method_name) # :nodoc:
+ if [:convert, :rules_for, :process].include? method_name.to_sym
+ true
+ elsif @converters.include? method_name.to_sym
+ true
+ else
+ super
+ end
+ end
- include StringConversions
-
private
# return the applicable rules for a given string.
#
# everything which contains whitespace is considered a sentence,
@@ -50,16 +60,23 @@
end
# process the string, rule by rule
def process(string, rules)
rules.inject(string) do |pwd, rule|
- if rule.is_a?(String) and respond_to?(rule.to_sym)
- send(rule.to_sym, pwd)
- elsif rule.is_a?(Hash) and rule.has_key?('table')
- replace(pwd, @rules['tables'][rule.fetch('table')])
- else
- pwd
- end
+ apply_rule(pwd, rule)
+ end
+ end
+
+ # apply a single rule to the password-to-be
+ def apply_rule(pwd, rule)
+ cmd, args = rule.to_a.flatten
+ m = @reader.load(cmd.to_sym).method(cmd.to_sym)
+
+ case m.arity
+ when 1
+ m.call(pwd)
+ when 3, -2
+ m.call(pwd, @rules, *args)
end
end
end
end