Class: XDry::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/xdry/parsing/scopes_support.rb

Direct Known Subclasses

ChildScope, SFile

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScope

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_classObject (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

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/xdry/parsing/scopes_support.rb', line 7

def children
  @children
end

#modelObject (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_tableObject



114
115
116
# File 'lib/xdry/parsing/scopes_support.rb', line 114

def child_action_table
  @child_action_table ||= {}
end

.child_collectionsObject



118
119
120
# File 'lib/xdry/parsing/scopes_support.rb', line 118

def child_collections
  @child_collections ||= []
end

.child_subscope_tableObject



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
  options = args.last.is_a?(Hash) ? args.pop : {}
  if args.include? :pop
    stop_children << child_class
  end
  if options[:start]
    child_subscope_table[child_class] = options[:start]
  end
  if var_name = options[: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 = options[: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_childrenObject



106
107
108
# File 'lib/xdry/parsing/scopes_support.rb', line 106

def stop_children
  @stop_children ||= []
end

Instance Method Details

#<<(child) ⇒ Object

Raises:

  • (StandardError)


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_scopesObject



61
62
63
# File 'lib/xdry/parsing/scopes_support.rb', line 61

def all_scopes
  parent_scopes + [self]
end

#assert_bound!Object

Raises:

  • (StandardError)


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

Raises:

  • (StandardError)


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

Returns:

  • (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_scopesObject



57
58
59
# File 'lib/xdry/parsing/scopes_support.rb', line 57

def parent_scopes
  []
end

#parserObject



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_sObject



53
54
55
# File 'lib/xdry/parsing/scopes_support.rb', line 53

def to_s
  "#{self.class.name}:#{@model}"
end