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.



491
492
493
# File 'lib/flay.rb', line 491

def modified
  @modified
end

Instance Method Details

#+(o) ⇒ Object

:nodoc:



532
533
534
# File 'lib/flay.rb', line 532

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

#[](a) ⇒ Object

:nodoc:



522
523
524
525
526
527
528
529
530
# File 'lib/flay.rb', line 522

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.



506
507
508
509
510
511
512
# File 'lib/flay.rb', line 506

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.



548
549
550
551
552
553
554
555
556
557
# File 'lib/flay.rb', line 548

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:



514
515
516
517
518
519
520
# File 'lib/flay.rb', line 514

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.



540
541
542
# File 'lib/flay.rb', line 540

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.



565
566
567
568
# File 'lib/flay.rb', line 565

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.



498
499
500
# File 'lib/flay.rb', line 498

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