Class: Patter::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/patter/pattern.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, source_provider = SourceProvider.instance) ⇒ Pattern

Returns a new instance of Pattern.



19
20
21
22
# File 'lib/patter/pattern.rb', line 19

def initialize(pattern, source_provider = SourceProvider.instance)
    @pattern = pattern
    @source_provider = source_provider
end

Class Method Details

.helpObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/patter/pattern.rb', line 51

def self.help
    return "        TAGS\n\n            {A} adjective       {N} noun\n            {S} symbol          {D} digit\n            {C} character\n\n        MODIFIERS\n\n            s: plural           t: titlecase\n            u: uppercase        l: lowercase\n            a: AlTeRnAtE case\n            <number>: repeat <number> times\n\n        Apply modifiers using a colon after the tag name.\n\n        EXAMPLES\n\n            Ten random uppercase letters: {C:10u}\n            Three camelcase nouns:        {N:3t}\n            Plural, titlecase noun:       {N:ts}\n            Uppercase adjective:          {A:u}\n            Five random digits:           {D:5}\n            A username:                   {A}_{N}\n    EOF\nend\n"

Instance Method Details

#to_sObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/patter/pattern.rb', line 24

def to_s
    @pattern.gsub(/\{([#{TAGS.keys.join}])(:(\w+))?\}/) do
        tag, modifiers = $1, $3
        source = @source_provider.get_source(TAGS[tag])

        next source.get_sample if !modifiers

        re = /([#{MODIFIERS.keys.join}])|([0-9]+)/
        chain = []
        n = 1

        modifiers.split(re).reject(&:empty?).each do |modifier|
            if modifier =~ /([0-9]+)/
                n = $1.to_i
            end

            if MODIFIERS[modifier]
                chain << MODIFIERS[modifier]
            end
        end

        source.get_samples(n).map do |sample|
            sample.transform(chain)
        end.join
    end
end