sugar-high 0.5.5 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +2 -29
- data/VERSION +1 -1
- data/lib/sugar-high/class_ext.rb +2 -133
- data/lib/sugar-high/file.rb +10 -11
- data/lib/sugar-high/file_ext.rb +13 -11
- data/lib/sugar-high/rails/concerns.rb +10 -4
- data/spec/sugar-high/file_spec.rb +26 -28
- data/spec/sugar-high/rails/concerns_spec.rb +31 -1
- data/spec/sugar-high/rails/fixture_user/scopes.rb +4 -0
- data/spec/sugar-high/rails/fixture_user/validations.rb +4 -0
- data/spec/sugar-high/rails/shared/associations.rb +1 -0
- data/spec/sugar-high/rails/shared/caching.rb +4 -0
- data/sugar-high.gemspec +6 -42
- metadata +19 -55
- data/lib/sugar-high/file_mutate.rb +0 -2
- data/lib/sugar-high/file_mutate/append_content.rb +0 -17
- data/lib/sugar-high/file_mutate/delete.rb +0 -29
- data/lib/sugar-high/file_mutate/insert_content.rb +0 -62
- data/lib/sugar-high/file_mutate/mutate.rb +0 -57
- data/lib/sugar-high/file_mutate/overwrite_content.rb +0 -17
- data/lib/sugar-high/file_mutate/remove_content.rb +0 -33
- data/lib/sugar-high/file_mutate/replace_content.rb +0 -45
- data/lib/sugar-high/file_mutation.rb +0 -58
- data/sandbox/test_routes_mutate.rb +0 -8
- data/spec/auto_load_blank_root.rb +0 -3
- data/spec/autoload_blank_root.rb +0 -3
- data/spec/autoload_blank_root/hello.rb +0 -6
- data/spec/autoload_blank_root/sailor.rb +0 -7
- data/spec/fixtures/application_file.rb +0 -6
- data/spec/fixtures/autoload_modules.rb +0 -3
- data/spec/fixtures/autoload_modules/subdir/first.rb +0 -7
- data/spec/fixtures/autoload_modules/subdir/second.rb +0 -5
- data/spec/fixtures/autoload_modules/subdir/third.rb +0 -4
- data/spec/fixtures/autoload_modules_root.rb +0 -3
- data/spec/fixtures/autoload_modules_root/first.rb +0 -7
- data/spec/fixtures/autoload_modules_root/second.rb +0 -4
- data/spec/fixtures/autoload_modules_root/third.rb +0 -7
- data/spec/fixtures/autoload_modulez.rb +0 -4
- data/spec/fixtures/autoload_modulez/first.rb +0 -7
- data/spec/fixtures/autoload_modulez/second.rb +0 -5
- data/spec/fixtures/autoload_modulez/third_one_here.rb +0 -4
- data/spec/fixtures/class_file.rb +0 -22
- data/spec/fixtures/class_file.txt +0 -0
- data/spec/fixtures/content_file.txt +0 -1
- data/spec/fixtures/routes_file.rb +0 -85
- data/spec/sugar-high/class_ext_spec.rb +0 -189
- data/spec/sugar-high/file/file_dsl_spec.rb +0 -4
- data/spec/sugar-high/file_mutate/append_content_spec.rb +0 -61
- data/spec/sugar-high/file_mutate/delete_spec.rb +0 -47
- data/spec/sugar-high/file_mutate/insert_before_last_spec.rb +0 -56
- data/spec/sugar-high/file_mutate/insert_content_spec.rb +0 -111
- data/spec/sugar-high/file_mutate/overwrite_content_spec.rb +0 -80
- data/spec/sugar-high/file_mutate/remove_content_spec.rb +0 -109
- data/spec/sugar-high/file_mutate/replace_content_spec.rb +0 -33
data/README.textile
CHANGED
@@ -10,10 +10,9 @@ h2. Usage
|
|
10
10
|
|
11
11
|
See specs for example use
|
12
12
|
|
13
|
-
h2. Update
|
13
|
+
h2. Update Aug 21, 2011
|
14
14
|
|
15
|
-
file_mutate
|
16
|
-
File object
|
15
|
+
Extracted *file_mutate* and *sweetloader* gems.
|
17
16
|
|
18
17
|
h2. Sugar packs
|
19
18
|
|
@@ -25,8 +24,6 @@ h2. Sugar packs
|
|
25
24
|
* dsl
|
26
25
|
* enumerable
|
27
26
|
* file
|
28
|
-
* file_mutate (backwards compatible)
|
29
|
-
* file_mutation
|
30
27
|
* includes
|
31
28
|
* kind_of
|
32
29
|
* math
|
@@ -127,30 +124,6 @@ h3. File
|
|
127
124
|
* self.overwrite : overwrite file with new content (mode = 'w')
|
128
125
|
* self.append : append to existing file with content or create new (mode = 'w+')
|
129
126
|
|
130
|
-
h3. File Mutate
|
131
|
-
Adds all File mutation modules to the File class
|
132
|
-
|
133
|
-
h3. File Mutation
|
134
|
-
|
135
|
-
Various File Mutation modules that can be added to any module or class for some nice benefits. Useful for generators fx.
|
136
|
-
|
137
|
-
Mutation modules within SugarHigh::FileMutate
|
138
|
-
|
139
|
-
* AppendContent
|
140
|
-
* Delete
|
141
|
-
* InsertContent
|
142
|
-
* OverwriteContent
|
143
|
-
* RemoveContent
|
144
|
-
* ReplaceContent
|
145
|
-
|
146
|
-
To add all mutate modules to the File class, simply:
|
147
|
-
|
148
|
-
<pre>File.mutate_ext :all</pre>
|
149
|
-
|
150
|
-
Otherwise, simply specify which ones:
|
151
|
-
|
152
|
-
<pre>File.mutate_ext :append_content, overwrite_content</pre>
|
153
|
-
|
154
127
|
h3. Hash
|
155
128
|
|
156
129
|
* hash_revert : Reverse keys and values
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/sugar-high/class_ext.rb
CHANGED
@@ -1,134 +1,3 @@
|
|
1
|
-
|
2
|
-
require 'sugar-high/array'
|
1
|
+
puts "Please use the new 'sweetloader' gem"
|
3
2
|
|
4
|
-
|
5
|
-
def include_and_extend(the_module, options={})
|
6
|
-
options[:instance_methods] ||= :InstanceMethods
|
7
|
-
options[:class_methods] ||= :ClassMethods
|
8
|
-
# Mainly include but be flexible
|
9
|
-
main_module = const_get(the_module.to_s.to_sym)
|
10
|
-
include main_module # for an extend_and_include method, change this to extend main_module
|
11
|
-
include main_module.const_get(options[:instance_methods]) if main_module.const_defined?(options[:instance_methods])
|
12
|
-
extend main_module.const_get(options[:class_methods]) if main_module.const_defined?(options[:class_methods])
|
13
|
-
end
|
14
|
-
|
15
|
-
def autoload_modules *args
|
16
|
-
|
17
|
-
options = args.extract_options!
|
18
|
-
root = options[:root] || AutoLoader.root || ''
|
19
|
-
path = root.strip.empty? ? self.name.to_s.underscore : [root, self.name.to_s.underscore].join('/')
|
20
|
-
from = options[:from] || path
|
21
|
-
|
22
|
-
# Here also could be adding of the file in top of load_paths like: $:.unshift File.dirname(__FILE__)
|
23
|
-
# It is very useful for situations of having not load_paths built Rails or Gems way.
|
24
|
-
args.each do |req_name|
|
25
|
-
ruby_file = req_name.to_s.underscore
|
26
|
-
|
27
|
-
send :autoload, req_name, AutoLoader.translate("#{from}/#{ruby_file}")
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
module AutoLoader
|
34
|
-
@@root = ''
|
35
|
-
@@namespaces = {}
|
36
|
-
|
37
|
-
def self.root
|
38
|
-
@@root
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.namespaces
|
42
|
-
@@namespaces
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.root= root
|
46
|
-
@@root = root
|
47
|
-
end
|
48
|
-
|
49
|
-
def self.namespaces= namespaces
|
50
|
-
@@namespaces = namespaces
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.translate name
|
54
|
-
names = name.split('/')
|
55
|
-
names.map do |name|
|
56
|
-
clazz_name = name.to_s.camelize
|
57
|
-
namespaces[clazz_name.to_sym] ? namespaces[clazz_name.to_sym] : name
|
58
|
-
end.join('/')
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
module ClassExt
|
63
|
-
def get_module name
|
64
|
-
# Module.const_get(name)
|
65
|
-
name.to_s.camelize.constantize
|
66
|
-
rescue
|
67
|
-
nil
|
68
|
-
end
|
69
|
-
|
70
|
-
def is_class?(clazz)
|
71
|
-
clazz.is_a?(Class) && (clazz.respond_to? :new)
|
72
|
-
end
|
73
|
-
|
74
|
-
def is_module?(clazz)
|
75
|
-
clazz.is_a?(Module) && !(clazz.respond_to? :new)
|
76
|
-
end
|
77
|
-
|
78
|
-
def class_exists?(name)
|
79
|
-
is_class? get_module(name)
|
80
|
-
rescue
|
81
|
-
return false
|
82
|
-
end
|
83
|
-
|
84
|
-
def module_exists?(name)
|
85
|
-
is_module? get_module(name)
|
86
|
-
rescue NameError
|
87
|
-
return false
|
88
|
-
end
|
89
|
-
|
90
|
-
def try_class name
|
91
|
-
return name if name.kind_of?(Class)
|
92
|
-
found = get_module(name) if name.kind_of_label?
|
93
|
-
return found if found.is_a?(Class)
|
94
|
-
rescue
|
95
|
-
false
|
96
|
-
end
|
97
|
-
|
98
|
-
def try_module name
|
99
|
-
return name if name.kind_of?(Module)
|
100
|
-
found = get_module(name) if name.kind_of_label?
|
101
|
-
return found if found.is_a?(Module)
|
102
|
-
rescue
|
103
|
-
false
|
104
|
-
end
|
105
|
-
|
106
|
-
def try_module_only name
|
107
|
-
return name if is_module?(name)
|
108
|
-
found = get_module(name) if name.kind_of_label?
|
109
|
-
return found if is_module?(found)
|
110
|
-
rescue
|
111
|
-
false
|
112
|
-
end
|
113
|
-
|
114
|
-
|
115
|
-
def find_first_class *names
|
116
|
-
classes = names.flatten.compact.uniq.inject([]) do |res, class_name|
|
117
|
-
found_class = try_class(class_name.to_s.camelize)
|
118
|
-
res << found_class if found_class
|
119
|
-
res
|
120
|
-
end
|
121
|
-
raise "Not one Class for any of: #{names} is currently loaded" if classes.empty?
|
122
|
-
classes.first
|
123
|
-
end
|
124
|
-
|
125
|
-
def find_first_module *names
|
126
|
-
modules = names.flatten.compact.uniq.inject([]) do |res, class_name|
|
127
|
-
found_class = try_module(class_name.to_s.camelize)
|
128
|
-
res << found_class if found_class
|
129
|
-
res
|
130
|
-
end
|
131
|
-
raise "Not one Module for any of: #{names} is currently loaded" if modules.empty?
|
132
|
-
modules.first
|
133
|
-
end
|
134
|
-
end
|
3
|
+
require 'sweetloader'
|
data/lib/sugar-high/file.rb
CHANGED
@@ -3,33 +3,32 @@ require 'sugar-high/arguments'
|
|
3
3
|
require 'sugar-high/path'
|
4
4
|
require 'sugar-high/regexp'
|
5
5
|
require 'sugar-high/string'
|
6
|
-
require 'sugar-high/file_mutate'
|
7
6
|
require 'sugar-high/file_ext'
|
8
7
|
|
9
|
-
class File
|
8
|
+
class File
|
10
9
|
include SugarHigh::FileExt
|
11
|
-
extend SugarHigh::FileExt::ClassMethods
|
10
|
+
extend SugarHigh::FileExt::ClassMethods
|
12
11
|
end
|
13
12
|
|
14
13
|
class Symbol
|
15
14
|
def as_filename
|
16
15
|
self.to_s.underscore
|
17
16
|
end
|
18
|
-
|
17
|
+
|
19
18
|
def valid_file_command?
|
20
19
|
[:read, :remove, :delete].include? self
|
21
20
|
end
|
22
|
-
|
21
|
+
|
23
22
|
def file
|
24
23
|
as_filename.file
|
25
|
-
end
|
26
|
-
end
|
24
|
+
end
|
25
|
+
end
|
27
26
|
|
28
27
|
class NilClass
|
29
28
|
def valid_file_command?
|
30
29
|
false
|
31
30
|
end
|
32
|
-
end
|
31
|
+
end
|
33
32
|
|
34
33
|
class Array
|
35
34
|
def file_names ext = '*'
|
@@ -39,7 +38,7 @@ end
|
|
39
38
|
|
40
39
|
class String
|
41
40
|
def as_filename
|
42
|
-
self.underscore
|
41
|
+
self.underscore
|
43
42
|
end
|
44
43
|
|
45
44
|
def valid_file_command?
|
@@ -57,10 +56,10 @@ class String
|
|
57
56
|
end
|
58
57
|
|
59
58
|
def new_file
|
60
|
-
begin
|
59
|
+
begin
|
61
60
|
file
|
62
61
|
rescue
|
63
|
-
File.open(self, 'w')
|
62
|
+
File.open(self, 'w')
|
64
63
|
end
|
65
64
|
end
|
66
65
|
end
|
data/lib/sugar-high/file_ext.rb
CHANGED
@@ -6,17 +6,17 @@ module SugarHigh
|
|
6
6
|
raise ArgumentError, "There is no file at: #{file_name}" if !File.file?(file_name)
|
7
7
|
File.zero?(file_name)
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def has_content? file_name, content_matcher, &block
|
11
11
|
file = get_file file_name
|
12
|
-
file.has_content? content_matcher, &block
|
13
|
-
end
|
14
|
-
|
12
|
+
file.has_content? content_matcher, &block
|
13
|
+
end
|
14
|
+
|
15
15
|
def read_from file_name, options = {}, &block
|
16
16
|
raise ArgumentError, "File to read from not found or not a file: #{file_name}" if !File.file? file_name
|
17
17
|
content = File.read file_name
|
18
18
|
|
19
|
-
if options[:before]
|
19
|
+
if options[:before]
|
20
20
|
begin
|
21
21
|
regexp = options[:before].to_regexp
|
22
22
|
index = content.match(regexp).offset_before
|
@@ -26,27 +26,29 @@ module SugarHigh
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
if options[:after]
|
29
|
+
if options[:after]
|
30
30
|
begin
|
31
31
|
regexp = options[:after].to_regexp
|
32
32
|
index = content.match(regexp).offset_after
|
33
|
-
content = content[index..-1]
|
33
|
+
content = content[index..-1]
|
34
34
|
rescue
|
35
35
|
raise ArgumentError, ":after option must be a string or regular expression, was : #{options[:after]}"
|
36
|
-
end
|
36
|
+
end
|
37
37
|
end
|
38
38
|
yield content if block
|
39
39
|
content
|
40
40
|
end
|
41
41
|
alias_method :read_content_from, :read_from
|
42
|
-
alias_method :with_content_from, :read_from
|
42
|
+
alias_method :with_content_from, :read_from
|
43
43
|
end
|
44
44
|
|
45
|
+
# instance methods
|
46
|
+
|
45
47
|
def blank?
|
46
48
|
File.zero?(self.path)
|
47
|
-
end
|
49
|
+
end
|
48
50
|
|
49
|
-
def has_content? content_matcher = nil, &block
|
51
|
+
def has_content? content_matcher = nil, &block
|
50
52
|
content_matcher ||= yield
|
51
53
|
begin
|
52
54
|
content_matcher = content_matcher.to_regexp
|
@@ -1,23 +1,29 @@
|
|
1
|
-
class
|
1
|
+
class Module
|
2
2
|
def concerned_with(*concerns)
|
3
3
|
concerns.each do |concern|
|
4
|
-
|
4
|
+
require_method "#{name.underscore}/#{concern}"
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
8
|
def shared_concerns(*concerns)
|
9
9
|
concerns.each do |concern|
|
10
|
-
|
10
|
+
require_method "shared/#{concern}"
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
def include_shared_concerns(*concerns)
|
15
15
|
concerns.each do |concern|
|
16
|
-
|
16
|
+
require_method "shared/#{concern}"
|
17
17
|
self.send :include, concern.to_s.camelize.constantize
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
alias_method :shared_concern, :shared_concerns
|
22
22
|
alias_method :include_shared_concern, :include_shared_concerns
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def require_method path
|
27
|
+
defined?(require_dependency) ? require_dependency(path) : require(path)
|
28
|
+
end
|
23
29
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'sugar-high/file'
|
3
|
-
require 'sugar-high/file_mutate'
|
4
3
|
|
5
4
|
describe "SugarHigh" do
|
6
5
|
describe "File" do
|
@@ -23,7 +22,7 @@ describe "SugarHigh" do
|
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
|
-
describe '#blank?' do
|
25
|
+
describe '#blank?' do
|
27
26
|
it "should return true for an empty file" do
|
28
27
|
File.new(empty_file).blank?.should be_true
|
29
28
|
end
|
@@ -37,7 +36,7 @@ describe "SugarHigh" do
|
|
37
36
|
it "should find content in file using String argument" do
|
38
37
|
File.overwrite(search_file) do
|
39
38
|
'Find this line right here!'
|
40
|
-
end
|
39
|
+
end
|
41
40
|
File.has_content?(search_file, 'line right').should be_true
|
42
41
|
File.has_content?(search_file, 'line left').should be_false
|
43
42
|
end
|
@@ -45,18 +44,18 @@ describe "SugarHigh" do
|
|
45
44
|
it "should find content in file using Regexp argument" do
|
46
45
|
File.overwrite(search_file) do
|
47
46
|
'Find this line right here!'
|
48
|
-
end
|
47
|
+
end
|
49
48
|
File.has_content?(search_file, /line right/).should be_true
|
50
49
|
File.has_content?(search_file, /line left/).should be_false
|
51
50
|
end
|
52
51
|
end
|
53
|
-
|
52
|
+
|
54
53
|
describe '#read_from' do
|
55
54
|
|
56
55
|
it "should read all the content into a block" do
|
57
56
|
File.read_from non_empty_file do |content|
|
58
57
|
content.should match /blip/
|
59
|
-
content.should match /blup/
|
58
|
+
content.should match /blup/
|
60
59
|
end
|
61
60
|
end
|
62
61
|
|
@@ -75,17 +74,17 @@ describe "SugarHigh" do
|
|
75
74
|
it "should read all the content before a given marker into a block" do
|
76
75
|
File.read_from non_empty_file, :before => 'blap' do |content|
|
77
76
|
content.should match /blip/
|
78
|
-
content.should_not match /blap/
|
77
|
+
content.should_not match /blap/
|
79
78
|
end
|
80
79
|
end
|
81
80
|
|
82
81
|
it "should read all the content after a given marker into a block" do
|
83
82
|
File.read_from non_empty_file, :after => 'blap' do |content|
|
84
83
|
content.should match /blup/
|
85
|
-
content.should_not match /blap/
|
84
|
+
content.should_not match /blap/
|
86
85
|
end
|
87
86
|
end
|
88
|
-
end
|
87
|
+
end
|
89
88
|
|
90
89
|
describe '#read_content (instance)' do
|
91
90
|
let(:non_empty_file) { fixture_file 'non-empty.txt' }
|
@@ -93,56 +92,56 @@ describe "SugarHigh" do
|
|
93
92
|
it "should read all the content into a block" do
|
94
93
|
File.new(non_empty_file).read_content do |content|
|
95
94
|
content.should match /blip/
|
96
|
-
content.should match /blup/
|
95
|
+
content.should match /blup/
|
97
96
|
end
|
98
97
|
end
|
99
|
-
end
|
100
|
-
|
98
|
+
end
|
99
|
+
|
101
100
|
describe '#read_from (class)' do
|
102
101
|
let(:non_empty_file) { fixture_file 'non-empty.txt' }
|
103
102
|
|
104
103
|
it "should read all the content into a block" do
|
105
104
|
File.read_from(non_empty_file) do |content|
|
106
105
|
content.should match /blip/
|
107
|
-
content.should match /blup/
|
106
|
+
content.should match /blup/
|
108
107
|
end
|
109
108
|
end
|
110
|
-
end
|
109
|
+
end
|
111
110
|
end
|
112
|
-
|
111
|
+
|
113
112
|
describe "Array" do
|
114
|
-
describe '#file_names' do
|
113
|
+
describe '#file_names' do
|
115
114
|
let(:replace_file) { fixture_file 'file.txt' }
|
116
|
-
|
115
|
+
|
117
116
|
before :each do
|
118
117
|
File.delete replace_file if File.file?(replace_file)
|
119
|
-
end
|
120
|
-
|
118
|
+
end
|
119
|
+
|
121
120
|
it "should return all file names of an array of paths to files" do
|
122
121
|
expr = fixtures_dir + '/*.txt'
|
123
122
|
Dir.glob(expr).file_names('txt').should include('empty', 'non-empty')
|
124
123
|
end
|
125
124
|
end
|
126
|
-
end
|
127
|
-
|
125
|
+
end
|
126
|
+
|
128
127
|
describe "String" do
|
129
|
-
describe '#new_file' do
|
128
|
+
describe '#new_file' do
|
130
129
|
let(:class_file) { fixture_file 'class_file.txt' }
|
131
|
-
|
130
|
+
|
132
131
|
it "should get the existing file" do
|
133
132
|
class_file.new_file.path.should =~ /class_file/
|
134
133
|
end
|
135
134
|
end
|
136
|
-
|
137
|
-
describe '#file' do
|
135
|
+
|
136
|
+
describe '#file' do
|
138
137
|
let(:class_file) { fixture_file 'class_file.txt' }
|
139
|
-
|
138
|
+
|
140
139
|
it "should get the file" do
|
141
140
|
class_file.file.path.should =~ /class_file/
|
142
141
|
end
|
143
142
|
end
|
144
143
|
|
145
|
-
describe '#dir' do
|
144
|
+
describe '#dir' do
|
146
145
|
it "should get the dir" do
|
147
146
|
fix_path = fixtures_dir.path
|
148
147
|
# puts fix_path
|
@@ -152,4 +151,3 @@ describe "SugarHigh" do
|
|
152
151
|
end
|
153
152
|
end
|
154
153
|
end
|
155
|
-
|