Class: Dentaku::RuleSet
- Inherits:
-
Object
- Object
- Dentaku::RuleSet
- Defined in:
- lib/dentaku/rule_set.rb
Instance Method Summary collapse
- #add_function(function) ⇒ Object
- #each ⇒ Object
- #filter(tokens) ⇒ Object
- #function(name) ⇒ Object
-
#initialize ⇒ RuleSet
constructor
A new instance of RuleSet.
- #rules ⇒ Object
- #select(categories, values) ⇒ Object
Constructor Details
#initialize ⇒ RuleSet
Returns a new instance of RuleSet.
5 6 7 8 |
# File 'lib/dentaku/rule_set.rb', line 5 def initialize self.custom_rules = [] self.custom_functions = {} end |
Instance Method Details
#add_function(function) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dentaku/rule_set.rb', line 18 def add_function(function) fn = ExternalFunction.new(function[:name], function[:type], function[:signature], function[:body]) custom_rules.push [ function_token_matchers(fn.name, *fn.tokens), fn.name ] custom_functions[fn.name] = fn clear_cache end |
#each ⇒ Object
14 15 16 |
# File 'lib/dentaku/rule_set.rb', line 14 def each rules.each { |r| yield r } end |
#filter(tokens) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/dentaku/rule_set.rb', line 30 def filter(tokens) categories = tokens.map(&:category).uniq values = tokens.map { |token| token.value.is_a?(Numeric) ? 0 : token.value } .reject { |token| [:fopen, :close].include?(token) } select(categories, values) end |
#function(name) ⇒ Object
48 49 50 |
# File 'lib/dentaku/rule_set.rb', line 48 def function(name) custom_functions.fetch(name) end |
#rules ⇒ Object
10 11 12 |
# File 'lib/dentaku/rule_set.rb', line 10 def rules custom_rules + core_rules end |
#select(categories, values) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dentaku/rule_set.rb', line 37 def select(categories, values) @cache ||= {} return @cache[categories + values] if @cache.has_key?(categories + values) @cache[categories + values] = rules.select do |pattern, _| categories_intersection = matcher_categories[pattern] & categories values_intersection = matcher_values[pattern] & values categories_intersection.length > 0 && (values_intersection.length > 0 || matcher_values[pattern].empty?) end end |