sugar-high 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sugar-high/array.rb +3 -1
- data/lib/sugar-high/file.rb +28 -0
- data/lib/sugar-high/kind_of.rb +19 -0
- data/lib/sugar-high/regexp.rb +10 -0
- data/spec/fixtures/non-empty.txt +3 -1
- data/spec/sugar-high/array_spec.rb +1 -1
- data/spec/sugar-high/file/file_spec.rb +37 -0
- data/spec/sugar-high/kind_of_spec.rb +18 -0
- data/sugar-high.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/sugar-high/array.rb
CHANGED
data/lib/sugar-high/file.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'sugar-high/blank'
|
2
2
|
require 'sugar-high/arguments'
|
3
3
|
require 'sugar-high/path'
|
4
|
+
require 'sugar-high/regexp'
|
4
5
|
|
5
6
|
class File
|
6
7
|
def self.delete! name
|
@@ -70,6 +71,33 @@ class File
|
|
70
71
|
end
|
71
72
|
|
72
73
|
|
74
|
+
def self.read_from file_name, options = {}, &block
|
75
|
+
raise ArgumentError, "File to read from not found or not a file: #{file_name}" if !File.file? file_name
|
76
|
+
content = File.read file_name
|
77
|
+
|
78
|
+
if options[:before]
|
79
|
+
begin
|
80
|
+
regexp = options[:before].to_regexp
|
81
|
+
index = content.match(regexp).offset_before
|
82
|
+
content = content[0..index]
|
83
|
+
rescue
|
84
|
+
raise ArgumentError, ":before option must be a string or regular expression, was : #{options[:before]}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
if options[:after]
|
89
|
+
begin
|
90
|
+
regexp = options[:after].to_regexp
|
91
|
+
index = content.match(regexp).offset_after
|
92
|
+
content = content[index..-1]
|
93
|
+
rescue
|
94
|
+
raise ArgumentError, ":after option must be a string or regular expression, was : #{options[:after]}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
yield content if block
|
98
|
+
content
|
99
|
+
end
|
100
|
+
|
73
101
|
def self.remove_content_from file_name, options = {}, &block
|
74
102
|
replace_content_from file_name, options.merge(:with => ''), &block
|
75
103
|
end
|
data/lib/sugar-high/kind_of.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "active_support/inflector"
|
2
|
+
|
1
3
|
class Object
|
2
4
|
def any_kind_of? *kinds
|
3
5
|
kinds.all_kinds.each do |kind|
|
@@ -27,6 +29,23 @@ module Enumerable
|
|
27
29
|
def select_kinds_of *kinds
|
28
30
|
select{|a| a.any_kind_of? *kinds }
|
29
31
|
end
|
32
|
+
|
33
|
+
def select_labels
|
34
|
+
select{|a| a.kind_of_label? }
|
35
|
+
end
|
36
|
+
|
37
|
+
def select_symbols
|
38
|
+
select{|a| a.kind_of_symbol? }
|
39
|
+
end
|
40
|
+
|
41
|
+
def select_strings
|
42
|
+
select_only :string
|
43
|
+
end
|
44
|
+
|
45
|
+
def select_only type
|
46
|
+
const = type.kind_of_label? ? "#{type.to_s.camelize}".constantize : type
|
47
|
+
select{|a| a.kind_of? const}
|
48
|
+
end
|
30
49
|
|
31
50
|
def all_kinds
|
32
51
|
map do |a|
|
data/lib/sugar-high/regexp.rb
CHANGED
data/spec/fixtures/non-empty.txt
CHANGED
@@ -44,5 +44,42 @@ describe "SugarHigh" do
|
|
44
44
|
Dir.glob(expr).file_names('txt').should == ['empty', 'non-empty']
|
45
45
|
end
|
46
46
|
end
|
47
|
+
|
48
|
+
describe '#read_from' do
|
49
|
+
let(:non_empty_file) { fixture_file 'non-empty.txt' }
|
50
|
+
|
51
|
+
it "should read all the content into a block" do
|
52
|
+
File.read_from non_empty_file do |content|
|
53
|
+
content.should match /blip/
|
54
|
+
content.should match /blup/
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should read all the content before a given marker" do
|
59
|
+
content = File.read_from non_empty_file, :before => 'blap'
|
60
|
+
content.should match /blip/
|
61
|
+
content.should_not match /blap/
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should read all the content after a given marker" do
|
65
|
+
content = File.read_from non_empty_file, :after => 'blap'
|
66
|
+
content.should match /blup/
|
67
|
+
content.should_not match /blap/
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should read all the content before a given marker into a block" do
|
71
|
+
File.read_from non_empty_file, :before => 'blap' do |content|
|
72
|
+
content.should match /blip/
|
73
|
+
content.should_not match /blap/
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should read all the content after a given marker into a block" do
|
78
|
+
File.read_from non_empty_file, :after => 'blap' do |content|
|
79
|
+
content.should match /blup/
|
80
|
+
content.should_not match /blap/
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
47
84
|
end
|
48
85
|
|
@@ -115,7 +115,25 @@ describe "SugarHigh" do
|
|
115
115
|
# puts "all kinds: #{@label_list.all_kinds}"
|
116
116
|
@label_list.all_kinds.should == [Symbol, String]
|
117
117
|
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '#select_strings' do
|
121
|
+
it "should map mixed array to String only array" do
|
122
|
+
[1, 'blip', [3, "hello"]].select_strings.should == ['blip']
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe '#select_labels' do
|
127
|
+
it "should map mixed array to String only array" do
|
128
|
+
[1, :blap, 'blip', [3, "hello"]].select_labels.should include(:blap, 'blip')
|
129
|
+
end
|
118
130
|
end
|
131
|
+
|
132
|
+
describe '#select_only' do
|
133
|
+
it "should map mixed array to String only array" do
|
134
|
+
[1, :blap, 'blip', [3, "hello"]].select_only(:string).should include('blip')
|
135
|
+
end
|
136
|
+
end
|
119
137
|
end
|
120
138
|
end
|
121
139
|
|
data/sugar-high.gemspec
CHANGED