Class: Configature::Stamper

Inherits:
Object
  • Object
show all
Defined in:
lib/configature/stamper.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Stamper

Returns a new instance of Stamper.



2
3
4
# File 'lib/configature/stamper.rb', line 2

def initialize(dir)
  @dir = dir
end

Instance Method Details

#apply!(force: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/configature/stamper.rb', line 30

def apply!(force: false)
  self.examples.each do |source, target|
    if (!force and File.exist?(target))
      yield(
        source,
        target,
        created: false,
        existing: true,
        config_required: !!File.read(target).match(/__[A-Z\-\_]+__/)
      ) if (block_given?)
    else
      FileUtils.copy(source, target)
  
      yield(
        source,
        target,
        created: true,
        existing: false,
        config_required: !!File.read(target).match(/__[A-Z\-\_]+__/)
      ) if (block_given?)
    end
  end
end

#clean!Object



20
21
22
23
24
25
26
27
28
# File 'lib/configature/stamper.rb', line 20

def clean!
  self.examples.each do |_source, target|
    if (File.exist?(target))
      File.unlink(target)

      yield(target) if (block_given?)
    end
  end
end

#examplesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/configature/stamper.rb', line 6

def examples
  map = Dir.glob(File.expand_path('*.example', @dir)).map do |source|
    [ source, source.delete_suffix('.example') ]
  end.to_h

  if (block_given?)
    map.each do |k,v|
      yield(k, v)
    end
  end

  map
end