Class: ToPass::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/to_pass/converter.rb

Overview

converts a given string into a password-like word

the string can be a word or a sentence. everthing which contains whitespace is considered a sentence

a more complete description of the algorithm capabilities is still pending.

see ToPass::ConverterReader and ToPass::AlgorithmReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ Converter

create a new converter, based on a set of conversion-rules



16
17
18
19
20
# File 'lib/to_pass/converter.rb', line 16

def initialize( rules )
  @rules = rules
  @reader = ConverterReader.new
  @converters = @reader.discover
end

Instance Attribute Details

#convertersObject

Returns the value of attribute converters.



14
15
16
# File 'lib/to_pass/converter.rb', line 14

def converters
  @converters
end

#rulesObject

Returns the value of attribute rules.



14
15
16
# File 'lib/to_pass/converter.rb', line 14

def rules
  @rules
end

Instance Method Details

#apply_rule(pwd, rule) ⇒ Object (private)

apply a single rule to the password-to-be



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/to_pass/converter.rb', line 71

def apply_rule(pwd, rule)
  cmd, args = if rule.respond_to?(:to_a)
                rule.to_a.flatten
              else
                [ rule, nil ]
              end
  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

#convert(string) ⇒ Object

convert a string into a password



23
24
25
# File 'lib/to_pass/converter.rb', line 23

def convert( string )
  process(string, rules_for(string))
end

#process(string, rules) ⇒ Object (private)

process the string, rule by rule



64
65
66
67
68
# File 'lib/to_pass/converter.rb', line 64

def process(string, rules)
  rules.inject(string) do |pwd, rule|
    apply_rule(pwd, rule)
  end
end

#respond_to?(method_name) ⇒ Boolean

proxy to converters

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
# File 'lib/to_pass/converter.rb', line 39

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

#rules_for(string) ⇒ Object (private)

return the applicable rules for a given string.

everything which contains whitespace is considered a sentence, otherwise it is most likely a word.



55
56
57
58
59
60
61
# File 'lib/to_pass/converter.rb', line 55

def rules_for( string )
  if string.include? ' ' or /\s/.match(string)
    @rules['sentence']
  else
    @rules['word']
  end
end