Class: XDry::Scope
- Inherits:
-
Object
- Object
- XDry::Scope
- Defined in:
- lib/xdry/parsing/scopes_support.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.parser_class ⇒ Object
readonly
Returns the value of attribute parser_class.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
- .child_action_table ⇒ Object
- .child_collections ⇒ Object
- .child_subscope_table ⇒ Object
-
.on(child_class, *args) ⇒ Object
on NSome, :pop, :store_into => :my_var on NOther, :start => SOther on NAwesome, :add_to => :awesomes.
- .parse_using(parser_class) ⇒ Object
- .stop_children ⇒ Object
Instance Method Summary collapse
- #<<(child) ⇒ Object
- #all_scopes ⇒ Object
- #assert_bound! ⇒ Object
- #bind(model) ⇒ Object
- #child_added(child) ⇒ Object
- #ends_after?(node) ⇒ Boolean
-
#initialize ⇒ Scope
constructor
A new instance of Scope.
- #parent_scopes ⇒ Object
- #parser ⇒ Object
- #subscope_for(node) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Scope
Returns a new instance of Scope.
9 10 11 12 13 14 |
# File 'lib/xdry/parsing/scopes_support.rb', line 9 def initialize @children = [] self.class.child_collections.each do |var_name| instance_variable_set("@#{var_name}", []) end end |
Class Attribute Details
.parser_class ⇒ Object (readonly)
Returns the value of attribute parser_class.
74 75 76 |
# File 'lib/xdry/parsing/scopes_support.rb', line 74 def parser_class @parser_class end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
7 8 9 |
# File 'lib/xdry/parsing/scopes_support.rb', line 7 def children @children end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/xdry/parsing/scopes_support.rb', line 6 def model @model end |
Class Method Details
.child_action_table ⇒ Object
114 115 116 |
# File 'lib/xdry/parsing/scopes_support.rb', line 114 def child_action_table @child_action_table ||= {} end |
.child_collections ⇒ Object
118 119 120 |
# File 'lib/xdry/parsing/scopes_support.rb', line 118 def child_collections @child_collections ||= [] end |
.child_subscope_table ⇒ Object
110 111 112 |
# File 'lib/xdry/parsing/scopes_support.rb', line 110 def child_subscope_table @child_subscope_table ||= {} end |
.on(child_class, *args) ⇒ Object
on NSome, :pop, :store_into => :my_var on NOther, :start => SOther on NAwesome, :add_to => :awesomes
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/xdry/parsing/scopes_support.rb', line 83 def on child_class, *args = args.last.is_a?(Hash) ? args.pop : {} if args.include? :pop stop_children << child_class end if [:start] child_subscope_table[child_class] = [:start] end if var_name = [:store_into] (child_action_table[child_class] ||= []) << lambda do |instance, child| instance.send(:instance_variable_set, "@#{var_name}", child) end attr_reader :"#{var_name}" end if coll_name = [:add_to] (child_action_table[child_class] ||= []) << lambda do |instance, child| instance.send(:instance_variable_get, "@#{coll_name}") << child end child_collections << coll_name attr_reader :"#{coll_name}" end end |
.parse_using(parser_class) ⇒ Object
76 77 78 |
# File 'lib/xdry/parsing/scopes_support.rb', line 76 def parse_using parser_class @parser_class = parser_class end |
.stop_children ⇒ Object
106 107 108 |
# File 'lib/xdry/parsing/scopes_support.rb', line 106 def stop_children @stop_children ||= [] end |
Instance Method Details
#<<(child) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/xdry/parsing/scopes_support.rb', line 42 def << child raise StandardError, "#{self.class.name} hasn't been bound to a model, but is trying to accept a child #{child}" if @model.nil? @children << child @model << child child_added child end |
#all_scopes ⇒ Object
61 62 63 |
# File 'lib/xdry/parsing/scopes_support.rb', line 61 def all_scopes parent_scopes + [self] end |
#assert_bound! ⇒ Object
22 23 24 |
# File 'lib/xdry/parsing/scopes_support.rb', line 22 def assert_bound! raise StandardError, "#{self.class.name} hasn't been bound to a model" if @model.nil? end |
#bind(model) ⇒ Object
16 17 18 19 20 |
# File 'lib/xdry/parsing/scopes_support.rb', line 16 def bind model raise StandardError, "#{self} already bound to #{@model} when trying to bind to #{model}" unless @model.nil? @model = model self end |
#child_added(child) ⇒ Object
49 50 51 |
# File 'lib/xdry/parsing/scopes_support.rb', line 49 def child_added child (self.class.child_action_table[child.class] || []).each { |action| action.call(self, child) } end |
#ends_after?(node) ⇒ Boolean
38 39 40 |
# File 'lib/xdry/parsing/scopes_support.rb', line 38 def ends_after? node self.class.stop_children.include? node.class end |
#parent_scopes ⇒ Object
57 58 59 |
# File 'lib/xdry/parsing/scopes_support.rb', line 57 def parent_scopes [] end |
#parser ⇒ Object
26 27 28 |
# File 'lib/xdry/parsing/scopes_support.rb', line 26 def parser @parser ||= create_parser end |
#subscope_for(node) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/xdry/parsing/scopes_support.rb', line 30 def subscope_for node if subscope_class = self.class.child_subscope_table[node.class] subscope_class.new(self, node) else nil end end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/xdry/parsing/scopes_support.rb', line 53 def to_s "#{self.class.name}:#{@model}" end |