eggshell 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d381930fd686eeb75503e121d00b86720a29440
4
- data.tar.gz: edd613b6221cf1af94b7253698191181ee3b0ed6
3
+ metadata.gz: 9169e6a1f27f0985214179e92840cee5f1a265b2
4
+ data.tar.gz: 0a481dda76b622003c1e3506fc82a9bc05cf6993
5
5
  SHA512:
6
- metadata.gz: fa442f2e0bb011ff8e70c437b6953f5861a200124509dc82bf77faa1c6c0205bff900cad37cd02569cb1738b99a3875055a7e896c78944b9af4d4fb2018a62c6
7
- data.tar.gz: 63d4368671e260d6a9a4a126bd6d32a15aa7e79d678f8fce9799efadbb06fed7af020c56c2fd2e2e67d9f7f21393cc2ebc1b547679c13e3372bfdd6699693e26
6
+ metadata.gz: 5089ec5885195c71ca2b3806696817dd50d97e5f2abd70ec07468fd23d536d1e9de66ef6c9cb13b9b41469daac8bd4e548973a43cde996f8574e3c27570b5ad5
7
+ data.tar.gz: 4f0b184e3f38f569dcfec07e9f9979cb7c59e32bc7357804e674e935cfa06237a76a4e926675c5f4a02fcb9bd5561e399c5ba6a2d74e1c7eb3b7eaa38a55e102
data/lib/eggshell.rb CHANGED
@@ -71,6 +71,7 @@ module Eggshell
71
71
  @blocks = @context.blocks
72
72
  @block_params = @context.block_params
73
73
  @expr_cache = @context.expr_cache
74
+ @ee = Eggshell::ExpressionEvaluator.new(@vars, @funcs)
74
75
 
75
76
  @noop_macro = Eggshell::MacroHandler::Defaults::NoOpHandler.new
76
77
  @noop_block = Eggshell::BlockHandler::Defaults::NoOpHandler.new
@@ -102,13 +103,7 @@ module Eggshell
102
103
  # @param Array func_names If `func_key` only refers to a namespace but the handler
103
104
  # needs to only handle a subset of functions, supply the list of function names here.
104
105
  def register_functions(func_key, handler, func_names = nil)
105
- if !func_key.index(':') && func_names.is_a?(Array)
106
- func_names.each do |fname|
107
- @funcs[func_key+func_name] = handler
108
- end
109
- else
110
- @funcs[func_key] = handler
111
- end
106
+ @ee.register_functions(func_key, handler, func_names)
112
107
  end
113
108
 
114
109
  def _error(msg)
@@ -1,23 +1,5 @@
1
- # Parses and evaluates statements (expressions).
2
- #
3
- # pre.
4
- # # simple expression
5
- # 1 + 5
6
- # 1 < (5 + 8) || 3 * 4 > 2
7
- #
8
- # # arrays and maps
9
- # [1, 2, 3]
10
- # [1, [2, 3], 4]
11
- # {'key': 'val', 'another': 'val2', 'num': 8.2}
12
- # [1, {'key': 'val'}]
13
- # {'arr': [1, 2, 3]}
14
- #
15
- # # variables set via @var macro
16
- # var.name + 5
17
- #
18
- # # function calls
19
- # fn1(arg1, "arg2", 3) + fn2({}, [])
20
- # /pre
1
+ # Parses and evaluates statements (expressions). Can be used statically or
2
+ # as an instance.
21
3
  module Eggshell;end
22
4
  class Eggshell::ExpressionEvaluator
23
5
  REGEX_EXPR_PLACEHOLDERS = /(\\|\$\[|\$\{|\]|\}|\+|\-|>|<|=|\s+|\(|\)|\*|\/`)/
@@ -98,6 +80,47 @@ class Eggshell::ExpressionEvaluator
98
80
  end
99
81
  end
100
82
 
83
+ def initialize(vars = nil, funcs = nil)
84
+ @vars = vars || {}
85
+ @funcs = funcs || {}
86
+ @cache = {}
87
+ end
88
+
89
+ # Registers a function for embedded expressions. Functions are grouped into namespaces,
90
+ # and a handler can be assigned to handle all function calls within that namespace, or
91
+ # a specific set of functions within the namespace. The root namespace is a blank string.
92
+ #
93
+ # @param String func_key In the form `ns` or `ns:func_name`. For functions in the
94
+ # root namespace, do `:func_name`.
95
+ # @param Object handler
96
+ # @param Array func_names If `func_key` only refers to a namespace but the handler
97
+ # needs to only handle a subset of functions, supply the list of function names here.
98
+ def register_functions(func_key, handler, func_names = nil)
99
+ if !func_key.index(':') && func_names.is_a?(Array)
100
+ func_names.each do |fname|
101
+ @funcs[func_key+func_name] = handler
102
+ end
103
+ else
104
+ @funcs[func_key] = handler
105
+ end
106
+ end
107
+
108
+ attr_reader :vars, :funcs
109
+
110
+ def parse(statement, cache = true)
111
+ parsed = @cache[statement]
112
+ return parsed if cache && parsed
113
+
114
+ parsed = self.class.struct(statement)
115
+ @cache[statement] = parsed if cache
116
+ return parsed
117
+ end
118
+
119
+ def evaluate(statement, cache = true)
120
+ parsed = parse(statement, cache)
121
+ return self.class.expr_eval(parsed, @vars, @funcs)
122
+ end
123
+
101
124
  # Normalizes a term.
102
125
  # @param Object term If `String`, attempts to infer type (either `Fixnum`, `Float`, or `[:var, varname]`)
103
126
  # @param Boolean preserve_str If true and input is string but not number, return string literal.
@@ -859,4 +882,8 @@ class Eggshell::ExpressionEvaluator
859
882
  end
860
883
 
861
884
  $errs_write = 10
862
- $errs_write_indent = 10
885
+ $errs_write_indent = 10
886
+
887
+ ee = Eggshell::ExpressionEvaluator.new
888
+ ee.vars['hello'] = 1
889
+ puts ee.evaluate("hello + 1")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eggshell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaiser Shahid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-17 00:00:00.000000000 Z
11
+ date: 2016-12-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: From fast and basic HTML to complex decouments and more, Eggshell aims
14
14
  to provide you with all the document generation power you need through simple markup.