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 CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -1,4 +1,6 @@
1
- class Array
1
+ require 'sugar-high/kind_of'
2
+
3
+ class Array
2
4
  def to_symbols option=nil
3
5
  res = self.flatten
4
6
  res = case option
@@ -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
@@ -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|
@@ -7,6 +7,16 @@ end
7
7
  class Regexp
8
8
  def to_regexp
9
9
  self
10
+ end
11
+ end
12
+
13
+ class MatchData
14
+ def offset_after
15
+ offset(0)[1] +1
16
+ end
17
+
18
+ def offset_before
19
+ offset(0)[0] -1
10
20
  end
11
21
  end
12
22
 
@@ -1 +1,3 @@
1
- blip
1
+ blip
2
+ blap
3
+ blup
@@ -11,6 +11,6 @@ describe "SugarHigh" do
11
11
  it "should translate nested array of numbers and strings into symbols only array, including numbers" do
12
12
  [1, 'blip', [3, "hello"]].to_symbols(:num).should == [:_1, :blip, :_3, :hello]
13
13
  end
14
- end
14
+ end
15
15
  end
16
16
  end
@@ -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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sugar-high}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
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"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup