Class: Sexp

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modifiedObject Also known as: modified?

Whether or not this sexp is a mutated/modified sexp.



570
571
572
# File 'lib/flay.rb', line 570

def modified
  @modified
end

Instance Method Details

#+(o) ⇒ Object

:nodoc:



611
612
613
# File 'lib/flay.rb', line 611

def + o # :nodoc:
  self.dup.concat o
end

#[](a) ⇒ Object

:nodoc:



601
602
603
604
605
606
607
608
609
# File 'lib/flay.rb', line 601

def [] a # :nodoc:
  s = super
  if Sexp === s then
    s.file = self.file
    s.line = self.line
    s.modified = self.modified
  end
  s
end

#all_structural_subhashesObject

Returns a list of structural hashes for all nodes (and sub-nodes) of this sexp.



585
586
587
588
589
590
591
# File 'lib/flay.rb', line 585

def all_structural_subhashes
  hashes = []
  self.deep_each do |node|
    hashes << node.structural_hash
  end
  hashes
end

#code_indexObject Also known as: has_code?

Return the index of the last non-code element, or nil if this sexp is not a code-bearing node.



627
628
629
630
631
632
633
634
635
636
# File 'lib/flay.rb', line 627

def code_index
  {
   :block  => 0,    # s(:block,                   *code)
   :class  => 2,    # s(:class,      name, super, *code)
   :module => 1,    # s(:module,     name,        *code)
   :defn   => 2,    # s(:defn,       name, args,  *code)
   :defs   => 3,    # s(:defs, recv, name, args,  *code)
   :iter   => 2,    # s(:iter, recv,       args,  *code)
  }[self.sexp_type]
end

#initialize_copy(o) ⇒ Object

:nodoc:



593
594
595
596
597
598
599
# File 'lib/flay.rb', line 593

def initialize_copy o # :nodoc:
  s = super
  s.file = o.file
  s.line = o.line
  s.modified = o.modified
  s
end

#split_at(n) ⇒ Object

Useful general array method that splits the array from 0..n and the rest. Returns both sections.



619
620
621
# File 'lib/flay.rb', line 619

def split_at n
  return self[0..n], self[n+1..-1]
end

#split_codeObject

Split the sexp into front-matter and code-matter, returning both. See #code_index.



644
645
646
647
# File 'lib/flay.rb', line 644

def split_code
  index = self.code_index
  self.split_at index if index
end

#structural_hashObject

Calculate the structural hash for this sexp. Cached, so don’t modify the sexp afterwards and expect it to be correct.



577
578
579
# File 'lib/flay.rb', line 577

def structural_hash
  @structural_hash ||= self.structure.hash
end