alter 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74c5ce71c54e56376a3271fbf31306893b91503e
4
+ data.tar.gz: 14ed8d1a9eccd98159d536c92b715c72ba6d9f51
5
+ SHA512:
6
+ metadata.gz: 5a39d7fe9a0063914edc0838bfbf2b8b4955cb18b294d93ec852629daa11ed9bd800eb345aaf8a77c5be13ab77d57a2f23b18afd6881e1ad5e765cada95c653f
7
+ data.tar.gz: c899e9a8e8058e440cf6f326e39e568848cb2c9d03016d14773a8408c2b05d625e2f77e7b74f2c0e167a423aa1b6387a9c89531c7e58d10b559f420985b60548
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Alter
2
2
 
3
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/baldwindavid/alter)
4
+
3
5
  Passing something like a blog post through many complex filters (Markdown, Liquid, regex, Nokogiri, etc) can get ugly and difficult to test and debug. Alter enforces structure and consistency by moving each filter to easy-to-write processor classes. It also keeps a handy history of all "alterations". The source is a mere 50 lines of code, so it should be easy to read and extend.
4
6
 
5
7
  ## Installation
@@ -29,28 +31,28 @@ class KumbayaProcessor < Alter::Processor
29
31
  end
30
32
  end
31
33
  </code></pre>
32
-
34
+
33
35
  You will already have access to the `input` attribute. You are simply delivering the `output` based upon the `input`. To use the processor, you will first create a new Alter item which will setup that initial `input` value.
34
36
 
35
37
  <pre lang="ruby"><code>
36
38
  text = Alter::Item.new "Your language sucks"
37
39
  </code></pre>
38
-
40
+
39
41
  Now you can run that item through the processor by passing the `KumbayaProcessor` class to the process method. The process method also accepts an array of processors.
40
42
 
41
43
  <pre lang="ruby"><code>
42
44
  text.process KumbayaProcessor
43
- text.value
45
+ text.output
44
46
  # result: "Your language is great"
45
47
  </code></pre>
46
48
 
47
49
  Calling process returns the altered item. Items have the following attributes:
48
50
 
49
- - `value` - the current value of the item
51
+ - `output` - the current output of the item
50
52
  - `input` - the original input of the item
51
53
  - `options` - the original options passed to the item
52
54
  - `history` - a history of every item alteration
53
-
55
+
54
56
  You will also have access to any `options` passed to the processor. Here is a class making use of `options`.
55
57
 
56
58
  <pre lang="ruby"><code>
@@ -66,20 +68,20 @@ end
66
68
 
67
69
  text = Alter::Item.new "Your language sucks", :age => 37
68
70
  text.process [KumbayaProcessor, EligibilityProcessor]
69
- text.value
71
+ text.output
70
72
  # result: "Your language is great and you could run for President"
71
73
  </code></pre>
72
-
74
+
73
75
  You can just as easily chain or separate these process calls. Options can also be passed to the process method if you only want them available to specific processors.
74
76
 
75
77
  <pre lang="ruby"><code>
76
78
  text = Alter::Item.new "Your language sucks"
77
79
  text.process KumbayaProcessor
78
80
  text.process EligibilityProcessor, :age => 33
79
- text.value
81
+ text.output
80
82
  # result: "Your language is great but you're too young to be President"
81
83
  </code></pre>
82
-
84
+
83
85
  ### History
84
86
 
85
87
  Alter keeps a history of every "alteration" made to the original input and stores it in the `history` array.
@@ -101,7 +103,7 @@ result:
101
103
  :options=>{:age=>33},
102
104
  :meta=>nil}]
103
105
  </code></pre>
104
-
106
+
105
107
  ### Metadata
106
108
 
107
109
  Extra metadata can be written to the history by providing a `meta` method in the processor class.
@@ -109,7 +111,7 @@ Extra metadata can be written to the history by providing a `meta` method in the
109
111
  <pre lang="ruby"><code>
110
112
  class UselessProcessor &#60; Alter::Processor
111
113
  def meta
