lib/glimmer/dsl/engine.rb in glimmer-0.6.0 vs lib/glimmer/dsl/engine.rb in glimmer-0.7.0

- old
+ new

@@ -22,10 +22,40 @@ def dsl dsl_stack.last end + def dsls + static_expressions.values.map(&:keys).flatten.uniq + end + + def disable_dsl(dsl_name) + dsl_name = dsl_name.to_sym + disabled_dsls << dsl_name + end + + def enable_dsl(dsl_name) + dsl_name = dsl_name.to_sym + disabled_dsls.delete(dsl_name) + end + + def disabled_dsls + @disabled_dsls ||= [] + end + + def enabled_dsls=(dsl_names) + dsls.each {|dsl_name| disable_dsl(dsl_name)} + dsl_names.each {|dsl_name| enable_dsl(dsl_name)} + end + + # Resets Glimmer's engine activity and configuration. Useful in rspec before or after blocks in tests. + def reset + parent_stack.clear + dsl_stack.clear + disabled_dsls.clear + end + # Dynamic expression chains of responsibility indexed by dsl def dynamic_expression_chains_of_responsibility @dynamic_expression_chains_of_responsibility ||= {} end @@ -44,27 +74,27 @@ def add_dynamic_expressions(dsl_namespace, expression_names) dsl = dsl_namespace.name.split("::").last.downcase.to_sym dynamic_expression_chains_of_responsibility[dsl] = expression_names.reverse.map do |expression_name| expression_class(dsl_namespace, expression_name).new end.reduce(nil) do |last_expresion_handler, expression| - Glimmer.logger&.debug "Adding dynamic expression: #{expression.class.name}" + Glimmer::Config.logger&.debug "Adding dynamic expression: #{expression.class.name}" expression_handler = ExpressionHandler.new(expression) expression_handler.next = last_expresion_handler if last_expresion_handler expression_handler end end def add_static_expression(static_expression) - Glimmer.logger&.debug "Adding static expression: #{static_expression.class.name}" + Glimmer::Config.logger&.debug "Adding static expression: #{static_expression.class.name}" keyword = static_expression.class.keyword static_expression_dsl = static_expression.class.dsl static_expressions[keyword] ||= {} static_expressions[keyword][static_expression_dsl] = static_expression Glimmer.define_method(keyword) do |*args, &block| begin retrieved_static_expression = Glimmer::DSL::Engine.static_expressions[keyword][Glimmer::DSL::Engine.dsl] - static_expression_dsl = Glimmer::DSL::Engine.static_expressions[keyword].keys.first if retrieved_static_expression.nil? + static_expression_dsl = (Glimmer::DSL::Engine.static_expressions[keyword].keys - Glimmer::DSL::Engine.disabled_dsls).first if retrieved_static_expression.nil? if retrieved_static_expression.nil? && Glimmer::DSL::Engine.dsl && (static_expression_dsl.nil? || !Glimmer::DSL::Engine.static_expressions[keyword][static_expression_dsl].is_a?(TopLevelExpression)) begin return Glimmer::DSL::Engine.interpret(keyword, *args, &block) rescue => e raise e if static_expression_dsl.nil? @@ -74,11 +104,11 @@ Glimmer::DSL::Engine.dsl_stack.push(static_expression_dsl || Glimmer::DSL::Engine.dsl) static_expression = Glimmer::DSL::Engine.static_expressions[keyword][Glimmer::DSL::Engine.dsl] if !static_expression.can_interpret?(Glimmer::DSL::Engine.parent, keyword, *args, &block) raise Error, "Invalid use of Glimmer keyword #{keyword} with args #{args} under parent #{Glimmer::DSL::Engine.parent}" else - Glimmer.logger&.debug "#{static_expression.class.name} will handle expression keyword #{keyword}" + Glimmer::Config.logger&.debug "#{static_expression.class.name} will handle expression keyword #{keyword}" return static_expression.interpret(Glimmer::DSL::Engine.parent, keyword, *args, &block).tap do |ui_object| Glimmer::DSL::Engine.add_content(ui_object, static_expression, &block) unless block.nil? Glimmer::DSL::Engine.dsl_stack.pop end end @@ -98,10 +128,10 @@ end # Interprets Glimmer dynamic DSL expression consisting of keyword, args, and block (e.g. shell(:no_resize) { ... }) def interpret(keyword, *args, &block) keyword = keyword.to_s - dynamic_expression_dsl = dynamic_expression_chains_of_responsibility.keys.first if dsl.nil? + dynamic_expression_dsl = (dynamic_expression_chains_of_responsibility.keys - disabled_dsls).first if dsl.nil? dsl_stack.push(dynamic_expression_dsl || dsl) expression = dynamic_expression_chains_of_responsibility[dsl].handle(parent, keyword, *args, &block) expression.interpret(parent, keyword, *args, &block).tap do |ui_object| add_content(ui_object, expression, &block) dsl_stack.pop