processr 0.5.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.9.0
data/examples/all.rb CHANGED
@@ -1,22 +1,31 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'processr'))
1
+ require 'rubygems'
2
+ require 'processr'
3
+
4
+ # Setup
2
5
 
3
6
  Processr.configure do |config|
4
7
  config.root = File.expand_path(File.dirname(__FILE__))
5
- config.out = File.join(config.root, 'output.txt')
8
+ config.out = nil
6
9
  end
7
10
 
8
11
 
9
-
10
- # Simple text concatenation.
12
+ puts "Simple text concatenation:"
11
13
 
12
14
  processor = Processr.new
13
- processor << 'one.txt'
14
- processor << 'two.txt'
15
- processor.process!
15
+ processor << "Some Text\n"
16
+ processor << "Some More Text"
17
+ puts processor.process!
18
+
16
19
 
20
+ puts "Simple text concatenation form files:"
21
+
22
+ processor = Processr.new
23
+ processor.files << File.join('..', 'spec', 'fixtures', 'one.txt')
24
+ processor.files << File.join('..', 'spec', 'fixtures', 'two.txt')
25
+ puts processor.process!
17
26
 
18
27
 
19
- # Simple textile filter.
28
+ puts "Simple textile filter using input:"
20
29
 
21
30
  TextileFilter = lambda do |buffer|
22
31
 
@@ -35,7 +44,5 @@ end
35
44
 
36
45
  processor = Processr.new
37
46
  processor.add_filter(TextileFilter)
38
- processor << 'text.textile'
39
- processor.process!
40
-
41
- puts "* Look in output.txt to see the result of this example."
47
+ processor << 'A _simple_ example of a "textile":http://www.textism.com/tools/textile/ parser using a *filter*.'
48
+ puts processor.process!
data/lib/processr.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  class Processr
4
+
4
5
  class << self
5
6
  def root=(path); @root = path; end
6
7
  def root; @root; end
@@ -13,10 +14,11 @@ class Processr
13
14
  end
14
15
  end
15
16
 
16
- attr_accessor :buffer, :filters
17
+ attr_accessor :buffer, :files, :filters
17
18
 
18
19
  def initialize
19
20
  self.buffer = ""
21
+ self.files = []
20
22
  self.filters = []
21
23
  end
22
24
 
@@ -25,23 +27,41 @@ class Processr
25
27
  end
26
28
 
27
29
  def process!
30
+ read_files
31
+ process_filters
32
+ write_output
33
+ end
34
+
35
+ def <<(text)
36
+ self.buffer << text
37
+ end
38
+
39
+ private
40
+ def read_files
41
+ self.files.each do |file|
42
+ read_file_to_buffer(file)
43
+ end
44
+ end
45
+
46
+ def read_file_to_buffer(file)
47
+ self.buffer << File.open(File.join(self.class.root, file)).read
48
+ end
49
+
50
+ def process_filters
28
51
  self.filters.each do |filter|
29
52
  self.buffer = filter.call(self.buffer)
30
53
  end
31
-
32
- write_buffer!
33
-
34
- true
35
54
  end
36
55
 
37
- def <<(file)
38
- self.buffer << File.open(File.join(self.class.root, file)).read
56
+ def write_output
57
+ self.class.out.nil? ? self.buffer : write_buffer_file
39
58
  end
40
59
 
41
- private
42
- def write_buffer!
60
+ def write_buffer_file
43
61
  output_file = File.open(self.class.out, "w+")
44
62
  output_file.puts self.buffer
45
63
  output_file.close
64
+
65
+ true
46
66
  end
47
67
  end
data/processr.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{processr}
8
- s.version = "0.5.0"
8
+ s.version = "0.9.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Josh Nesbitt"]
@@ -23,15 +23,12 @@ Gem::Specification.new do |s|
23
23
  "TODO",
24
24
  "VERSION",
25
25
  "examples/all.rb",
