Module: Ruby::Node::Traversal
- Included in:
- Ruby::Node
- Defined in:
- lib/ruby/node/traversal.rb
Instance Method Summary collapse
- #has_token?(token) ⇒ Boolean
- #has_type?(klass) ⇒ Boolean
- #has_value?(value) ⇒ Boolean
- #is_instance_of?(klass) ⇒ Boolean
- #left_of?(right) ⇒ Boolean
- #matches?(args, &block) ⇒ Boolean
- #position?(pos) ⇒ Boolean
- #right_of?(left) ⇒ Boolean
- #select(*args, &block) ⇒ Object
Instance Method Details
#has_token?(token) ⇒ Boolean
56 57 58 59 60 61 62 63 |
# File 'lib/ruby/node/traversal.rb', line 56 def has_token?(token) case token when ::Array type.each { |type| return true if has_token?(token) } and false else self.token == token end if respond_to?(:token) end |
#has_type?(klass) ⇒ Boolean
38 39 40 41 42 43 44 45 |
# File 'lib/ruby/node/traversal.rb', line 38 def has_type?(klass) case klass when ::Array klass.each { |klass| return true if has_type?(klass) } and false else is_a?(klass) # allow to pass a symbol or string, too end end |
#has_value?(value) ⇒ Boolean
65 66 67 |
# File 'lib/ruby/node/traversal.rb', line 65 def has_value?(value) self.value == value if respond_to?(:value) end |
#is_instance_of?(klass) ⇒ Boolean
47 48 49 50 51 52 53 54 |
# File 'lib/ruby/node/traversal.rb', line 47 def is_instance_of?(klass) case klass when ::Array klass.each { |klass| return true if has_type?(klass) } and false else instance_of?(klass) # allow to pass a symbol or string, too end end |
#left_of?(right) ⇒ Boolean
73 74 75 |
# File 'lib/ruby/node/traversal.rb', line 73 def left_of?(right) right.nil? || self.position < right.position end |
#matches?(args, &block) ⇒ Boolean
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ruby/node/traversal.rb', line 14 def matches?(args, &block) conditions = args.last.is_a?(::Hash) ? args.pop : {} conditions[:is_a] = args unless args.empty? conditions.inject(!conditions.empty?) do |result, (type, value)| result && case type when :is_a has_type?(value) when :class is_instance_of?(value) when :token has_token?(value) when :value has_value?(value) when :pos, :position position?(value) when :right_of right_of?(value) when :left_of left_of?(value) end end && (!block_given? || block.call(self)) end |
#position?(pos) ⇒ Boolean
69 70 71 |
# File 'lib/ruby/node/traversal.rb', line 69 def position?(pos) position == pos end |
#right_of?(left) ⇒ Boolean
77 78 79 |
# File 'lib/ruby/node/traversal.rb', line 77 def right_of?(left) left.nil? || left.position < self.position end |
#select(*args, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/ruby/node/traversal.rb', line 4 def select(*args, &block) result = [] result << self if matches?(args.dup, &block) children = (prolog.try(:elements).to_a || []) + nodes children.flatten.compact.inject(result) do |result, node| result + node.select(*args, &block) end end |