sugar-high 0.2.1 → 0.2.2
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/VERSION +1 -1
- data/lib/sugar-high/alias.rb +25 -2
- data/lib/sugar-high/file.rb +3 -3
- data/spec/sugar-high/alias_spec.rb +71 -0
- data/spec/sugar-high/file/file_mutate_spec.rb +21 -1
- data/sugar-high.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/lib/sugar-high/alias.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
require 'sugar-high/methods'
|
2
2
|
require 'sugar-high/hash'
|
3
|
+
require 'sugar-high/arguments'
|
4
|
+
require 'sugar-high/array'
|
5
|
+
require 'active_support/inflector'
|
3
6
|
|
4
7
|
class Module
|
5
8
|
|
@@ -52,9 +55,29 @@ class Module
|
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
58
|
+
def alias_hash(hash)
|
59
|
+
pluralize = hash.delete(:pluralize)
|
60
|
+
singularize = hash.delete(:singularize)
|
61
|
+
# option = :pluralize => pluralize, :singularize => singularize
|
62
|
+
|
63
|
+
hash.each_pair do |original, alias_meth|
|
64
|
+
alias_for original, alias_meth
|
65
|
+
alias_for original.to_s.singularize, alias_meth.to_s.singularize, :singularize => true if singularize
|
66
|
+
alias_for original.to_s.pluralize, alias_meth.to_s.pluralize, :pluralize => true if pluralize
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
55
70
|
def alias_for(original, *aliases)
|
56
|
-
|
57
|
-
|
71
|
+
pluralize = last_option(aliases)[:pluralize]
|
72
|
+
singularize = last_option(aliases)[:singularize]
|
73
|
+
|
74
|
+
class_eval "alias_method :#{original.to_s.singularize}, :#{original}" if singularize
|
75
|
+
class_eval "alias_method :#{original.to_s.pluralize}, :#{original}" if pluralize
|
76
|
+
|
77
|
+
aliases.flatten.select_labels.each do |alias_meth|
|
78
|
+
class_eval "alias_method :#{alias_meth}, :#{original}"
|
79
|
+
class_eval "alias_method :#{alias_meth.to_s.pluralize}, :#{original}" if pluralize
|
80
|
+
class_eval "alias_method :#{alias_meth.to_s.singularize}, :#{original}" if singularize
|
58
81
|
end
|
59
82
|
end
|
60
83
|
alias_method :aliases_for, :alias_for
|
data/lib/sugar-high/file.rb
CHANGED
@@ -39,7 +39,7 @@ class File
|
|
39
39
|
# replaces content found at replacement_expr with content resulting from yielding block
|
40
40
|
# File.replace_content_from 'myfile.txt', where => /HelloWorld/, with => 'GoodBye'
|
41
41
|
def self.replace_content_from file_name, options = {}, &block
|
42
|
-
replacement_expr = options[:where]
|
42
|
+
replacement_expr = options[:where] || options[:content]
|
43
43
|
new_content = options[:with]
|
44
44
|
|
45
45
|
replacement_expr = case replacement_expr
|
@@ -48,7 +48,7 @@ class File
|
|
48
48
|
when String
|
49
49
|
/#{Regexp.escape(replacement_expr)}/
|
50
50
|
else
|
51
|
-
raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where option"
|
51
|
+
raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where or :content option"
|
52
52
|
end
|
53
53
|
|
54
54
|
# get existing file content
|
@@ -150,7 +150,7 @@ class File
|
|
150
150
|
when String
|
151
151
|
args.first
|
152
152
|
when Hash
|
153
|
-
options[:content]
|
153
|
+
options[:content] || yield if block
|
154
154
|
else
|
155
155
|
return yield if block
|
156
156
|
raise ArgumentError, "You must supply content to insert, either as a String before the options hash, a :content option or a block"
|
@@ -31,6 +31,54 @@ class Ged
|
|
31
31
|
multi_alias :_before_ => :kristian, :hejsa => :hello, :_direction_ => :reverse
|
32
32
|
end
|
33
33
|
|
34
|
+
class Plural
|
35
|
+
def monster
|
36
|
+
'monster'
|
37
|
+
end
|
38
|
+
|
39
|
+
alias_for :monster, :pluralize => true
|
40
|
+
end
|
41
|
+
|
42
|
+
class Plural2
|
43
|
+
def monster
|
44
|
+
'monster'
|
45
|
+
end
|
46
|
+
|
47
|
+
alias_for :monster, :beast, :pluralize => true
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
class Singular
|
52
|
+
def monsters
|
53
|
+
'monsters'
|
54
|
+
end
|
55
|
+
|
56
|
+
alias_for :monsters, :singularize => true
|
57
|
+
end
|
58
|
+
|
59
|
+
class Singular2
|
60
|
+
def monsters
|
61
|
+
'monsters'
|
62
|
+
end
|
63
|
+
|
64
|
+
alias_for :monsters, :beasts, :singularize => true
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
class AliasHash
|
69
|
+
def monsters
|
70
|
+
'monsters'
|
71
|
+
end
|
72
|
+
|
73
|
+
def monster
|
74
|
+
'monster'
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
alias_hash :monsters => :beasts, :singularize => true
|
79
|
+
end
|
80
|
+
|
81
|
+
|
34
82
|
class Wow
|
35
83
|
REGISTRATION_LINKS = {
|
36
84
|
:new_registration => :sign_up,
|
@@ -64,6 +112,29 @@ describe "SugarHigh" do
|
|
64
112
|
Ged.new.respond_to?(:kristian_hejsa).should be_true
|
65
113
|
end
|
66
114
|
|
115
|
+
it "should find singularized alias" do
|
116
|
+
Singular.new.respond_to?(:monster).should be_true
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should find singularized alias" do
|
120
|
+
Singular2.new.respond_to?(:beast).should be_true
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should find pluralized alias" do
|
124
|
+
Plural.new.respond_to?(:monsters).should be_true
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should find pluralized alias" do
|
128
|
+
Plural2.new.respond_to?(:beasts).should be_true
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should find nice aliases from using alias_hash" do
|
132
|
+
ah = AliasHash.new
|
133
|
+
ah.beasts.should == 'monsters'
|
134
|
+
ah.beast.should == 'monster'
|
135
|
+
end
|
136
|
+
|
137
|
+
|
67
138
|
it "should find alias methods!" do
|
68
139
|
w = Wow.new
|
69
140
|
w.respond_to?(:new_registration_link).should be_true
|
@@ -22,7 +22,7 @@ describe "SugarHigh::File" do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
describe '#remove_content_from' do
|
25
|
+
describe '#remove_content_from with :where option' do
|
26
26
|
let(:replace_file) { fixture_file 'file.txt' }
|
27
27
|
|
28
28
|
it "should remove content from existing file" do
|
@@ -34,6 +34,19 @@ describe "SugarHigh::File" do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
describe '#remove_content_from with :content option' do
|
38
|
+
let(:replace_file) { fixture_file 'file.txt' }
|
39
|
+
|
40
|
+
it "should remove content from existing file" do
|
41
|
+
File.overwrite(replace_file) do
|
42
|
+
'Hello You'
|
43
|
+
end
|
44
|
+
File.remove_content_from replace_file, :content => 'You'
|
45
|
+
File.read(replace_file).should_not match /You/
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
37
50
|
describe '#insert_into' do
|
38
51
|
let(:insertion_file) { fixture_file 'insertion.txt' }
|
39
52
|
|
@@ -63,6 +76,13 @@ describe "SugarHigh::File" do
|
|
63
76
|
File.read(insertion_file).should_not match /Hello Goodbye/
|
64
77
|
end
|
65
78
|
|
79
|
+
it "should insert Hello before Goodbye using a block as content to insert" do
|
80
|
+
File.insert_into insertion_file, :before => 'Goodbye' do
|
81
|
+
'Hello '
|
82
|
+
end
|
83
|
+
File.read(insertion_file).should_not match /Hello Goodbye/
|
84
|
+
end
|
85
|
+
|
66
86
|
it "should insert Hello after Goodbye using a :with option and a Regexp for the after expression" do
|
67
87
|
File.insert_into insertion_file, :content => ' Hello', :after => /Goodbye/
|
68
88
|
File.read(insertion_file).should_not match /Goodbye Hello/
|
data/sugar-high.gemspec
CHANGED