sugar-high 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -7,8 +7,8 @@ begin
7
7
  gem.email = "kmandrup@gmail.com"
8
8
  gem.homepage = "http://github.com/kristianmandrup/sugar-high"
9
9
  gem.authors = ["Kristian Mandrup"]
10
- gem.add_development_dependency "rspec", ">= 2.0.0.beta.19"
11
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
10
+ gem.add_development_dependency "rspec", "~> 2.0.0"
11
+ gem.add_dependency "require_all", "~> 1.1.0"
12
12
  end
13
13
  Jeweler::GemcutterTasks.new
14
14
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.7
@@ -52,6 +52,13 @@ class Module
52
52
  end
53
53
  end
54
54
 
55
+ def alias_for(original, *aliases)
56
+ aliases.each do |alias_meth|
57
+ class_eval "alias_method :#{alias_meth}, :#{original} if respond_to? :#{original}"
58
+ end
59
+ end
60
+ alias_method :aliases_for, :alias_for
61
+
55
62
  protected
56
63
 
57
64
  def make_name name, alias_name, config_options
@@ -1,6 +1,17 @@
1
1
  require 'sugar-high/blank'
2
+ require 'sugar-high/arguments'
2
3
 
3
- class File
4
+ class File
5
+ def self.delete! name
6
+ return nil if !File.exist?(name)
7
+ File.delete name
8
+ end
9
+
10
+ def self.delete_file! name
11
+ return nil if !File.file?(name)
12
+ File.delete name
13
+ end
14
+
4
15
  def self.blank? file_name
5
16
  raise ArgumentError, "Filename argument must not be blank" if file_name.blank?
6
17
  raise ArgumentError, "There is no file at: #{file_name}" if !File.file?(file_name)
@@ -57,10 +68,81 @@ class File
57
68
  true # signal success!
58
69
  end
59
70
 
60
- # TODO: Needs spec
71
+
61
72
  def self.remove_content_from file_name, options = {}, &block
62
73
  replace_content_from file_name, options.merge(:with => ''), &block
