Class: Dry::Validation::Messages::Template Private
- Inherits:
-
Object
- Object
- Dry::Validation::Messages::Template
- Defined in:
- lib/dry/validation/template.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Template wraps a string with interpolation tokens and defines evaluator function dynamically
Constant Summary collapse
- TOKEN_REGEXP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
/%{(\w*)}/
Instance Attribute Summary collapse
-
#evaluator ⇒ Proc
readonly
private
!@attribute [r] evaluator.
-
#text ⇒ String
readonly
private
!@attribute [r] text.
-
#tokens ⇒ Hash
readonly
private
!@attribute [r] tokens.
Class Method Summary collapse
- .[](input) ⇒ Object private
- .parse(input) ⇒ Object private
Instance Method Summary collapse
- #call(data = EMPTY_HASH) ⇒ Object (also: #[]) private
- #data(input) ⇒ Object private
-
#initialize(text, tokens, evaluator) ⇒ Template
constructor
private
A new instance of Template.
Constructor Details
#initialize(text, tokens, evaluator) ⇒ Template
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Template.
47 48 49 50 51 |
# File 'lib/dry/validation/template.rb', line 47 def initialize(text, tokens, evaluator) @text = text @tokens = tokens @evaluator = evaluator end |
Instance Attribute Details
#evaluator ⇒ Proc (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
!@attribute [r] evaluator
27 28 29 |
# File 'lib/dry/validation/template.rb', line 27 def evaluator @evaluator end |
#text ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
!@attribute [r] text
19 20 21 |
# File 'lib/dry/validation/template.rb', line 19 def text @text end |
#tokens ⇒ Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
!@attribute [r] tokens
23 24 25 |
# File 'lib/dry/validation/template.rb', line 23 def tokens @tokens end |
Class Method Details
.[](input) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
30 31 32 |
# File 'lib/dry/validation/template.rb', line 30 def self.[](input) new(*parse(input)) end |
.parse(input) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dry/validation/template.rb', line 35 def self.parse(input) tokens = input.scan(TOKEN_REGEXP).flatten(1).map(&:to_sym) text = input.gsub('%', '#') evaluator = <<-RUBY.strip -> (#{tokens.map { |token| "#{token}:" }.join(", ")}) { "#{text}" } RUBY [text, tokens, eval(evaluator, binding, __FILE__, __LINE__ - 3)] end |
Instance Method Details
#call(data = EMPTY_HASH) ⇒ Object Also known as: []
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 |
# File 'lib/dry/validation/template.rb', line 59 def call(data = EMPTY_HASH) data.empty? ? evaluator.() : evaluator.(data) end |
#data(input) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
54 55 56 |
# File 'lib/dry/validation/template.rb', line 54 def data(input) tokens.each_with_object({}) { |k, h| h[k] = input[k] } end |