itrigga-core_ext 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,24 +1,36 @@
1
1
  source "http://rubygems.org"
2
+ gemspec
2
3
  # Add dependencies required to use your gem here.
3
4
  # Example:
4
5
  # gem "activesupport", ">= 2.3.5"
5
6
 
6
7
  # Add dependencies to develop your gem here.
7
8
  # Include everything needed to run rake, tests, features, etc.
8
- gem "fastercsv", ">= 1.5.4"
9
+ gem "fastercsv", ">= 1.5.4", :require=>'false'
10
+ gem "bundler"
11
+
12
+ # HTML-cleaning
13
+ gem "sanitize"
14
+
15
+
9
16
 
10
17
  group :development do
11
- gem "rspec", "1.3.0"
12
- gem "rspec-rails", "1.3.2"
18
+ gem "rspec", "2.9.0"
19
+ gem "rspec-rails", "2.9.0"
13
20
  # gem "bundler", ">= 1.0.0"
14
21
  gem "jeweler", "~> 1.6.4"
15
22
  end
16
23
 
17
24
  group :test do
18
- gem "rspec", "1.3.0"
19
- gem "rspec-rails", "1.3.2"
20
- # gem "bundler", ">= 1.0.0"
21
- # gem "rcov", ">= 0"
22
- gem "fastercsv", ">= 1.5.4"
25
+ gem "rspec", "2.9.0"
23
26
  gem 'test-unit', '1.2.3' # needed by rspec if using ruby 1.9.X
27
+ gem "rspec-rails", "2.9.0"
28
+
29
+
30
+ if RUBY_VERSION =~ /^1.8/
31
+ gem "rcov", ">= 0"
32
+ else
33
+ gem 'simplecov', :require => false
34
+ gem 'simplecov-rcov', :require => false
35
+ end
24
36
  end
data/Rakefile CHANGED
@@ -35,23 +35,30 @@ Jeweler::Tasks.new do |gem|
35
35
  end
36
36
  Jeweler::RubygemsDotOrgTasks.new
37
37
 
38
- require 'spec/version'
39
- require 'spec/rake/spectask'
40
- require 'spec/ruby'
41
38
 
42
- Spec::Rake::SpecTask.new(:spec) do |spec|
43
- spec.spec_files = FileList['spec/**/*_spec.rb']
44
- spec.spec_opts = ['--options', 'spec/spec.opts']
45
- end
39
+ require 'rspec/core/rake_task'
46
40
 
41
+ desc 'Default: run specs.'
47
42
  task :default => :spec
48
43
 
49
- desc "Run all specs with rcov"
50
- Spec::Rake::SpecTask.new(:rcov) do |t|
51
- t.spec_files = FileList['spec/**/*_spec.rb']
52
- t.spec_opts = ['--options', 'spec/spec.opts']
44
+ desc "Run specs"
45
+ RSpec::Core::RakeTask.new do |t|
46
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
47
+ # Put spec opts in a file named .rspec in root
48
+ end
49
+
50
+ desc "Generate code coverage"
51
+ RSpec::Core::RakeTask.new(:coverage) do |t|
52
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
53
53
  t.rcov = true
54
- t.rcov_dir = 'coverage'
55
- t.rcov_opts = ['--exclude', "features,kernel,load-diff-lcs\.rb,instance_exec\.rb,lib/spec.rb,lib/spec/runner.rb,^spec/*,bin/spec,examples,/gems,/Library/Ruby,\.autotest,#{ENV['GEM_HOME']}"]
56
- t.rcov_opts << '--sort coverage --text-summary --aggregate coverage.data'
54
+ t.rcov_opts = ['--exclude', 'spec']
55
+ end
56
+ require 'rdoc/task'
57
+ Rake::RDocTask.new do |rdoc|
58
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
59
+
60
+ rdoc.rdoc_dir = 'rdoc'
61
+ rdoc.title = "net_helper #{version}"
62
+ rdoc.rdoc_files.include('README*')
63
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
64
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.0
1
+ 1.5.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "itrigga-core_ext"
8
- s.version = "1.5.0"
8
+ s.version = "1.5.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Al Davidson", "Anson Kelly"]
@@ -61,6 +61,7 @@ Gem::Specification.new do |s|
61
61
  end
62
62
  else
63
63
  s.add_dependency(%q<fastercsv>, [">= 1.5.4"])
64
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
64
65
  s.add_dependency(%q<rspec>, ["= 1.3.0"])
65
66
  s.add_dependency(%q<rspec-rails>, ["= 1.3.2"])
66
67
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
@@ -1,6 +1,16 @@
1
1
  # -*- encoding : utf-8 -*-
2
+ require 'sanitize'
2
3
  class String
3
4
 
5
+ def to_plain_text_preserving_links( )
6
+ ::Sanitize.clean( self, :elements=>['a'], :attributes=>{'a'=>['href']} ).to_s.strip
7
+ end
8
+
9
+ def to_plain_text()
10
+ ::Sanitize.clean( HTMLEntities.new.decode(self) ).to_s.strip
11
+ end
12
+
13
+
4
14
  def remove_whitespace
