collin-fold 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile.rb CHANGED
@@ -28,7 +28,7 @@ end
28
28
 
29
29
  namespace :gem do
30
30
  task :version do
31
- @version = "0.0.6"
31
+ @version = "0.0.9"
32
32
  end
33
33
 
34
34
  task :build => :spec do
data/lib/fold.rb CHANGED
@@ -6,6 +6,8 @@ require 'facets'
6
6
  module Fold
7
7
  require 'fold/engine'
8
8
  require 'fold/abstract_fold'
9
+ require 'fold/abstract_slice'
9
10
  require 'fold/fold_factory'
11
+ require 'fold/slice_factory'
10
12
  require 'fold/precompiler'
11
13
  end
@@ -9,8 +9,8 @@ module Fold
9
9
  :children => [],
10
10
  :tabs => -1
11
11
  }.merge(source)
12
-
13
- attrs[:text].gsub! self.class::Regex, ''
12
+
13
+ attrs[:text].gsub! self.class::Regex, '' if self.class.clear_match
14
14
 
15
15
  super attrs
16
16
  end
@@ -0,0 +1,6 @@
1
+ require 'ostruct'
2
+
3
+ module Fold
4
+ class AbstractSlice < OpenStruct
5
+ end
6
+ end
@@ -30,6 +30,11 @@ module Fold
30
30
 
31
31
  fold.metaclass.send :attr_accessor, var.gsub('@', '')
32
32
  end
33
+ that = self
34
+ fold.meta_def :method_missing do |meth, *args|
35
+ return that.send(meth, *args) if that.respond_to?(meth)
36
+ super
37
+ end
33
38
 
34
39
  fold
35
40
  end
@@ -46,11 +51,12 @@ module Fold
46
51
  end
47
52
 
48
53
  module ClassMethods
49
- def folds id, regex=AbstractFold::Regex, &block
54
+ def folds id, regex=AbstractFold::Regex, clear_match=true, &block
50
55
  fold= Class.new(AbstractFold)
51
56
  fold.const_set :Regex, regex
52
57
 
53
58
  fold.send :define_method, :render, &block if block_given?
59
+ fold.class.send :define_method, :clear_match do; clear_match end
54
60
 
55
61
  const_set id, fold
56
62
  defined_folds << fold
@@ -1,6 +1,7 @@
1
1
  module Fold
2
2
  class Precompiler
3
3
  include Fold::FoldFactory
4
+ include Fold::SliceFactory
4
5
 
5
6
  def fold lines
6
7
  last_line= produce
@@ -12,7 +13,7 @@ module Fold
12
13
  parent_stack= []
13
14
 
14
15
  lines.each do |text|
15
- line = produce text
16
+ line = produce process(text)
16
17
 
17
18
  indent = line.tabs - last_line.tabs
18
19
 
@@ -33,7 +34,9 @@ module Fold
33
34
  parent_line = parent_stack.last
34
35
  parent_line.children << line
35
36
  end
36
-
37
+
38
+ line.parent = parent_line unless parent_line.nil?
39
+
37
40
  last_line= line
38
41
  end
39
42
 
@@ -0,0 +1,65 @@
1
+ require 'ruby2ruby'
2
+
3
+ module Fold
4
+ module SliceFactory
5
+ def self.included klass
6
+ klass.extend ClassMethods
7
+ end
8
+
9
+ def process line=''
10
+ self.class.defined_slices.each do |slice|
11
+ line.gsub! slice::Regex do |match|
12
+ text = $1
13
+ eval(slice::Block.to_ruby).call
14
+ end
15
+ end
16
+ line
17
+ end
18
+
19
+ # def produce line=''
20
+ # # This all happens because I need to access Precompiler attributes
21
+ # # from within AbstractFold render methods
22
+ # # Pretty much needs a nice refactor, but don't have the time.
23
+ # # PS: thx ruby
24
+ # fold = if klass= detect_class(line)
25
+ # klass.new attrs
26
+ # else
27
+ # AbstractFold.new attrs.merge(:tabs => -1)
28
+ # end
29
+ #
30
+ # instance_variables.each do |var|
31
+ # fold.instance_variable_set var, instance_variable_get(var)
32
+ #
33
+ # fold.metaclass.send :attr_accessor, var.gsub('@', '')
34
+ # end
35
+ # that = self
36
+ # fold.meta_def :method_missing do |meth, *args|
37
+ # return that.send(meth, *args) if that.respond_to?(meth)
38
+ # super
39
+ # end
40
+ #
41
+ # fold
42
+ # end
43
+
44
+ def detect_slice_class line
45
+ return nil if line.blank?
46
+ self.class.defined_slices.reverse.detect {|slice| slice::Regex and slice::Regex.match(line)}
47
+ end
48
+
49
+ module ClassMethods
50
+ def slices id, regex=AbstractSlice::Regex, &block
51
+ slice= Class.new(AbstractSlice)
52
+
53
+ slice.const_set :Regex, regex
54
+ slice.const_set :Block, block
55
+
56
+ const_set id, slice
57
+ defined_slices << slice
58
+ end
59
+
60
+ def defined_slices
61
+ @slices||= []
62
+ end
63
+ end
64
+ end
65
+ end
@@ -12,6 +12,11 @@ describe Fold::AbstractFold, ".initialize" do
12
12
  Fold::Precompiler.folds :What, /what/
13
13
  Fold::Precompiler::What::Regex.should == /what/
14
14
  end
15
+
16
+ it "and specifies whether or not to rip the match from the source" do
17
+ Fold::Precompiler.folds :Whatever, /whatever/, false
18
+ Fold::Precompiler::Whatever.clear_match.should == false
19
+ end
15
20
  end
