Class: Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_parser_extras.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dyn = false) ⇒ Environment

Returns a new instance of Environment.



624
625
626
627
628
629
630
# File 'lib/ruby_parser_extras.rb', line 624

def initialize dyn = false
  @dyn = []
  @env = []
  @use = []
  @init = false
  self.extend
end

Instance Attribute Details

#dynObject (readonly)

Returns the value of attribute dyn.



588
589
590
# File 'lib/ruby_parser_extras.rb', line 588

def dyn
  @dyn
end

#envObject (readonly)

Returns the value of attribute env.



588
589
590
# File 'lib/ruby_parser_extras.rb', line 588

def env
  @env
end

#initObject

Returns the value of attribute init.



589
590
591
# File 'lib/ruby_parser_extras.rb', line 589

def init
  @init
end

Instance Method Details

#[](k) ⇒ Object



591
592
593
# File 'lib/ruby_parser_extras.rb', line 591

def [] k
  self.all[k]
end

#[]=(k, v) ⇒ Object



595
596
597
598
# File 'lib/ruby_parser_extras.rb', line 595

def []= k, v
  raise "no" if v == true
  self.current[k] = v
end

#allObject



600
601
602
603
# File 'lib/ruby_parser_extras.rb', line 600

def all
  idx = @dyn.index false
  @env[0..idx].reverse.inject { |env, scope| env.merge scope }
end

#currentObject



605
606
607
# File 'lib/ruby_parser_extras.rb', line 605

def current
  @env.first
end

#dynamicObject



609
610
611
612
# File 'lib/ruby_parser_extras.rb', line 609

def dynamic
  idx = @dyn.index false
  @env[0...idx].reverse.inject { |env, scope| env.merge scope } || {}
end

#dynamic?Boolean

Returns:

  • (Boolean)


614
615
616
# File 'lib/ruby_parser_extras.rb', line 614

def dynamic?
  @dyn[0] != false
end

#extend(dyn = false) ⇒ Object



618
619
620
621
622
# File 'lib/ruby_parser_extras.rb', line 618

def extend dyn = false
 @dyn.unshift dyn
 @env.unshift({})
 @use.unshift({})
end

#unextendObject



632
633
634
635
636
637
# File 'lib/ruby_parser_extras.rb', line 632

def unextend
  @dyn.shift
  @env.shift
  @use.shift
  raise "You went too far unextending env" if @env.empty?
end

#use(id) ⇒ Object



639
640
641
642
643
644
645
# File 'lib/ruby_parser_extras.rb', line 639

def use id
  @env.each_with_index do |env, i|
    if env[id] then
      @use[i][id] = true
    end
  end
end

#used?(id) ⇒ Boolean

Returns:

  • (Boolean)


647
648
649
650
651
# File 'lib/ruby_parser_extras.rb', line 647

def used? id
  idx = @dyn.index false # REFACTOR
  u = @use[0...idx].reverse.inject { |env, scope| env.merge scope } || {}
  u[id]
end