112
- {
114
+ {
113
115
  :random => "This is so #{rand(1000)}",
114
116
  :data => "This is so meta"
115
117
  }
@@ -1,11 +1,13 @@
1
1
  require "alter/version"
2
2
 
3
3
  module Alter
4
-
4
+
5
5
  class Item
6
6
  attr_accessor :value, :history, :options
7
7
  attr_reader :input
8
-
8
+
9
+ alias_method :output, :value
10
+
9
11
  def initialize(input, options = {})
10
12
  @input = input
11
13
  @value = input
@@ -15,23 +17,23 @@ module Alter
15
17
 
16
18
  def process(processors = [], mergeable_options = {})
17
19
  merged_options = options.merge(mergeable_options)
18
-
20
+
19
21
  [processors].flatten.each do |processor|
20
22
  run_processor(processor.new(value, merged_options))
21
23
  end
22
-
24
+
23
25
  self
24
26
  end
25
-
27
+
26
28
  def run_processor(processor)
27
29
  self.value = processor.output
28
30
  self.history << Alter::Alteration.new(:processor => processor.class, :input => processor.input, :output => processor.output, :options => processor.options, :meta => processor.meta)
29
31
  end
30
32
  end
31
-
33
+
32
34
  class Alteration
33
35
  attr_accessor :processor, :input, :output, :options, :meta
34
-
36
+
35
37
  def initialize(attrs = {})
36
38
  attrs.each { |k, v| self.send("#{k}=", v) }
37
39
  end
@@ -45,7 +47,7 @@ module Alter
45
47
  @input = input
46
48
  @options = options
47
49
  end
48
-
50
+
49
51
  def output
50
52
  input
51
53
  end
@@ -1,3 +1,3 @@
1
1
  module Alter
2
- VERSION = "0.0.2"
3
- end
2
+ VERSION = "0.0.3"
3
+ end
@@ -1,14 +1,14 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Item" do
4
-
4
+
5
5
  before :each do
6
-
6
+
7
7
  class TextProcessor < Alter::Processor
8
8
  def output
9
9
  input + " + output"
10
10
  end
11
-
11
+
12
12
  def meta
13
13
  {
14
14
  :color => "blue",
@@ -16,19 +16,19 @@ describe "Item" do
16
16
  }
17
17
  end
18
18
  end
19
-
19
+
20
20
  class FirstProcessor < Alter::Processor
21
21
  def output
22
22
  input + " + first"
23
23
  end
24
24
  end
25
-
25
+
26
26
  class SecondProcessor < Alter::Processor
27
27
  def output
28
28
  input + " + second"
29
29
  end
30
30
  end
31
-
31
+
32
32
  class EligibilityProcessor < Alter::Processor
33
33
  def output
34
34
  if options[:age] >= 35
@@ -38,7 +38,7 @@ describe "Item" do
38
38
  end
39
39
  end
40
40
  end
41
-
41
+
42
42
  end
43
43
 
44
44
  it "should update the value based upon a custom output method in a processor" do
@@ -47,49 +47,55 @@ describe "Item" do
47
47
  @item.value.should == "Initial text + output"
48
48
  end
49
49
 
50
+ it "should allow the value to be called as output" do
51
+ @item = Alter::Item.new "Initial text"
52
+ @item.process TextProcessor
53
+ @item.output.should == "Initial text + output"
54
+ end
55
+
50
56
  it "should allow an array of processors to be passed via the process method" do
51
57
  @item = Alter::Item.new "Initial text"
52
58
  @item.process [FirstProcessor, SecondProcessor]
53
59
  @item.value.should == "Initial text + first + second"
54
60
  end
55
-
61
+
56
62
  it "should allow processors to be chained" do
57
63
  @item = Alter::Item.new "Initial text"
58
64
  @item.process FirstProcessor
59
65
  @item.value.should == "Initial text + first"
60
66
  @item.process SecondProcessor
61
67
  @item.value.should == "Initial text + first + second"
62
- end
63
-
68
+ end
69
+
64
70
  it "should write history to the item" do
65
71
  @item = Alter::Item.new "Initial text"
66
72
  @item.process [FirstProcessor, SecondProcessor]
67
73
  @item.history.size.should == 2
68
74
  end
69
-
75
+
70
76
  it "should attach meta data to the history if provided" do
71
77
  @item = Alter::Item.new "Initial text"
72
78
  @item.process TextProcessor
73
79
  @item.history.first.meta[:color].should == "blue"
74
- end
75
-
80
+ end
81
+
76
82
  it "should write a static alteration record to the history" do
77
83
  @item = Alter::Item.new "Initial text"
78
84
  @item.process TextProcessor
79
85
  first_request = @item.history.first.meta[:rand]
80
86
  @item.history.first.meta[:rand] == first_request
81
87
  end
82
-
88
+
83
89
  it "should allow passing options via the item" do
84
90
  @item = Alter::Item.new "Initial text", :age => 36
85
91
  @item.process EligibilityProcessor
86
92
  @item.value.should == "Initial text + President eligible"
87
93
  end
88
-
94
+
89
95
  it "should allow passing options via the process method" do
90
96
  @item = Alter::Item.new "Initial text"
91
97
  @item.process EligibilityProcessor, :age => 32
92
98
  @item.value.should == "Initial text + Too young to be President"
93
99
  end
94
-
95
- end
100
+
101
+ end
metadata CHANGED
@@ -1,27 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - David Baldwin
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-03-23 00:00:00.000000000Z
11
+ date: 2016-09-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
- requirement: &70221407731100 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *70221407731100
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  description: Enforce structure by moving content filters to easy-to-write processor
26
28
  classes
27
29
  email:
@@ -30,7 +32,7 @@ executables: []
30
32
  extensions: []
31
33
  extra_rdoc_files: []
32
34
  files:
33
- - .gitignore
35
+ - ".gitignore"
34
36
  - Gemfile
35
37
  - LICENSE
36
38
  - README.md
@@ -43,27 +45,26 @@ files:
43
45
  - spec/spec_helper.rb
44
46
  homepage: ''
45
47
  licenses: []
48
+ metadata: {}
46
49
  post_install_message:
47
50
  rdoc_options: []
48
51
  require_paths:
49
52
  - lib
50
53
  required_ruby_version: !ruby/object:Gem::Requirement
51
- none: false
52
54
  requirements:
53
- - - ! '>='
55
+ - - ">="
54
56
  - !ruby/object:Gem::Version
55
57
  version: '0'
56
58
  required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
59
  requirements:
59
- - - ! '>='
60
+ - - ">="
60
61
  - !ruby/object:Gem::Version
61
62
  version: '0'
62
63
  requirements: []
63
64
  rubyforge_project:
64
- rubygems_version: 1.8.10
65
+ rubygems_version: 2.4.8
65
66
  signing_key:
66
- specification_version: 3
67
+ specification_version: 4
67
68
  summary: Alter enforces structure by moving content filters to easy-to-write processor
68
69
  classes
69
70
  test_files: