Class: Patter::Pattern

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

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

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
50
# File 'lib/patter/pattern.rb', line 24

def to_s
    @pattern.gsub(/\{([#{TAGS.keys.join}])(:(\w+))?\}/) do
        tag = $1
        modifiers = $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