Module: HeadMusic::Style::Guideline::Wording

Included in:
HeadMusic::Style::Guideline
Defined in:
lib/head_music/style/guideline/wording.rb

Overview

How a guideline says what it wants: the locale keys it addresses and the sentences rendered from them. Extended rather than included, because the wording belongs to the rule – the class – and not to one analysis.

Instance Method Summary collapse

Instance Method Details

#instruction_keyObject



16
# File 'lib/head_music/style/guideline/wording.rb', line 16

def instruction_key = "guidelines.#{template_key}.instruction"

#name_keyObject



14
# File 'lib/head_music/style/guideline/wording.rb', line 14

def name_key = "guidelines.#{template_key}.name"

#render_instruction(config) ⇒ Object

Violations are already phrased imperatively, so a guideline with nothing more specific to say instructs with the sentence it would complain with.



39
40
41
42
43
# File 'lib/head_music/style/guideline/wording.rb', line 39

def render_instruction(config)
  return render_violation(config) unless HeadMusic::Style::Template.exists?(instruction_key)

  render_template(instruction_key, config)
end

#render_name(config) ⇒ Object

Where a locale has nothing to say, the class name read as a sentence beats nothing.

The first letter is upcased here rather than in the data because a name can lead with an interpolation – “%contour contour” – whose value has to stay lowercase for the violation sentence that embeds it mid-sentence.



27
28
29
30
31
32
33
34
35
# File 'lib/head_music/style/guideline/wording.rb', line 27

def render_name(config)
  rendered =
    if HeadMusic::Style::Template.exists?(name_key)
      render_template(name_key, config)
    else
      template_key.tr("_", " ")
    end
  rendered.sub(/\A./) { |letter| letter.upcase }
end

#render_template(key, config, extra_values = {}) ⇒ Object

Takes the configuration rather than finished values, so template_values – where numbers humanize – runs inside the locale the sentence renders in.



62
63
64
65
66
67
68
69
70
71
# File 'lib/head_music/style/guideline/wording.rb', line 62

def render_template(key, config, extra_values = {})
  HeadMusic::Style::Template.in_locale_of(key) do
    values = template_values(config).merge(extra_values)
    if values.key?(:count)
      HeadMusic::Style::Template.pluralize(key, **values)
    else
      HeadMusic::Style::Template.render(key, **values)
    end
  end
end

#render_violation(config) ⇒ Object



45
46
47
# File 'lib/head_music/style/guideline/wording.rb', line 45

def render_violation(config)
  render_template(violation_key(config), config)
end

#render_violations(config) ⇒ Object



56
57
58
# File 'lib/head_music/style/guideline/wording.rb', line 56

def render_violations(config)
  violation_keys(config).map { |key| render_template(key, config) }
end

#template_keyObject

Addressed in the locale files by the snake_case of the class name, so a new guideline needs no declaration.



10
11
12
# File 'lib/head_music/style/guideline/wording.rb', line 10

def template_key
  @template_key ||= HeadMusic::Utilities::Case.to_snake_case(name.split("::").last)
end

#template_values(config) ⇒ Object

Not the config itself: seven items declare none and read their values from class constants, and I18n returns a template untouched when given no values.



75
76
77
# File 'lib/head_music/style/guideline/wording.rb', line 75

def template_values(config)
  config.except(:violation_key)
end

#violation_key(_config = {}) ⇒ Object

Takes the configuration: a guide may name a different template.



19
# File 'lib/head_music/style/guideline/wording.rb', line 19

def violation_key(_config = {}) = "guidelines.#{template_key}.violations.default"

#violation_keys(config = {}) ⇒ Object

Every key an analysis can choose between, not only the one a bare config names. Load-time verification renders all of them: a branch reached by one kind of failure is otherwise unrendered until a student causes it.



52
53
54
# File 'lib/head_music/style/guideline/wording.rb', line 52

def violation_keys(config = {})
  [violation_key(config)]
end