Class: Audiothority::Enforcer

Inherits:
Object
  • Object
show all
Defined in:
lib/audiothority/enforcer.rb

Instance Method Summary collapse

Constructor Details

#initialize(suspects, console, options = {}) ⇒ Enforcer

Returns a new instance of Enforcer.



5
6
7
8
9
10
# File 'lib/audiothority/enforcer.rb', line 5

def initialize(suspects, console, options={})
  @suspects = suspects
  @console = console
  @extract = options[:extract] || Extract.new
  @society = options[:society] || EmptySociety.new
end

Instance Method Details

#enforceObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/audiothority/enforcer.rb', line 12

def enforce
  @suspects.each do |path, violations|
    violations = violations.select(&:applicable?)
    @extract.as_tags(path.children, save: true) do |tags|
      changes = violations.map do |violation|
        field = violation.field
        values = fields_from(tags, field)
        choices = choices_from(values)
        Change.new(field, choices, tags)
      end
      changes << RewriteChange.new(:track, tags)
      changes << RewriteChange.new(:year, tags)
      @console.say(%(changes for #{path}:))
      changes.each do |change|
        change.present(@console)
      end
      if perform_changes?
        changes.each(&:perform)
        @society.transfer(path)
        @console.say
      end
    end
  end
end