26
- "examples/one.txt",
27
- "examples/output.txt",
28
- "examples/text.textile",
29
- "examples/two.txt",
30
26
  "lib/processr.rb",
31
27
  "processr.gemspec",
32
28
  "readme.rdoc",
33
- "spec/fixtures/one.js",
34
- "spec/fixtures/two.js",
29
+ "spec/fixtures/one.txt",
30
+ "spec/fixtures/output.txt",
31
+ "spec/fixtures/two.txt",
35
32
  "spec/lib/processr_spec.rb",
36
33
  "spec/spec_helper.rb",
37
34
  "spec/watch.rb"
data/readme.rdoc CHANGED
@@ -9,7 +9,9 @@
9
9
 
10
10
  == Overview
11
11
 
12
- Processr is a simple text processing and concatenation library. It takes a number of input files and outputs a single file with the result. Text can be passed through filters to modify the output.
12
+ Processr is a simple text processing and concatenation library. It takes a number of input strings (or files) and outputs a single string (or file) containing the result. Text can be passed through filters to modify the output.
13
+
14
+
13
15
 
14
16
  == Installation
15
17
 
@@ -17,8 +19,11 @@ The project is hosted on rubygems.org. Getting it is simple:
17
19
 
18
20
  gem install processr
19
21
 
22
+
23
+
20
24
  == Usage
21
25
 
26
+
22
27
  === Configuration
23
28
 
24
29
  Use the configuration block to setup Processr
@@ -28,21 +33,37 @@ Use the configuration block to setup Processr
28
33
  config.out = File.join(config.root, 'output.txt')
29
34
  end
30
35
 
36
+ If an output file is specified the result will be written to that file, otherwise the result will be returned directly from the #process! method.
37
+
38
+
31
39
  === Basic
32
40
 
33
41
  processor = Processr.new
34
- processor << 'input_one.txt'
35
- processor << 'input_two.txt'
36
- processor.process!
42
+ processor << "Some\n"
43
+ processor << "Text"
44
+ processor.process! # => "Some\nText"
45
+
46
+ This will result in a concatenated string being returned.
37
47
 
38
- This will result in a single file being output with the contents of all files inputted.
39
48
 
40
- === filters
49
+ === Basic (from file)
50
+
51
+ processor = Processr.new
52
+ processor.files << 'input_one.txt'
53
+ processor.files << 'input_two.txt'
54
+ processor.process! # => contents of input_one.txt and input_two.txt
55
+
56
+ This will result in the contents of input_one.txt and input_two.txt returned.
57
+
58
+
59
+ === Filters
41
60
 
42
61
  Filters can be used to modify the output of a processing session. A filter is any object that responds to #call. Filters take a single argument (the input buffer) and must return the modified buffer for further processing. For example:
43
62
 
44
63
  lambda do |buffer|
45
64
  # ...do something with buffer here...
65
+
66
+ buffer
46
67
  end
47
68
 
48
69
  Or
@@ -51,6 +72,8 @@ Or
51
72
 
52
73
  def self.call(buffer)
53
74
  # ...do something with buffer here...
75
+
76
+ buffer
54
77
  end
55
78
 
56
79
  end
@@ -74,8 +97,8 @@ You can register a filter by calling #add_filter on an instance of Processr. A f
74
97
 
75
98
  processor = Processr.new
76
99
  processor.add_filter(TextileFilter)
77
- processor << 'text.textile'
78
- processor.process!
100
+ processor << 'A _simple_ example of a "textile":http://www.textism.com/tools/textile/ parser using a *filter*.'
101
+ processor.process! # => "A <em>simple</em> example of a <a href="http://www.textism.com/tools/textile/">textile</a> parser using a <strong>filter</strong>."
79
102
 
80
103
  Run the examples for more information.
81
104
 
File without changes
File without changes
File without changes
@@ -1,10 +1,11 @@
1
1
  describe Processr do
