Class: Flooph

Inherits:
Parslet::Parser
  • Object
show all
Defined in:
lib/flooph.rb

Defined Under Namespace

Classes: Transform

Constant Summary collapse

VERSION =
'0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}) ⇒ Flooph

Create a new, reusable template parser and evaluator.

Parameters:

  • vars (Hash) (defaults to: {})

    symbol-to-values used in templates and conditional evaluations.



13
14
15
16
# File 'lib/flooph.rb', line 13

def initialize(vars={})
  super()
  @vars = vars
end

Instance Attribute Details

#varsObject

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

Evaluate an expression, looking up values and performing simple math.

f = Flooph.new cats:17, dogs:25
p f.calculate("cats + dogs")
#=> 42

Parameters:

  • vars (Hash) (defaults to: nil)

    variable values to use for this and future evaluations. If omitted, existing variable values will be used. (see also #vars and #update_variables)

Returns:

  • (Hash)

    the new variable values after updating.



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 `&&`

Parameters:

  • vars (Hash) (defaults to: nil)

    variable values to use for this and future evaluations. If omitted, existing variable values will be used. (see also #vars and #update_variables)

Returns:

  • (true, false)

    the result of the evaluation.



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.                         #
{.}                                            #

Parameters:

  • vars (Hash) (defaults to: nil)

    variable values to use for this and future evaluations. If omitted, existing variable values will be used. (see also #vars and #update_variables)

Returns:

  • (String)

    the template after transformation.



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_

Parameters:

  • vars (Hash) (defaults to: nil)

    initial variable values to base references on. If omitted, existing variable values will be used.

Returns:

  • (Hash)

    the new variable values after updating.



107
108
109
110
# File 'lib/flooph.rb', line 107

def update_variables(str, vars=nil)
  parse_and_transform(:varset, str, vars)
  @vars
end