file_mutate 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format nested
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport", ">= 3.0.1"
4
+ gem "i18n"
5
+ gem "sugar-high", ">= 0.6.0"
6
+ gem "sweetloader", ">= 0.1.0"
7
+
8
+
9
+ group :development do
10
+ gem "shoulda", ">= 0"
11
+ gem "rspec", ">= 2.4.1"
12
+ gem "bundler", ">= 1.0.11"
13
+ gem "jeweler", ">= 1.6.4"
14
+ gem "rcov", ">= 0"
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,49 @@
1
+ h1. File Mutate
2
+
3
+ A File Muatation DSL and API that is easy to add to any module or class.
4
+ It is often convenient to extend the core File class with the file mutation APIs in order to gain a better DSL
5
+ for performing file mutations.
6
+
7
+ This gem has been extracted from the "sugar-high":https://github.com/kristianmandrup/sugar-high gem
8
+
9
+ h2. Content Mutation DSL
10
+
11
+ File mutation modules:
12
+
13
+ * Append content
14
+ * Insert content
15
+ * Overwrite content
16
+ * Remove content
17
+ * Replace content
18
+
19
+ * Delete files
20
+
21
+ The modules follow the naming convention: @FileMutate::AppendContent@
22
+
23
+ h2. Usage
24
+
25
+ To include all file mutation modules:
26
+
27
+ @File.file_mutate :all@
28
+
29
+ To include set of file mutation modules:
30
+
31
+ @File.file_mutate :append, :insert@
32
+
33
+ Note that the @file_mutate@ macro is made available for all modules.
34
+
35
+ h2. Contributing to file_mutate
36
+
37
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
38
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
39
+ * Fork the project
40
+ * Start a feature/bugfix branch
41
+ * Commit and push until you are happy with your contribution
42
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
43
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
44
+
45
+ h2. Copyright
46
+
47
+ Copyright (c) 2011 Kristian Mandrup. See LICENSE.txt for
48
+ further details.
49
+
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "file_mutate"
18
+ gem.homepage = "http://github.com/kristianmandrup/file_mutate"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Adds file mutation DSL to any module or extends File class}
21
+ gem.description = %Q{The file mutation DSL you always wish was included in Ruby}
22
+ gem.email = "kmandrup@gmail.com"
23
+ gem.authors = ["Kristian Mandrup"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,97 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{file_mutate}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Kristian Mandrup}]
12
+ s.date = %q{2011-08-24}
13
+ s.description = %q{The file mutation DSL you always wish was included in Ruby}
14
+ s.email = %q{kmandrup@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.textile",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "file_mutate.gemspec",
28
+ "lib/file_mutate.rb",
29
+ "lib/file_mutate/append_content.rb",
30
+ "lib/file_mutate/delete.rb",
31
+ "lib/file_mutate/insert_content.rb",
32
+ "lib/file_mutate/mutate.rb",
33
+ "lib/file_mutate/overwrite_content.rb",
34
+ "lib/file_mutate/remove_content.rb",
35
+ "lib/file_mutate/replace_content.rb",
36
+ "lib/file_mutation.rb",
37
+ "spec/file_mutate/append_content_spec.rb",
38
+ "spec/file_mutate/delete_spec.rb",
39
+ "spec/file_mutate/insert_before_last_spec.rb",
40
+ "spec/file_mutate/insert_content_spec.rb",
41
+ "spec/file_mutate/overwrite_content_spec.rb",
42
+ "spec/file_mutate/remove_content_spec.rb",
43
+ "spec/file_mutate/replace_content_spec.rb",
44
+ "spec/fixtures/application_file.rb",
45
+ "spec/fixtures/class_file.rb",
46
+ "spec/fixtures/class_file.txt",
47
+ "spec/fixtures/content_file.txt",
48
+ "spec/fixtures/empty.txt",
49
+ "spec/fixtures/file.txt",
50
+ "spec/fixtures/non-empty.txt",
51
+ "spec/fixtures/routes_file.rb",
52
+ "spec/fixtures/search_file.txt",
53
+ "spec/spec_helper.rb"
54
+ ]
55
+ s.homepage = %q{http://github.com/kristianmandrup/file_mutate}
56
+ s.licenses = [%q{MIT}]
57
+ s.require_paths = [%q{lib}]
58
+ s.rubygems_version = %q{1.8.8}
59
+ s.summary = %q{Adds file mutation DSL to any module or extends File class}
60
+
61
+ if s.respond_to? :specification_version then
62
+ s.specification_version = 3
63
+
64
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
65
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.1"])
66
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
67
+ s.add_runtime_dependency(%q<sugar-high>, [">= 0.6.0"])
68
+ s.add_runtime_dependency(%q<sweetloader>, [">= 0.1.0"])
69
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
70
+ s.add_development_dependency(%q<rspec>, [">= 2.4.1"])
71
+ s.add_development_dependency(%q<bundler>, [">= 1.0.11"])
72
+ s.add_development_dependency(%q<jeweler>, [">= 1.6.4"])
73
+ s.add_development_dependency(%q<rcov>, [">= 0"])
74
+ else
75
+ s.add_dependency(%q<activesupport>, [">= 3.0.1"])
76
+ s.add_dependency(%q<i18n>, [">= 0"])
77
+ s.add_dependency(%q<sugar-high>, [">= 0.6.0"])
78
+ s.add_dependency(%q<sweetloader>, [">= 0.1.0"])
79
+ s.add_dependency(%q<shoulda>, [">= 0"])
80
+ s.add_dependency(%q<rspec>, [">= 2.4.1"])
81
+ s.add_dependency(%q<bundler>, [">= 1.0.11"])
82
+ s.add_dependency(%q<jeweler>, [">= 1.6.4"])
83
+ s.add_dependency(%q<rcov>, [">= 0"])
84
+ end
85
+ else
86
+ s.add_dependency(%q<activesupport>, [">= 3.0.1"])
87
+ s.add_dependency(%q<i18n>, [">= 0"])
88
+ s.add_dependency(%q<sugar-high>, [">= 0.6.0"])
89
+ s.add_dependency(%q<sweetloader>, [">= 0.1.0"])
90
+ s.add_dependency(%q<shoulda>, [">= 0"])
91
+ s.add_dependency(%q<rspec>, [">= 2.4.1"])
92
+ s.add_dependency(%q<bundler>, [">= 1.0.11"])
93
+ s.add_dependency(%q<jeweler>, [">= 1.6.4"])
94
+ s.add_dependency(%q<rcov>, [">= 0"])
95
+ end
96
+ end
97
+
@@ -0,0 +1,2 @@
1
+ require 'file_mutation'
2
+
@@ -0,0 +1,16 @@
1
+ module FileMutate
2
+ module AppendContent
3
+ def append content=nil, &block
4
+ File.append self.path, content, &block
5
+ end
6
+
7
+ module ClassMethods
8
+ def append path, content=nil, &block
9
+ File.open(path, 'a') do |f|
10
+ f.puts content ||= yield
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,28 @@
1
+ require 'sugar-high/blank'
2
+ require 'sugar-high/arguments'
3
+ require 'sugar-high/path'
4
+ require 'sugar-high/regexp'
5
+ require 'sugar-high/string'
6
+ require 'sugar-high/file'
7
+
8
+ module FileMutate
9
+ module Delete
10
+ module ClassMethods
11
+ def delete! name
12
+ return nil if !File.exist?(name)
13
+ File.delete name
14
+ end
15
+ alias_method :delete_file!, :delete!
16
+ end
17
+
18
+ def delete!
19
+ File.delete(self.path)
20
+ end
21
+ alias_method :delete_file!, :delete!
22
+
23
+ def mutate marker, place, &block
24
+ File.mutate_file self.path, marker, place, &block
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,61 @@
1
+ module FileMutate
2
+ module InsertContent
3
+ def insert *args, &block
4
+ File.insert_into self.path, *args, &block
5
+ end
6
+
7
+ module ClassMethods
8
+ # insert_into 'my_file.txt', :after => 'Blip', :content => 'Hello
9
+ # insert_into 'my_file.txt', 'Hello', :after => 'Blip'
10
+ # insert_into 'my_file.txt', :after => 'Blip' do
11
+ # 'Hello'
12
+ # end
13
+ def insert_into file_name, *args, &block
14
+ options = last_option args
15
+ content = insertion_content options, *args, &block
16
+
17
+ # no content to insert?
18
+ return nil if content.blank?
19
+
20
+ file = begin
21
+ get_file(file_name)
22
+ rescue
23
+ return nil
24
+ end
25
+
26
+ # already inserted?
27
+ return nil if !options[:repeat] && file.has_content?(content)
28
+
29
+ # find where to insert
30
+ place, marker = if options[:before]
31
+ [ :before, options[:before] ]
32
+ elsif options[:before_last]
33
+ [ :before_last, options[:before_last] ]
34
+ else
35
+ [ :after, options[:after] ]
36
+ end
37
+
38
+ return nil if !file.has_content?(marker)
39
+
40
+ # do mutation
41
+ res = file.mutate marker, place do
42
+ content
43
+ end
44
+ res
45
+ end
46
+
47
+ def insertion_content options = {}, *args, &block
48
+ case args.first
49
+ when String
50
+ args.first
51
+ when Hash
52
+ options[:content] || (yield if block)
53
+ else
54
+ return yield if block
55
+ raise ArgumentError, "You must supply content to insert, either as a String before the options hash, a :content option or a block"
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,56 @@
1
+ module FileMutate
2
+ module Mutate
3
+ module ClassMethods
4
+ def mutate_file file, marker, place, &block
5
+ raise ArgumentError, "You must define a replacement marker for a :before, :before_last or :after key" if !marker
6
+
7
+ file = File.get_file(file)
8
+
9
+ if place == :before_last
10
+ content = file.read
11
+ content = content.insert_before_last yield, marker
12
+ file.overwrite content
13
+ return
14
+ end
15
+
16
+ marker_found = file.has_content? marker
17
+ return nil if !marker_found
18
+
19
+ replace_in_file file, /(#{marker})/mi do |match|
20
+ place == :after ? "#{match}\n #{yield}" : "#{yield}\n #{match}"
21
+ end
22
+ true
23
+ end
24
+
25
+ def replace_in_file(file, regexp, *args, &block)
26
+ file = File.get_file(file)
27
+ content = file.read.gsub(regexp, *args, &block)
28
+ file.overwrite content
29
+ end
30
+
31
+
32
+ def get_file file_name
33
+ case file_name
34
+ when PathString, String
35
+ File.new file_name
36
+ when File
37
+ file_name
38
+ else
39
+ raise ArgumentError, "Could not be converted to a File object: #{file_name}"
40
+ end
41
+ end
42
+
43
+ def get_filepath file
44
+ case file
45
+ when PathString, String
46
+ file
47
+ when File
48
+ file.path
49
+ else
50
+ raise ArgumentError, "Could not be converted to a file path: #{file_name}"
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,16 @@
1
+ module FileMutate
2
+ module OverwriteContent
3
+ def overwrite content=nil, &block
4
+ File.overwrite self.path, content, &block
5
+ end
6
+
7
+ module ClassMethods
8
+ def overwrite file, content=nil, &block
9
+ File.open(get_filepath(file).path, 'w') do |f|
10
+ f.puts content ||= yield
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,32 @@
1
+ module FileMutate
2
+ module RemoveContent
3
+ def remove_content options=nil, &block
4
+ opt_str = case options
5
+ when String
6
+ options
7
+ when Hash
8
+ content = options[:content] || options[:where]
9
+ raise ArgumentError, "Bad :content value in Hash" if !content || content.strip.empty?
10
+ content.strip
11
+ else
12
+ raise ArgumentError, "non-block argument must be either String or Hash with a :content option" if !block
13
+ end
14
+ content = block ? yield : opt_str
15
+
16
+ File.remove_content_from self.path, :content => content, :with => '', &block
17
+ end
18
+ alias_method :remove, :remove_content
19
+
20
+ module ClassMethods
21
+ def remove_from file_name, content=nil, &block
22
+ content ||= yield
23
+ replace_content_from file_name, :content => content, :with => '', &block
24
+ end
25
+
26
+ def remove_content_from file_name, options = {}, &block
27
+ replace_content_from file_name, options.merge(:with => ''), &block
28
+ end
29
+ end
30
+ end
31
+ end
32
+