sugar-high 0.1.5 → 0.1.6
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/README.markdown +3 -1
- data/VERSION +1 -1
- data/lib/sugar-high/file.rb +58 -1
- data/lib/sugar-high/module.rb +8 -0
- data/lib/sugar-high/not.rb +17 -0
- data/spec/sugar-high/file_spec.rb +46 -4
- data/sugar-high.gemspec +3 -2
- metadata +4 -3
data/README.markdown
CHANGED
@@ -36,7 +36,9 @@ See specs for example use
|
|
36
36
|
|
37
37
|
### File
|
38
38
|
|
39
|
-
* self.blank? and blank? : Is file empty?
|
39
|
+
* self.blank? and blank? : Is file empty?
|
40
|
+
* self.overwrite : overwrite file with new content (mode = 'w')
|
41
|
+
* self.append : append to existing file with content or create new (mode = 'w+')
|
40
42
|
|
41
43
|
String:
|
42
44
|
* path : expand String with path operations :up and :down
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
data/lib/sugar-high/file.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'sugar-high/
|
1
|
+
require 'sugar-high/blank'
|
2
2
|
|
3
3
|
class File
|
4
4
|
def self.blank? file_name
|
@@ -9,9 +9,66 @@ class File
|
|
9
9
|
|
10
10
|
def blank?
|
11
11
|
File.zero?(self.path)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.overwrite path, content=nil, &block
|
15
|
+
File.open(path, 'w') do |f|
|
16
|
+
f.puts content ||= yield
|
17
|
+
end
|
12
18
|
end
|
19
|
+
|
20
|
+
def self.append path, content=nil, &block
|
21
|
+
File.open(path, 'w+') do |f|
|
22
|
+
f.puts content ||= yield
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# replaces content found at replacement_expr with content resulting from yielding block
|
27
|
+
# File.replace_content_from 'myfile.txt', where => /HelloWorld/, with => 'GoodBye'
|
28
|
+
def self.replace_content_from file_name, options = {}, &block
|
29
|
+
replacement_expr = options[:where]
|
30
|
+
new_content = options[:with]
|
31
|
+
|
32
|
+
replacement_expr = case replacement_expr
|
33
|
+
when Regexp
|
34
|
+
replacement_expr
|
35
|
+
when String
|
36
|
+
/#{Regexp.escape(replacement_expr)}/
|
37
|
+
else
|
38
|
+
raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where option"
|
39
|
+
end
|
40
|
+
|
41
|
+
# get existing file content
|
42
|
+
content = File.read file_name
|
43
|
+
|
44
|
+
# return nil if no mathing replacement found
|
45
|
+
return nil if !(content =~ replacement_expr)
|
46
|
+
|
47
|
+
new_content ||= yield if block
|
48
|
+
|
49
|
+
raise ArgumentError, "Content to be replaced with must be specified as a :with option or as a block" if !new_content
|
50
|
+
|
51
|
+
# remove content that matches expr, by replacing with empty
|
52
|
+
mutated_content = content.gsub replacement_expr, new_content
|
53
|
+
|
54
|
+
# write mutated content as new file
|
55
|
+
File.overwrite file_name, mutated_content
|
56
|
+
|
57
|
+
true # signal success!
|
58
|
+
end
|
59
|
+
|
60
|
+
# TODO: Needs spec
|
61
|
+
def self.remove_content_from file_name, options = {}, &block
|
62
|
+
replace_content_from file_name, options.merge(:with => ''), &block
|
63
|
+
end
|
13
64
|
end
|
14
65
|
|
66
|
+
class Array
|
67
|
+
def file_names ext = '*'
|
68
|
+
self.map{|a| a.gsub( /(.*)\//, '').gsub(/\.#{Regexp.escape(ext)}/, '')}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
15
72
|
class String
|
16
73
|
def path
|
17
74
|
self.extend PathString
|
data/lib/sugar-high/module.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
require 'active_support/inflector'
|
2
2
|
|
3
|
+
class Module
|
4
|
+
def last_name
|
5
|
+
# name.gsub /^(.*)::/, ''
|
6
|
+
name.demodulize
|
7
|
+
end
|
8
|
+
alias_method :demodulize, :last_name
|
9
|
+
end
|
10
|
+
|
3
11
|
def modules *module_names, &block
|
4
12
|
module_names.flatten.each do |name|
|
5
13
|
class_eval %{
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Object
|
2
|
+
define_method :not do
|
3
|
+
Not.new(self)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Not
|
7
|
+
private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }
|
8
|
+
|
9
|
+
def initialize(subject)
|
10
|
+
@subject = subject
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(sym, *args, &blk)
|
14
|
+
!@subject.send(sym, *args, &blk)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -3,8 +3,13 @@ require 'sugar-high/file'
|
|
3
3
|
|
4
4
|
describe "SugarHigh" do
|
5
5
|
describe "File" do
|
6
|
-
let(:empty_file)
|
7
|
-
let(:
|
6
|
+
let(:empty_file) { fixture_file 'empty.txt' }
|
7
|
+
let(:non_empty_file) { fixture_file 'non-empty.txt'}
|
8
|
+
let(:replace_file) { fixture_file 'file.txt' }
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
File.delete replace_file if File.file?(replace_file)
|
12
|
+
end
|
8
13
|
|
9
14
|
describe '#self.blank' do
|
10
15
|
it "should return true for an empty file" do
|
@@ -12,7 +17,7 @@ describe "SugarHigh" do
|
|
12
17
|
end
|
13
18
|
|
14
19
|
it "should return false for a NON-empty file" do
|
15
|
-
File.blank?(
|
20
|
+
File.blank?(non_empty_file).should_not be_true
|
16
21
|
end
|
17
22
|
end
|
18
23
|
|
@@ -22,8 +27,45 @@ describe "SugarHigh" do
|
|
22
27
|
end
|
23
28
|
|
24
29
|
it "should return false for a NON-empty file" do
|
25
|
-
File.new(
|
30
|
+
File.new(non_empty_file).blank?.should_not be_true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
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'
|
26
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
|
+
describe '#file_names' do
|
60
|
+
let(:replace_file) { fixture_file 'file.txt' }
|
61
|
+
|
62
|
+
before :each do
|
63
|
+
File.delete replace_file if File.file?(replace_file)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should return all file names of an array of paths to files" do
|
67
|
+
expr = fixtures_dir + '/*.txt'
|
68
|
+
Dir.glob(expr).file_names('txt').should == ['empty', 'non-empty']
|
27
69
|
end
|
28
70
|
end
|
29
71
|
|
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.
|
8
|
+
s.version = "0.1.6"
|
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-
|
12
|
+
s.date = %q{2010-08-28}
|
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 = [
|
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
|
|
37
37
|
"lib/sugar-high/metaclass.rb",
|
38
38
|
"lib/sugar-high/methods.rb",
|
39
39
|
"lib/sugar-high/module.rb",
|
40
|
+
"lib/sugar-high/not.rb",
|
40
41
|
"lib/sugar-high/rspec.rb",
|
41
42
|
"spec/fixtures/empty.txt",
|
42
43
|
"spec/fixtures/non-empty.txt",
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 6
|
9
|
+
version: 0.1.6
|
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-
|
17
|
+
date: 2010-08-28 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- lib/sugar-high/metaclass.rb
|
65
65
|
- lib/sugar-high/methods.rb
|
66
66
|
- lib/sugar-high/module.rb
|
67
|
+
- lib/sugar-high/not.rb
|
67
68
|
- lib/sugar-high/rspec.rb
|
68
69
|
- spec/fixtures/empty.txt
|
69
70
|
- spec/fixtures/non-empty.txt
|