Class: MailyHerald::Utils::MarkupEvaluator
- Inherits:
-
Object
- Object
- MailyHerald::Utils::MarkupEvaluator
- Defined in:
- lib/maily_herald/utils.rb
Defined Under Namespace
Modules: Filters
Constant Summary collapse
- VariableSignature =
/\A[\w\.\[\]]+(\s*|\s*.+)?\Z/
Class Method Summary collapse
Instance Method Summary collapse
- #evaluate_conditions(conditions) ⇒ Object
- #evaluate_start_at(markup) ⇒ Object
-
#initialize(drop) ⇒ MarkupEvaluator
constructor
A new instance of MarkupEvaluator.
Constructor Details
#initialize(drop) ⇒ MarkupEvaluator
Returns a new instance of MarkupEvaluator.
64 65 66 |
# File 'lib/maily_herald/utils.rb', line 64 def initialize drop @drop = drop end |
Class Method Details
.test_conditions(conditions) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/maily_herald/utils.rb', line 22 def self.test_conditions conditions return true if !conditions || conditions.empty? drop = Class.new(Liquid::Drop) do def has_key?(name); true; end def invoke_drop(name); true; end alias :[] :invoke_drop end.new evaluator = Utils::MarkupEvaluator.new(drop) evaluator.evaluate_conditions(conditions) true rescue return false end |
.test_start_at(markup) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/maily_herald/utils.rb', line 38 def self.test_start_at markup return true if !markup || markup.empty? drop = Class.new(Liquid::Drop) do def has_key?(name); true; end def invoke_drop(name) t = Time.now t.define_singleton_method(:[]) do |v| Time.now end t.define_singleton_method(:has_key?) do |v| true end t end alias :[] :invoke_drop end.new evaluator = Utils::MarkupEvaluator.new(drop) val = evaluator.evaluate_start_at(markup) return val.is_a?(Time) || val.is_a?(DateTime) rescue return false end |
Instance Method Details
#evaluate_conditions(conditions) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/maily_herald/utils.rb', line 68 def evaluate_conditions conditions return true if !conditions || conditions.empty? condition = MarkupEvaluator.create_liquid_condition conditions template = Liquid::Template.parse(conditions) raise StandardError unless template.errors.empty? liquid_context = Liquid::Context.new([@drop, template.assigns], template.instance_assigns, template.registers, true, {}) @drop.context = liquid_context if @drop.is_a?(Liquid::Drop) val = condition.evaluate liquid_context raise(ArgumentError, "Conditions do not evaluate to boolean (got `#{val}`)") unless [true, false].include?(val) val end |
#evaluate_start_at(markup) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/maily_herald/utils.rb', line 83 def evaluate_start_at markup begin Time.parse(markup) rescue raise(ArgumentError, "Start at is not a proper variable: `#{markup}`") unless VariableSignature =~ markup liquid_context = Liquid::Context.new([@drop], {}, {}, true, {}) liquid_context.add_filters([Filters::Date]) @drop.context = liquid_context if @drop.is_a?(Liquid::Drop) #liquid_context[markup] variable = Liquid::Variable.new markup variable.render(liquid_context) end end |