Module: DoNotation

Defined in:
lib/do_notation.rb

Defined Under Namespace

Classes: Rewriter

Class Method Summary collapse

Class Method Details

.pp(obj, indent = '') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/do_notation.rb', line 17

def self.pp(obj, indent='')
  return obj.inspect unless obj.is_a? Array
  return '()' if obj.empty?

  str = '(' + pp(obj.first, indent + ' ')

  if obj.length > 1
    str << ' '

    next_indent = indent + (' ' * str.length)

    str << obj[1..-1].map{ |o| pp(o, next_indent) }.join("\n#{next_indent}")
  end

  str << ')'

  str
end

.ruby_for(klass, block) ⇒ Object



8
9
10
# File 'lib/do_notation.rb', line 8

def self.ruby_for(klass, block)
  "#{klass.name}.instance_eval { #{ruby_for_block(block)} }"
end

.ruby_for_block(block) ⇒ Object



12
13
14
15
# File 'lib/do_notation.rb', line 12

def self.ruby_for_block(block)
  @cached_ruby ||= {}
  @cached_ruby[block.to_s] ||= Ruby2Ruby.new.process(DoNotation::Rewriter.new.process(block.to_sexp))
end

.run(klass, &block) ⇒ Object



4
5
6
# File 'lib/do_notation.rb', line 4

def self.run(klass, &block)
  eval(ruby_for(klass, block), block.binding).call
end