Class: Anodator::Message

Inherits:
String
  • Object
show all
Defined in:
lib/anodator/message.rb

Overview

Message is error message builder.

Message is generated with formated string for expand several keywords. Message#expand method is expand keywords by data_provider suppried by parameter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_message) ⇒ Message

Returns a new instance of Message.



14
15
16
17
18
19
20
# File 'lib/anodator/message.rb', line 14

def initialize(template_message)
  @template = template_message.to_s

  if @template.split(//).size.zero?
    raise ArgumentError.new("template_message cannot be blank")
  end
end

Instance Attribute Details

#templateObject (readonly)

message



12
13
14
# File 'lib/anodator/message.rb', line 12

def template
  @template
end

Instance Method Details

#expand(data_provider) ⇒ Object

expand message with data_provider

message keyword is expressed [[target_expression::attribute]]. data_provider is data source for current check target.

  • target_expression: specify target.

  • attribute: actual printed value

    • name: target name

    • number: target number

    • value: target actual data



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/anodator/message.rb', line 32

def expand(data_provider)
  @template.gsub(/\[\[([^:]+)::([^\]]+)\]\]/) do
    spec_item = data_provider.spec_item_by_expression($1)
    case $2
    when "name"
      spec_item.name
    when "number"
      spec_item.number
    when "value"
      data_provider[$1]
    else
      raise UnknownMessageAttributeError.new("Unknown message attribute '#{$2}'")
    end
  end
end

#validate_configurationObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/anodator/message.rb', line 48

def validate_configuration
  @template.gsub(/\[\[([^:]+)::([^\]]+)\]\]/) do
    Validator::Base.values.spec_item_by_expression($1)
    unless %W(name number value).include?($2)
      raise UnknownMessageAttributeError.new("Unknown message attribute '#{$2}'")
    end
  end
rescue UnknownTargetExpressionError, UnknownMessageAttributeError => e
  raise InvalidConfiguration.new(e.to_s)
end