2
2
 
3
- before :all do
3
+ before :each do
4
4
  Processr.configure do |config|
5
5
  config.root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
6
- config.out = File.join(config.root, 'examples', 'all')
7
6
  end
7
+
8
+ @processor = Processr.new
8
9
  end
9
10
 
10
11
  it "should successfully accept a configuration block" do
@@ -20,7 +21,7 @@ describe Processr do
20
21
  Processr.out.should be_nil
21
22
 
22
23
  root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
23
- out = File.join(root, 'examples', 'all')
24
+ out = File.join(root, '..', 'examples', 'all')
24
25
 
25
26
  Processr.configure do |config|
26
27
  config.root = root
@@ -31,4 +32,50 @@ describe Processr do
31
32
  Processr.out.should == out
32
33
  end
33
34
 
35
+ it "should allow text to be added to the buffer" do
36
+ @processor.buffer.should == ""
37
+ @processor << "Some Text"
38
+ @processor.buffer.should == "Some Text"
39
+ end
40
+
41
+ it "should read files to the buffer if no output file is provided" do
42
+ Processr.out = nil
43
+ @processor.buffer.should == ""
44
+ @processor.files << File.join('fixtures', 'one.txt')
45
+ @processor.files << File.join('fixtures', 'two.txt')
46
+ @processor.process!.should == "one\ntwo\n"
47
+ end
48
+
49
+ it "should write files from the buffer if an output file is given" do
50
+ Processr.out = File.join(Processr.root, 'fixtures', 'output.txt')
51
+
52
+ @processor.buffer.should == ""
53
+ @processor.files << File.join('fixtures', 'one.txt')
54
+ @processor.process!.should be_true
55
+
56
+ File.exists?(File.join(Processr.root, 'fixtures', 'output.txt')).should == true
57
+ File.open(File.join(Processr.root, 'fixtures', 'output.txt')).read.should == "one\n"
58
+ end
59
+
60
+ it "should allow the use of filters to modify output" do
61
+ Processr.out = nil
62
+
63
+ LanguageFilter = lambda do |buffer|
64
+ lookup = [
65
+ "fucking",
66
+ "hate"
67
+ ]
68
+
69
+ lookup.each do |word|
70
+ buffer.gsub!(word, ("*" * word.size))
71
+ end
72
+
73
+ buffer
74
+ end
75
+
76
+ @processor.add_filter(LanguageFilter)
77
+ @processor << "I fucking hate bad language."
78
+ @processor.process!.should == "I ******* **** bad language."
79
+ end
80
+
34
81
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 5
7
+ - 9
8
8
  - 0
9
- version: 0.5.0
9
+ version: 0.9.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Josh Nesbitt
@@ -47,15 +47,12 @@ files:
47
47
  - TODO
48
48
  - VERSION
49
49
  - examples/all.rb
50
- - examples/one.txt
51
- - examples/output.txt
52
- - examples/text.textile
53
- - examples/two.txt
54
50
  - lib/processr.rb
55
51
  - processr.gemspec
56
52
  - readme.rdoc
57
- - spec/fixtures/one.js
58
- - spec/fixtures/two.js
53
+ - spec/fixtures/one.txt
54
+ - spec/fixtures/output.txt
55
+ - spec/fixtures/two.txt
59
56
  - spec/lib/processr_spec.rb
60
57
  - spec/spec_helper.rb
61
58
  - spec/watch.rb
data/examples/output.txt DELETED
@@ -1 +0,0 @@
1
- A <em>simple</em> example of a <a href="http://www.textism.com/tools/textile/">textile</a> parser using a <strong>filter</strong>.
@@ -1 +0,0 @@
1
- A _simple_ example of a "textile":http://www.textism.com/tools/textile/ parser using a *filter*.
data/spec/fixtures/two.js DELETED
@@ -1 +0,0 @@
1
- two