5
15
  self.gsub(" ","")
6
16
  end
@@ -14,14 +24,37 @@ class String
14
24
  # Includes duplicates
15
25
  #
16
26
  def word_count
17
- self.to_utf8.scan(/(\w|-|')+/).size
27
+ #re = /[\p{Word}\p{Punct}]/u # <-- tried several forms of this regex, this appears to be the only one that works correctly with Cyrillic AND Arabic script
28
+ #re = /\s+/u
29
+ #to_utf8.scan(re).size
30
+
31
+ split.inject(0) do |sum, word|
32
+ if word.contains_cjk?
33
+ sum += word.scan(cjk_regex).size # => ONLY work in Ruby 1.9.
34
+ # Search for other methods to do this for 1.8
35
+ else
36
+ sum += 1
37
+ end
38
+ end
39
+ end
40
+
41
+ def char_count
42
+ trim.size
43
+ end
44
+
45
+ def cjk_regex
46
+ /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/
47
+ end
48
+
49
+ def contains_cjk?
50
+ !!(self =~ /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/)
18
51
  end
19
52
 
20
53
  #
21
54
  # Removes starting, trailing whitespace and double spaces
22
55
  #
23
- def trim
24
- self.gsub(/^(.*[^\s])\s+$/, '\1').gsub(/^\s*(.*)$/, '\1')
56
+ def trim
57
+ self.gsub(/^(.*[^\s])\s+$/, '\1').gsub(/^\s*(.*)$/, '\1')
25
58
  end
26
59
 
27
60
  def to_utf16le
@@ -30,27 +63,28 @@ class String
30
63
 
31
64
  def to_active_record_condition
32
65
  "%#{self.trim.gsub(/[[:space:]]+/, '%')}%"
33
- end
34
66
 
67
+ end
68
+
35
69
  # need to force encoding for ruby 1.9 otherwise regex fails when comparing string of 2 different encodings
36
- # TODO : String.blank? - do we need to do force encoding? is UTF-8 a good default?
70
+ # TODO : String.blank? - do we need to do force encoding? is UTF-8 a good default?
37
71
  def blank?
38
72
  begin
39
73
  if Gem::Version.new(''+RUBY_VERSION) >= Gem::Version.new("1.9.0") # only for ruby 1.9+
40
74
  self.dup.force_encoding("UTF-8") !~ /\S/
41
- else
75
+ else
42
76
  self !~ /\S/
43
77
  end
44
- rescue ArgumentError => e
45
- if e.message =~ /^(invalid\ byte\ sequence|incompatible\ character\ encodings)/
46
- empty?
47
- else
48
- raise e
78
+ rescue ArgumentError => e
79
+ if e.message =~ /^(invalid\ byte\ sequence|incompatible\ character\ encodings)/
80
+ empty?
81
+ else
82
+ raise e
49
83
  end
50
- end
84
+ end
51
85
  end
52
86
 
53
87
  # converts the encoding to UTF-8 regardless of current encoding
54
- def to_utf8
88
+ def to_utf8
55
89
  text = self.dup
56
90
  # shiny new ruby 1.9 way
57
91
  return text if text.encoding.name == "UTF-8" && text.valid_encoding? # already utf-8 yay!
@@ -76,7 +110,7 @@ class String
76
110
 
77
111
  # ok so we are out of suggestions. Just return the string and hope that its ok
78
112
  text
79
-
113
+
80
114
  end
81
115
 
82
116
  end
@@ -6,8 +6,27 @@ RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
6
6
  $LOAD_PATH.unshift(File.join(RAILS_ROOT, 'lib'))
7
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
8
8
 
9
+ require 'rubygems'
10
+ require 'bundler/setup'
11
+
12
+ require 'rubygems'
13
+ require 'bundler'
14
+ begin
15
+ Bundler.setup(:default, :development, :test)
16
+ rescue Bundler::BundlerError => e
17
+ $stderr.puts e.message
18
+ $stderr.puts "Run `bundle install` to install missing gems"
19
+ exit e.status_code
20
+ end
21
+ require 'test/unit'
22
+ require 'rspec'
23
+
9
24
  require 'itrigga/core_ext'
10
25
 
11
26
  # Requires supporting files with custom matchers and macros, etc,
12
27
  # in ./support/ and its subdirectories.
13
28
  Dir["RAILS_ROOT}/support/**/*.rb"].each {|f| require f}
29
+
30
+ RSpec.configure do |config|
31
+
32
+ end
@@ -23,6 +23,81 @@ describe String do
23
23
  str.word_count.should == 2
24
24
  end
25
25
  end
26
+
27
+ # these examples generated with http://generator.lorem-ipsum.info/
28
+ context "when string is in Arabic" do
29
+ before{ @string =<<-END
30
+ من وقد جحافل ليرتفع بولندا, تعد أم العالم العمليات اقتصادية. مما ثم غضون عشوائية الدنمارك, كل يبق وبعد التاريخ،, تم الفاشي والألمانية، للإمبراطورية أضف. شبح واعتلاء بالهجوم و. الإطلاق الربيع، والإتحاد عن على, تسبب وبداية بتخصيص وضم من. تم بها وبدأت واستمر الإنجليز،, كنقطة انتباه لان تم.
31
+
32
+ قد فقد دخول."
33
+ END
34
+ }
35
+ it "should count the right number of words" do
36
+ @string.word_count.should == 50
37
+ end
38
+ end
39
+
40
+ context "when string is in Cyrillic" do
41
+ before{ @string = "Йн пэр аффэрт ыпикурэи интылльэгэбат! Эи ючю витаэ экшырки адвыржаряюм. Эож тебиквюэ патриоквюы ан, вим дёко омниюм конкльюдатюрквюэ но! Ыт ыюм нонумй альяквюам, ыам ед эррэм фюйзчыт, вяш ку эквюедым." }
42
+ it "should count the right number of words" do
43
+ @string.word_count.should == 30
44
+ end
45
+ end
46
+
47
+ context "when string is in Hebrew" do
48
+ before{ @string = "אל סדר וקשקש תקשורת. רבה לעתים מיותר ומהימנה את, הרוח חשמל כלשהו ויש אל. לעריכת האטמוספירה שער על. הטבע אנגלית שיתופית בקר גם, צעד או מושגי אודות. כתב אם שונה." }
49
+ it "should count the right number of words" do
50
+ @string.word_count.should == 30
51
+ end
52
+ end
53
+
54
+
55
+ context "when string contains Chinese" do
56
+ # see http://stackoverflow.com/questions/12488565/how-to-count-words-in-a-multi-language-text-using-ruby-javascript
57
+ before{ @string = 'The last Olympics was held in 北京' }
58
+ it "should count the english words as words, plus one word for each Chinese character" do
59
+ @string.word_count.should == 8
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "contains_cjk?" do
65
+ context "when the string contains Chinese characters" do
66
+ it "should be true" do
67
+ 'The last Olympics was held in 北京'.contains_cjk?.should == true
68
+ end
69
+ end
70
+ context "when the string does not contain Chinese characters" do
71
+ it "should be false" do
72
+ 'The last Olympics was held in London'.contains_cjk?.should == false
73
+ end
74
+ end
26
75
  end
27
76
 
77
+
78
+ describe "to_plain_text_preserving_links" do
79
+ context "when it contains a tags" do
80
+ let(:content){ 'here is <a href="http://foo.com/">foo</a>' }
81
+
82
+ it "preserves the a tags" do
83
+ content.to_plain_text_preserving_links.should == 'here is <a href="http://foo.com/">foo</a>'
84
+ end
85
+
86
+ context "and other tags" do
87
+ let(:content){ '<b>here is <a href="http://foo.com/"><i>foo</i></a></b><img src="http://foo.com/bar.jpg">' }
88
+
89
+ it "removes any non-a tags" do
90
+ content.to_plain_text_preserving_links.should == 'here is <a href="http://foo.com/">foo</a>'
91
+ end
92
+ end
93
+ context "and the a tags contain attributes other than href" do
94
+ let(:content){ '<b>here is <a rel="nofollow" href="http://foo.com/" title="Go to foo">foo</a></b><img src="http://foo.com/bar.jpg">' }
95
+
96
+ it "removes any non-href attributes but leaves the href attributes unaffected" do
97
+ content.to_plain_text_preserving_links.should == 'here is <a href="http://foo.com/">foo</a>'
98
+ end
99
+
100
+ end
101
+ end
102
+ end
28
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itrigga-core_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2012-05-08 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: fastercsv
17
- requirement: &70102764111580 !ruby/object:Gem::Requirement
17
+ requirement: &70191121319620 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.5.4
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70102764111580
25
+ version_requirements: *70191121319620
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70102764111100 !ruby/object:Gem::Requirement
28
+ requirement: &70191121319140 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - =
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.3.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70102764111100
36
+ version_requirements: *70191121319140
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec-rails
39
- requirement: &70102764110600 !ruby/object:Gem::Requirement
39
+ requirement: &70191121318640 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - =
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.3.2
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70102764110600
47
+ version_requirements: *70191121318640
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: jeweler
50
- requirement: &70102764110120 !ruby/object:Gem::Requirement
50
+ requirement: &70191121318160 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: 1.6.4
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70102764110120
58
+ version_requirements: *70191121318160
59
59
  description: ! "Adds methods for \n Hash: \n - symbolize_keys\n - recursive_symbolize_keys\n
60
60
  \ Array:\n - to_csv (requires FasterCSV gem if ruby 1.8)\n - select_with_index\n
61
61
  \ - to_auto_complete_list\n - active_record_type_condition_rendering\n "