16
21
 
17
22
  describe Fold::AbstractFold, ".render" do
@@ -28,6 +33,12 @@ describe Fold::AbstractFold, ".render" do
28
33
  what= Fold::Precompiler::What.new(:text=>'whatever')
29
34
  what.render.should== "ever?"
30
35
  end
36
+
37
+ it "doesn't clear match text if clear_match is false" do
38
+ Fold::Precompiler.folds :Dirty, /dirty/, false
39
+ dirty = Fold::Precompiler::Dirty.new(:text=>"dirty bird")
40
+ dirty.render.should == "dirty bird"
41
+ end
31
42
 
32
43
  it "renders children" do
33
44
  Fold::Precompiler.folds :Parent, /^parent / do
@@ -0,0 +1,8 @@
1
+ require "ostruct"
2
+ require 'rspec/fold_spec_helper'
3
+
4
+ describe Fold::AbstractSlice do
5
+ it "inherits OpenStruct" do
6
+ Fold::AbstractSlice.superclass.should == OpenStruct
7
+ end
8
+ end
@@ -5,6 +5,10 @@ class Included
5
5
  def initialize
6
6
  @instance_variable = []
7
7
  end
8
+
9
+ def passed
10
+ :passed
11
+ end
8
12
  end
9
13
 
10
14
  describe Fold::FoldFactory do
@@ -47,13 +51,15 @@ describe Fold::FoldFactory, ".produce" do
47
51
  @it.produce.instance_variables.should include("@instance_variable")
48
52
  end
49
53
 
54
+ it "methods fall through" do
55
+ @it.produce.passed.should == :passed
56
+ end
57
+
50
58
  it "and sets up attribute accessors for them" do
51
59
  @it.produce.instance_variable.should === []
52
60
  end
53
61
  end
54
62
 
55
-
56
-
57
63
  #describe Fold::Precompiler::Action::Regex do
58
64
  # it "matches !LINE" do
59
65
  # Fold::Precompiler::Action::Regex.should match("!LINE")
@@ -9,6 +9,10 @@ end
9
9
  class Confused < Fold::Precompiler
10
10
  folds :Line, //
11
11
  folds :What, /what/
12
+
13
+ slices :Octothorp, /#([\w]+)/ do
14
+ "octothorpedo:#{text}"
15
+ end
12
16
  end
13
17
 
14
18
  describe Fold::Precompiler, ".folds" do
@@ -45,6 +49,22 @@ LINE 7
45
49
  }
46
50
  @it.fold(en.lines).children.length.should == 3
47
51
  end
52
+
53
+ it "children can be orphans" do
54
+ en = Fold::Engine.new "LINE 1"
55
+ @it.fold(en.lines).parent.should == nil
56
+ end
57
+
58
+ it "lets children know about their parents" do
59
+ en = Fold::Engine.new %{
60
+ Line 1
61
+ Line 2
62
+ Line 3
63
+ }
64
+
65
+ folded = @it.fold(en.lines)
66
+ folded.children.first.parent.should == folded
67
+ end
48
68
 
49
69
  it "barfs on innappropriate indentation" do
50
70
  en= Fold::Engine.new %{
@@ -0,0 +1,40 @@
1
+ require 'rspec/fold_spec_helper'
2
+
3
+ class Included
4
+ include Fold::FoldFactory
5
+ include Fold::SliceFactory
6
+
7
+ slices :Interpolation, /#\{(.*?)\}/ do
8
+ text
9
+ end
10
+
11
+ def initialize
12
+ @instance_variable = []
13
+ end
14
+
15
+ def passed
16
+ :passed
17
+ end
18
+ end
19
+
20
+ describe Fold::SliceFactory do
21
+ it "inherits module" do
22
+ Fold::SliceFactory.class.should == Module
23
+ end
24
+
25
+ before(:each) do
26
+ @it = Included.new
27
+ end
28
+
29
+ it "has list of included slices" do
30
+ Included.defined_slices.should include(Included::Interpolation)
31
+ end
32
+
33
+ it "generates constant" do
34
+ Included::Interpolation.should_not be_nil
35
+ end
36
+
37
+ it "replaces items in a string" do
38
+ @it.process('#{hey} whats #{up}').should == "hey whats up"
39
+ end
40
+ end
@@ -2,5 +2,6 @@ require 'rubygems'
2
2
  __DIR__ = File.dirname(__FILE__)
3
3
  $LOAD_PATH << "#{__DIR__}/.." unless $LOAD_PATH.include?("#{__DIR__}/..")
4
4
  FoldFixtureRoot= File.expand_path("#{__DIR__}/fixtures/fold")
5
+
5
6
  require "lib/fold"
6
7
  require 'pathname'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collin-fold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collin Miller
@@ -47,6 +47,8 @@ files:
47
47
  - lib/fold/abstract_fold.rb
48
48
  - lib/fold/fold_factory.rb
49
49
  - lib/fold/precompiler.rb
50
+ - lib/fold/slice_factory.rb
51
+ - lib/fold/abstract_slice.rb
50
52
  - rspec/fixtures
51
53
  - rspec/fixtures/fold
52
54
  - rspec/fixtures/fold/fixture.target.fold
@@ -58,6 +60,9 @@ files:
58
60
  - rspec/fold/fold_factory_spec.rb
59
61
  - rspec/fold/engine_spec.rb
60
62
  - rspec/fold/abstract_fold_spec.rb
63
+ - rspec/fold/slice_factory_spec.rb
64
+ - rspec/fold/abstract_slice_spec.rb
65
+ - rspec/fold/precompiler_spec.rb~
61
66
  has_rdoc: false
62
67
  homepage: http://github.com/collin/fold
63
68
  post_install_message: