Class: Flooph
- Inherits:
-
Parslet::Parser
- Object
- Parslet::Parser
- Flooph
- Defined in:
- lib/flooph.rb
Defined Under Namespace
Classes: Transform
Constant Summary collapse
- VERSION =
'0.2'
Instance Attribute Summary collapse
-
#vars ⇒ Object
The current values used when evaluating templates and conditionals.
Instance Method Summary collapse
-
#calculate(str, vars = nil) ⇒ Hash
Evaluate an expression, looking up values and performing simple math.
-
#conditional(str, vars = nil) ⇒ true, false
Evaluate simple conditional expressions to a boolean value, with variable lookup.
-
#initialize(vars = {}) ⇒ Flooph
constructor
Create a new, reusable template parser and evaluator.
-
#transform(str, vars = nil) ⇒ String
Evaluate a template like the following example, inserting content and evaluating conditional branches.
-
#update_variables(str, vars = nil) ⇒ Hash
Parse a simple hash setup for setting and updating values.
Constructor Details
#initialize(vars = {}) ⇒ Flooph
Create a new, reusable template parser and evaluator.
13 14 15 16 |
# File 'lib/flooph.rb', line 13 def initialize(vars={}) super() @vars = vars end |
Instance Attribute Details
#vars ⇒ Object
The current values used when evaluating templates and conditionals. Can also be updated by user input using #update_variables.
8 9 10 |
# File 'lib/flooph.rb', line 8 def vars @vars end |
Instance Method Details
#calculate(str, vars = nil) ⇒ Hash
122 123 124 |
# File 'lib/flooph.rb', line 122 def calculate(str, vars=nil) parse_and_transform(:value, str, vars) end |
#conditional(str, vars = nil) ⇒ true, false
Evaluate simple conditional expressions to a boolean value, with variable lookup. Examples:
cats? # variable cats is set (and not set to `false` or `no`)
cats = 42 # variable `cats` equals 42
cats>0 & dogs>0 # if both variables are numbers greater than zero
-
Numeric and string comparisons, using any of ‘< > = == ≤ <= ≥ >= ≠ !=`
-
Non-present variables or invalid comparisons always result in false
-
-
Variable presence/truthiness using just name (isDead) or with optional trailing question mark (isDead?).
-
Boolean composition, e.g. ‘a | b & c & (d | !e) || !(f && g)`
-
‘!foo` means “not foo”, inverting the meaning
-
‘&` has higher precedence than `|`
-
‘|` is the same as `||`; `&` is the same as `&&`
-
76 77 78 |
# File 'lib/flooph.rb', line 76 def conditional(str, vars=nil) parse_and_transform(:boolean_expression, str, vars) end |
#transform(str, vars = nil) ⇒ String
Evaluate a template like the following example, inserting content and evaluating conditional branches. If you don’t supply ‘vars` then the existing values for the instance are used.
Hello, {=name}! # Insert values from variables.
#
{?trollLocation="cave"} # Conditional based on boolean expressions.
There is a troll glaring at you. # See #conditional for how to write conditions.
{|} # Conditional 'else' clause.
The air smells bad here, like rotting meat. #
{.} # End of the if/else.
#
{?debug}Troll is at {=trollLocation}.{.} # Conditional based on if the variable exists (and isn't false)
#
{?dogs>0} #
I own {=dogs} dogg{?dogs=1}y{|}ies{.} now. # Conditionals can be inline.
{.} #
#
{? cats=42 } #
I have exactly 42 cats! I'll never get more. #
{| cats=1 } # Else-if for chained conditionals.
I have a cat. If I get another, I'll have two. #
{| cats>1 } #
I have {=cats} cats. #
If I get another, I'll have {=cats+1}. # Output can do simple addition/subtraction.
{|} #
I don't have any cats. #
{.} #
51 52 53 54 55 |
# File 'lib/flooph.rb', line 51 def transform(str, vars=nil) parse_and_transform(:mkup, str, vars).tap do |result| result.gsub!(/\n{3,}/, "\n\n") if result end end |
#update_variables(str, vars = nil) ⇒ Hash
Parse a simple hash setup for setting and updating values. For example:
f = Flooph.new # No variables yet
f.update_variables <<-END
debug: false
cats: 17
alive: yes
trollLocation: "cave"
END
f.conditional "cats > 3"
#=> true
f.update_variables "oldCats:cats \n cats: cats + 1"
f.calculate "cats"
#=> 18
f.calculate "oldCats"
#=> 17
Legal value types are:
-
Booleans: ‘true`, `false`, `yes`, `no`
-
Numbers: ‘-3`, `12`, `3.1415`
-
Strings: ‘“foo”`, `“Old Barn”` _must use double quotes_
-
Variables: ‘cats + 7` _only supports add/subtract, not multiplication_
107 108 109 110 |
# File 'lib/flooph.rb', line 107 def update_variables(str, vars=nil) parse_and_transform(:varset, str, vars) @vars end |