63
- end
74
+ end
75
+
76
+ # TODO: Needs spec
77
+
78
+ # insert_into 'my_file.txt', :after => 'Blip', :content => 'Hello
79
+ # insert_into 'my_file.txt', :after => 'Blip', 'Hello
80
+ # insert_into 'my_file.txt', :after => 'Blip' do
81
+ # 'Hello'
82
+ # end
83
+
84
+ def self.insert_into file_name, *args, &block
85
+ options = last_option args
86
+ content = Insert.content options, *args, &block
87
+
88
+ file = File.new(file_name)
89
+ return nil if !File.exist?(file)
90
+
91
+ # already inserted?
92
+ return nil if content.blank? || (file.read =~ /#{content}/)
93
+
94
+ place, marker = if options[:before]
95
+ [ :before, options[:before] ]
96
+ else
97
+ [ :after, options[:after] ]
98
+ end
99
+
100
+ marker = Insert.get_marker marker
101
+
102
+ return nil if !(File.new(file.path).read =~ /#{marker}/)
103
+
104
+ Mutate.mutate_file file.path, marker, place do
105
+ content
106
+ end
107
+ end
108
+
109
+ module Insert
110
+ def self.get_marker marker
111
+ marker = case marker
112
+ when String
113
+ Regexp.escape(marker)
114
+ when Regexp
115
+ marker.source
116
+ end
117
+ end
118
+
119
+ def self.content options = {}, *args, &block
120
+ case args.first
121
+ when String
122
+ args.first
123
+ when Hash
124
+ options[:content]
125
+ else
126
+ return yield if block
127
+ raise ArgumentError, "You must supply content to insert, either as a String before the options hash, a :content option or a block"
128
+ end
129
+ end
130
+ end
131
+
132
+ module Mutate
133
+ def self.mutate_file file, marker, place, &block
134
+ raise ArgumentError, "You must define a replacement marker for a :before or :after key" if !marker
135
+
136
+ replace_in_file file, /(#{marker})/mi do |match|
137
+ place == :after ? "#{match}\n #{yield}" : "#{yield}\n #{match}"
138
+ end
139
+ end
140
+
141
+ def self.replace_in_file(path, regexp, *args, &block)
142
+ content = File.read(path).gsub(regexp, *args, &block)
143
+ File.open(path, 'wb') { |file| file.write(content) }
144
+ end
145
+ end
64
146
  end
65
147
 
66
148
  class Array
File without changes
data/lib/sugar-high.rb CHANGED
@@ -0,0 +1,2 @@
1
+ require 'require_all'
2
+ require File.dirname(__FILE__) + '/sugar-high'
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'sugar-high/file'
3
+
4
+ describe "SugarHigh::File" do
5
+ let(:empty_file) { fixture_file 'empty.txt' }
6
+ let(:non_empty_file) { fixture_file 'non-empty.txt'}
7
+ let(:replace_file) { fixture_file 'file.txt' }
8
+
9
+ before :each do
10
+ File.delete replace_file if File.file?(replace_file)
11
+ end
12
+
13
+ describe '#replace_content_from' do
14
+ let(:replace_file) { fixture_file 'file.txt' }
15
+
16
+ it "should remove content from existing file" do
17
+ File.overwrite(replace_file) do
18
+ 'Hello You'
19
+ end
20
+ File.replace_content_from replace_file, :where => 'You', :with => 'Me'
21
+ File.read(replace_file).should_not match /You/
22
+ end
23
+ end
24
+
25
+ describe '#remove_content_from' do
26
+ let(:replace_file) { fixture_file 'file.txt' }
27
+
28
+ it "should remove content from existing file" do
29
+ File.overwrite(replace_file) do
30
+ 'Hello You'
31
+ end
32
+ File.remove_content_from replace_file, :where => 'You'
33
+ File.read(replace_file).should_not match /You/
34
+ end
35
+ end
36
+
37
+ describe '#insert_into' do
38
+ let(:insertion_file) { fixture_file 'insertion.txt' }
39
+
40
+ before :each do
41
+ File.overwrite(insertion_file) do
42
+ 'Goodbye'
43
+ end
44
+ end
45
+
46
+ after :each do
47
+ File.delete insertion_file if File.file?(insertion_file)
48
+ end
49
+
50
+ it "should insert Hello before Goodbye using a block as content" do
51
+ File.insert_into insertion_file, :before => 'Goodbye' do
52
+ 'Hello'
53
+ end
54
+ end
55
+
56
+ it "should insert Hello before Goodbye using a content string arg" do
57
+ File.insert_into insertion_file, "Hello ", :before => 'Goodbye'
58
+ File.read(insertion_file).should_not match /Hello Goodbye/
59
+ end
60
+
61
+ it "should insert Hello before Goodbye using a :content option" do
62
+ File.insert_into insertion_file, :content => 'Hello ', :before => 'Goodbye'
63
+ File.read(insertion_file).should_not match /Hello Goodbye/
64
+ end
65
+
66
+ it "should insert Hello after Goodbye using a :with option and a Regexp for the after expression" do
67
+ File.insert_into insertion_file, :content => ' Hello', :after => /Goodbye/
68
+ File.read(insertion_file).should_not match /Goodbye Hello/
69
+ end
70
+ end
71
+ end
@@ -32,30 +32,6 @@ describe "SugarHigh" do
32
32
  end
33
33
  end
34
34
 
35
- describe '#replace_content_from' do
36
- let(:replace_file) { fixture_file 'file.txt' }
37
-
38
- it "should remove content from existing file" do
39
- File.overwrite(replace_file) do
40
- 'Hello You'
41
- end
42
- File.replace_content_from replace_file, :where => 'You', :with => 'Me'
43
- File.read(replace_file).should_not match /You/
44
- end
45
- end
46
-
47
- describe '#remove_content_from' do
48
- let(:replace_file) { fixture_file 'file.txt' }
49
-
50
- it "should remove content from existing file" do
51
- File.overwrite(replace_file) do
52
- 'Hello You'
53
- end
54
- File.remove_content_from replace_file, :where => 'You'
55
- File.read(replace_file).should_not match /You/
56
- end
57
- end
58
-
59
35
  describe '#file_names' do
60
36
  let(:replace_file) { fixture_file 'file.txt' }
61
37
 
data/sugar-high.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sugar-high}
8
- s.version = "0.1.6"
8
+ s.version = "0.1.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2010-08-28}
12
+ s.date = %q{2010-08-29}
13
13
  s.description = %q{More Ruby sugar - inspired by the 'zuker' project}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -33,12 +33,12 @@ Gem::Specification.new do |s|
33
33
  "lib/sugar-high/hash.rb",
34
34
  "lib/sugar-high/includes.rb",
35
35
  "lib/sugar-high/kind_of.rb",
36
- "lib/sugar-high/matchers/have_aliases.rb",
37
36
  "lib/sugar-high/metaclass.rb",
38
37
  "lib/sugar-high/methods.rb",
39
38
  "lib/sugar-high/module.rb",
40
39
  "lib/sugar-high/not.rb",
41
- "lib/sugar-high/rspec.rb",
40
+ "lib/sugar-high/rspec/configure.rb",
41
+ "lib/sugar-high/rspec/matchers/have_aliases.rb",
42
42
  "spec/fixtures/empty.txt",
43
43
  "spec/fixtures/non-empty.txt",
44
44
  "spec/spec_helper.rb",
@@ -46,7 +46,8 @@ Gem::Specification.new do |s|
46
46
  "spec/sugar-high/arguments_spec.rb",
47
47
  "spec/sugar-high/array_spec.rb",
48
48
  "spec/sugar-high/blank_spec.rb",
49
- "spec/sugar-high/file_spec.rb",
49
+ "spec/sugar-high/file/file_mutate_spec.rb",
50
+ "spec/sugar-high/file/file_spec.rb",
50
51
  "spec/sugar-high/includes_spec.rb",
51
52
  "spec/sugar-high/kind_of_spec.rb",
52
53
  "spec/sugar-high/methods_spec.rb",
@@ -64,7 +65,8 @@ Gem::Specification.new do |s|
64
65
  "spec/sugar-high/arguments_spec.rb",
65
66
  "spec/sugar-high/array_spec.rb",
66
67
  "spec/sugar-high/blank_spec.rb",
67
- "spec/sugar-high/file_spec.rb",
68
+ "spec/sugar-high/file/file_mutate_spec.rb",
69
+ "spec/sugar-high/file/file_spec.rb",
68
70
  "spec/sugar-high/includes_spec.rb",
69
71
  "spec/sugar-high/kind_of_spec.rb",
70
72
  "spec/sugar-high/methods_spec.rb",
@@ -76,12 +78,15 @@ Gem::Specification.new do |s|
76
78
  s.specification_version = 3
77
79
 
78
80
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
79
- s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
81
+ s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
82
+ s.add_runtime_dependency(%q<require_all>, ["~> 1.1.0"])
80
83
  else
81
- s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
84
+ s.add_dependency(%q<rspec>, ["~> 2.0.0"])
85
+ s.add_dependency(%q<require_all>, ["~> 1.1.0"])
82
86
  end
83
87
  else
84
- s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
88
+ s.add_dependency(%q<rspec>, ["~> 2.0.0"])
89
+ s.add_dependency(%q<require_all>, ["~> 1.1.0"])
85
90
  end
86
91
  end
87
92
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 6
9
- version: 0.1.6
8
+ - 7
9
+ version: 0.1.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-28 00:00:00 +02:00
17
+ date: 2010-08-29 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -23,17 +23,30 @@ dependencies:
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">="
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 2
30
30
  - 0
31
31
  - 0
32
- - beta
33
- - 19
34
- version: 2.0.0.beta.19
32
+ version: 2.0.0
35
33
  type: :development
36
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: require_all
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 1
46
+ - 0
47
+ version: 1.1.0
48
+ type: :runtime
49
+ version_requirements: *id002
37
50
  description: More Ruby sugar - inspired by the 'zuker' project
38
51
  email: kmandrup@gmail.com
39
52
  executables: []
@@ -60,12 +73,12 @@ files:
60
73
  - lib/sugar-high/hash.rb
61
74
  - lib/sugar-high/includes.rb
62
75
  - lib/sugar-high/kind_of.rb
63
- - lib/sugar-high/matchers/have_aliases.rb
64
76
  - lib/sugar-high/metaclass.rb
65
77
  - lib/sugar-high/methods.rb
66
78
  - lib/sugar-high/module.rb
67
79
  - lib/sugar-high/not.rb
68
- - lib/sugar-high/rspec.rb
80
+ - lib/sugar-high/rspec/configure.rb
81
+ - lib/sugar-high/rspec/matchers/have_aliases.rb
69
82
  - spec/fixtures/empty.txt
70
83
  - spec/fixtures/non-empty.txt
71
84
  - spec/spec_helper.rb
@@ -73,7 +86,8 @@ files:
73
86
  - spec/sugar-high/arguments_spec.rb
74
87
  - spec/sugar-high/array_spec.rb
75
88
  - spec/sugar-high/blank_spec.rb
76
- - spec/sugar-high/file_spec.rb
89
+ - spec/sugar-high/file/file_mutate_spec.rb
90
+ - spec/sugar-high/file/file_spec.rb
77
91
  - spec/sugar-high/includes_spec.rb
78
92
  - spec/sugar-high/kind_of_spec.rb
79
93
  - spec/sugar-high/methods_spec.rb
@@ -117,7 +131,8 @@ test_files:
117
131
  - spec/sugar-high/arguments_spec.rb
118
132
  - spec/sugar-high/array_spec.rb
119
133
  - spec/sugar-high/blank_spec.rb
120
- - spec/sugar-high/file_spec.rb
134
+ - spec/sugar-high/file/file_mutate_spec.rb
135
+ - spec/sugar-high/file/file_spec.rb
121
136
  - spec/sugar-high/includes_spec.rb
122
137
  - spec/sugar-high/kind_of_spec.rb
123
138
  - spec/sugar-high/methods_spec.rb