Class: Dry::Schema::Messages::Template Private
- Inherits:
-
Object
- Object
- Dry::Schema::Messages::Template
- Defined in:
- lib/dry/schema/messages/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.
49 50 51 52 53 |
# File 'lib/dry/schema/messages/template.rb', line 49 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
29 30 31 |
# File 'lib/dry/schema/messages/template.rb', line 29 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
21 22 23 |
# File 'lib/dry/schema/messages/template.rb', line 21 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
25 26 27 |
# File 'lib/dry/schema/messages/template.rb', line 25 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.
32 33 34 |
# File 'lib/dry/schema/messages/template.rb', line 32 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.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dry/schema/messages/template.rb', line 37 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.
61 62 63 |
# File 'lib/dry/schema/messages/template.rb', line 61 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.
56 57 58 |
# File 'lib/dry/schema/messages/template.rb', line 56 def data(input) tokens.each_with_object({}) { |k, h| h[k] = input[k] } end |