Class: Pipewrench

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pipewrench.rb,
lib/pipewrench/options.rb,
lib/pipewrench/version.rb

Defined Under Namespace

Classes: Options

Constant Summary collapse

VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin, options = Options.new) ⇒ Pipewrench

Returns a new instance of Pipewrench.



8
9
10
11
12
13
14
15
16
# File 'lib/pipewrench.rb', line 8

def initialize(stdin, options=Options.new)
  @stdin = stdin
  @options = options

  if options.rails
    require 'active_support'
    require 'active_support/core_ext'
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/pipewrench.rb', line 5

def options
  @options
end

Instance Method Details

#each(&blk) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/pipewrench.rb', line 34

def each(&blk)
  if options.strip
    @stdin.each_line.map(&:rstrip).map(&blk)
  else
    @stdin.each_line(&blk)
  end
end

#evaluate(cmd) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/pipewrench.rb', line 26

def evaluate(cmd)
  if options.map
    map { |line| line.instance_eval(cmd) }
  else
    to_a.instance_eval(cmd)
  end
end

#run(cmd) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/pipewrench.rb', line 18

def run(cmd)
  if options.compact
    evaluate(cmd).compact
  else
    evaluate(cmd)
  end
end