Class: Tr8n::LanguageContext
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #applies_to_token?(token) ⇒ Boolean
- #config ⇒ Object
- #fallback_rule ⇒ Object
- #find_matching_rule(obj) ⇒ Object
-
#initialize(attrs = {}) ⇒ LanguageContext
constructor
A new instance of LanguageContext.
-
#to_cache_hash ⇒ Object
Cache Methods.
- #token_expression ⇒ Object
-
#vars(obj) ⇒ Object
prepare variables for evaluation.
Methods inherited from Base
attributes, belongs_to, has_many, hash_value, #hash_value, #method_missing, #to_hash, #update_attributes
Constructor Details
#initialize(attrs = {}) ⇒ LanguageContext
Returns a new instance of LanguageContext.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tr8n/language_context.rb', line 31 def initialize(attrs = {}) super self.attributes[:rules] = {} if hash_value(attrs, :rules) hash_value(attrs, :rules).each do |key, rule| self.attributes[:rules][key] = Tr8n::LanguageContextRule.new(rule.merge(:language_context => self)) end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Tr8n::Base
Instance Method Details
#applies_to_token?(token) ⇒ Boolean
55 56 57 |
# File 'lib/tr8n/language_context.rb', line 55 def applies_to_token?(token) token_expression.match(token) != nil end |
#config ⇒ Object
42 43 44 45 |
# File 'lib/tr8n/language_context.rb', line 42 def config context_rules = Tr8n.config.context_rules hash_value(context_rules, keyword.to_sym) || {} end |
#fallback_rule ⇒ Object
59 60 61 |
# File 'lib/tr8n/language_context.rb', line 59 def fallback_rule @fallback_rule ||= rules.values.detect{|rule| rule.fallback?} end |
#find_matching_rule(obj) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/tr8n/language_context.rb', line 98 def find_matching_rule(obj) token_vars = vars(obj) rules.values.each do |rule| next if rule.fallback? return rule if rule.evaluate(token_vars) end fallback_rule end |
#to_cache_hash ⇒ Object
Cache Methods
111 112 113 114 115 116 117 118 |
# File 'lib/tr8n/language_context.rb', line 111 def to_cache_hash hash = to_hash(:keyword, :description, :keys, :default_key, :token_expression, :variables, :token_mapping) hash[:rules] = {} rules.each do |key, rule| hash[:rules][key] = rule.to_cache_hash end hash end |
#token_expression ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/tr8n/language_context.rb', line 47 def token_expression @token_expression ||= begin exp = self.attributes[:token_expression] exp = Regexp.new(exp[1..-2]) exp end end |
#vars(obj) ⇒ Object
prepare variables for evaluation
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/tr8n/language_context.rb', line 64 def vars(obj) vars = {} variables.each do |key| method = hash_value(config, "variables.#{key}") unless method vars[key] = obj next end if method.is_a?(String) if obj.is_a?(Hash) object = hash_value(obj, 'object') || obj if object.is_a?(Hash) vars[key] = hash_value(object, method, :whole => true) else vars[key] = object.send(method) end elsif obj.is_a?(String) vars[key] = obj else vars[key] = obj.send(method) end elsif method.is_a?(Proc) vars[key] = method.call(obj) else vars[key] = obj end vars[key] = vars[key].to_s if vars[key].is_a?(Symbol) end vars end |