extra_lib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzNhZTFmMTQ0MGU5MTFiMGU3ZDliODE4MWUyYzZkNGM4YzcyNzliMA==
5
+ data.tar.gz: !binary |-
6
+ YjllMzIzODllMTg2MjNmY2Q5Zjk3MzczNDJkOGNkMDI0YTJiZDk4OA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NDM3ZDhlYzQ2OGQxNTdkMDI1YTQxZjliNDQ4ZmM4MzU4ZGExZTlhNGNmY2Fk
10
+ N2ZkNmE3Njc2ZWUwODgzZDJjZjA4ZWFkNjQxNDIyMmNmODFkZDVjNDkyMjM1
11
+ ZTY4MGM2ZWZmNDZjMTNkNzllOGFkZWI4ZjdhYzQyYTBmMzJhODc=
12
+ data.tar.gz: !binary |-
13
+ ZDMyYWM5M2MzYWQ1OWJmZTlhMTMzYWJlNWQ1OTVjOTcyNGY5MTJlYTFmZjkx
14
+ M2FkOTdkN2U1YzIwZmE1YjljYmM5MjgzZmRhMWQyYzcwNzcwMTFhNWUyZDhh
15
+ OTE1YTJiMzdiZGVjNmIwYjk4Mjk1NmNiMDk2MjdjZmE1ZjBlYTU=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *~
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ #source 'https://rubygems.org'
2
+ source 'http://ruby.taobao.org/'
3
+
4
+ # Specify your gem's dependencies in extra_lib.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 jhjguxin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # ExtraLib
2
+
3
+ extra library for ruby from francis jiang, which include some extra method for ruby base class, need 'activeresource'
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'extra_lib'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install extra_lib
18
+
19
+ ## Usage
20
+
21
+ please check rspec ...
22
+
23
+ ## Debug
24
+
25
+ irb
26
+ $:.push File.expand_path("../lib", __FILE__)
27
+ require 'rubygems'
28
+ require 'bundler'
29
+ Bundler.require(:default)
30
+ require "extra_lib"
31
+ require "debugger"
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+ begin
7
+ require 'rdoc/task'
8
+ rescue LoadError
9
+ require 'rdoc/rdoc'
10
+ require 'rake/rdoctask'
11
+ RDoc::Task = Rake::RDocTask
12
+ end
13
+
14
+ require "bundler/gem_tasks"
15
+ require 'rspec/core/rake_task'
16
+
17
+ RSpec::Core::RakeTask.new('spec')
18
+
19
+ # If you want to make this the default task
20
+ task :default => :spec
data/extra_lib.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'extra_lib/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "extra_lib"
8
+ spec.version = ExtraLib::VERSION
9
+ spec.authors = ["jhjguxin"]
10
+ spec.email = ["864248765@qq.com"]
11
+ spec.description = %q{extra library for ruby from francis jiang}
12
+ spec.summary = %q{some extra method for ruby base class, need 'activeresource'}
13
+ spec.homepage = "https://github.com/jhjguxin/extra_lib"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activeresource", "~> 3.2.9"
22
+ spec.add_dependency 'activesupport'
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "debugger", "~> 1.2.3"
25
+ spec.add_development_dependency 'rspec', '~> 2.13.0'
26
+ #spec.add_development_dependency "rspec-expectations", "~> 2.13.0"
27
+ spec.add_development_dependency "rake"
28
+ end
data/lib/extra_lib.rb ADDED
@@ -0,0 +1,22 @@
1
+ ## require all active_support by hand
2
+ require 'active_support/all'
3
+
4
+ require "extra_lib/version"
5
+ require "extra_lib/core_ext/array"
6
+ require "extra_lib/core_ext/class"
7
+ require "extra_lib/core_ext/enumerable"
8
+ require "extra_lib/core_ext/file"
9
+ require "extra_lib/core_ext/module"
10
+ require "extra_lib/core_ext/numeric"
11
+ require "extra_lib/core_ext/object"
12
+ require "extra_lib/core_ext/array"
13
+ require "extra_lib/core_ext/objectspace"
14
+ require "extra_lib/core_ext/openstruct"
15
+ require "extra_lib/core_ext/string"
16
+ require "extra_lib/core_ext/symbol"
17
+
18
+
19
+ #Dir["#{File.dirname(__FILE__)}/lib/extra_lib/core_ext/*.rb"].each do |path|
20
+ # next if File.basename(path, '') == 'logger'
21
+ # require path
22
+ #end
@@ -0,0 +1,66 @@
1
+ class Array
2
+ # Thanks to manveru for this fun code :)
3
+ # All it does is flip the first and last elements. Pretty cool, eh? :)
4
+ #
5
+ # Example: <tt>[1, 2, 3, 4].flipflop #=> [4, 2, 3, 1]</tt>
6
+ #
7
+ # Returns: Array
8
+ #
9
+ def flipflop
10
+ if size > 1
11
+ [last] + self[1...-1] + [first]
12
+ else
13
+ self
14
+ end
15
+ end
16
+
17
+ # Destructive version of Array#flipflop.
18
+ #
19
+ # Returns: Array or nil
20
+ #
21
+ def flipflop!
22
+ if size > 1
23
+ a, b = shift, pop
24
+ unshift(b); push(a)
25
+ end
26
+ end
27
+
28
+ # Similar to String#nothing?, except it joins all the elements first and does
29
+ # the same check.
30
+ #
31
+ # Example: <tt>[" ", " ", ""].nothing? #=> true<tt>
32
+ #
33
+ # Returns: True or false.
34
+ #
35
+ def nothing?
36
+ join('').strip.empty?
37
+ end
38
+
39
+ # Check if the array contains any instances of a specific class.
40
+ #
41
+ # Example: <tt>['foo', 1, :bar].contains? Symbol #=> true</tt>
42
+ #
43
+ def contains?(klass)
44
+ map { |obj| obj.class }.include? klass
45
+ end
46
+
47
+ # replaces matched array <tt>match_array</tt> as <tt>replace_array</tt> if the array contains all elements and with same sort
48
+ #
49
+ # Example: <tt>[1, 5, 3, 4, 2].replace_array([5,3],["3", "5"]) #=> [1, "3", "5", 4, 2]</tt>
50
+ #
51
+ def replace_array(match_array, replace_arry)
52
+ array = []
53
+ match_array
54
+ self.each_index{|i|
55
+ if self[i].eql?(match_array.first) and self.slice(i, match_array.count).eql?(match_array)
56
+ array.concat self.first(i)
57
+ array.concat replace_arry
58
+ array.concat self.drop(i + match_array.count)
59
+ break
60
+ end
61
+ }
62
+ array = self if array.empty?
63
+ array
64
+ end
65
+
66
+ end
@@ -0,0 +1,23 @@
1
+ class Class
2
+ # Get all instances of this class that exist in ObjectSpace.
3
+ #
4
+ # Example: <tt>Module.instances #=> [Marshal, ObjectSpace, GC, Math, ...]</tt>
5
+ #
6
+ # Returns: Array
7
+ #
8
+ def instances
9
+ objects = []; ObjectSpace.each_object(self) { |obj| objects << obj }; objects
10
+ end
11
+
12
+ # Check whether the class has the parent `klass'.
13
+ #
14
+ # Example: <tt>Module.has_parent?(Object) #=> true</tt>
15
+ #
16
+ # Returns: True or false
17
+ #
18
+ def ancestor?(klass)
19
+ ancestors[1..-1].include? klass
20
+ end
21
+
22
+ alias parent? ancestor?
23
+ end
@@ -0,0 +1,13 @@
1
+ module Enumerable
2
+ # Get a hash representation of an array. The number of flattened array elements
3
+ # has to be even in order for this to work.
4
+ #
5
+ # Example: <tt>[:foo, :bar, :baz, :qux].to_hash #=> {:foo => :bar, :baz => :qux}</tt>
6
+ #
7
+ # Returns: Hash object
8
+ #
9
+ def to_hash
10
+ Hash[*to_a.flatten]
11
+ end
12
+
13
+ end
@@ -0,0 +1,2 @@
1
+ class File
2
+ end
@@ -0,0 +1,24 @@
1
+ class Module
2
+ # Go through each defined class in the module. Credit to apeiros for this =)
3
+ #
4
+ def each_class
5
+ constants.each { |constant_name|
6
+ constant = const_get(constant_name.intern)
7
+ yield constant if constant.class == Class && constant.name =~ /#{self.name}/
8
+ }
9
+ end
10
+
11
+ # List classes within a module. Thanks to apeiros for this.
12
+ #
13
+ # Example: <tt>Class.classes #=> [TrueClass, FalseClass, NilClass, Class]</tt>
14
+ #
15
+ # Returns: Array
16
+ #
17
+ def classes
18
+ list = []
19
+ each_class { |class_constant|
20
+ list << class_constant
21
+ }
22
+ return list
23
+ end
24
+ end
@@ -0,0 +1,53 @@
1
+ # See the math module, as methods from Math were added in Numeric dynamically.
2
+ #
3
+ class Numeric
4
+
5
+ # Checks whether a number is a negative number.
6
+ # Example: <tt>-4.negative? #=> true</tt>
7
+ #
8
+ # Returns: True or false
9
+ #
10
+ def negative?
11
+ self < 0
12
+ end
13
+
14
+ # Checks whether a number is positive. Here we will consider zero as being
15
+ # a positive number.
16
+ #
17
+ # Example: <tt>5.positive? #=> true</tt>
18
+ #
19
+ # Returns: True or false
20
+ #
21
+ def positive?
22
+ self >= 0
23
+ end
24
+
25
+ # Add commas every 3 spots in a number.
26
+ #
27
+ # Example: <tt>(4569810.12).format #=> 4,569,810.12</tt>
28
+ #
29
+ # Returns: Commatized string
30
+ #
31
+ def format(comma = ',', decimal = '.')
32
+ to_s.reverse.scan(/(?:-?\d{1,3}(?:\.\d{1,3})?-?)/).map { |s| s.sub('.', decimal) }.join(comma).reverse
33
+ end
34
+
35
+ # Credit to apeiros for this method.
36
+ #
37
+ # Min/max method.
38
+ #
39
+ # Example: <tt>-2.crop(0..1) # => 0</tt>
40
+ #
41
+ # Returns: Numeric
42
+ #
43
+ def crop(range_or_min, max=nil)
44
+ range = max ? range_or_min..max : range_or_min
45
+ if range.include?(self)
46
+ self
47
+ elsif self < range.first
48
+ range.first
49
+ else
50
+ range.last
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,88 @@
1
+ class Object
2
+ # whytheluckystiff: http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
3
+ #
4
+ # Gets a metaclass (a class of a class).
5
+ #
6
+ # Example: <tt>'hello'.metaclass #=> #<Class:#<String:0xb7a57998>></tt>
7
+ #
8
+ # Returns: The metaclass.
9
+ #
10
+ def metaclass
11
+ class << self; self; end
12
+ end
13
+
14
+ # whytheluckystiff: http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
15
+ #
16
+ # Evaluate code on the metaclass.
17
+ #
18
+ # Example:
19
+ #
20
+ # <tt> s = 'foo'; s.meta_eval { define_method(:longer) { self * 2 } }</tt>
21
+ #
22
+ # <tt> s.longer #=> "foofoo"'</tt>
23
+ #
24
+ # Returns: The block's final expression.
25
+ #
26
+ def meta_eval(&block)
27
+ metaclass.instance_eval(&block)
28
+ end
29
+
30
+ # whytheluckystiff: http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
31
+ #
32
+ # Define an instance method on the metaclass.
33
+ #
34
+ # Example: <tt>s = 'foo'; s.meta_def(:longer) { self * 2 }; s.longer #=> "foofoo"</tt>
35
+ #
36
+ # Returns: A Proc object of the method.
37
+ #
38
+ def meta_def(name, &block)
39
+ meta_eval { define_method(name, &block) }
40
+ end
41
+
42
+ # whytheluckystiff: http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
43
+ #
44
+ # Adds a class instance method.
45
+ #
46
+ # Example:
47
+ #
48
+ # <tt> SomeClass.class_def(:whoami) { 'I am SomeClass, silly!' }</tt>
49
+ #
50
+ # <tt> SomeClass.whoami #=> "I am SomeClass, silly!"</tt>
51
+ #
52
+ # Returns: A Proc object of the method, or nil.
53
+ #
54
+ def class_def(name, &block)
55
+ class_eval { define_method(name, &block) } if kind_of? Class
56
+ end
57
+
58
+ # Credit to the original author. This method retrieves a deep copy of the
59
+ # current object.
60
+ #
61
+ # Returns: Deep copy of the same object.
62
+ #
63
+ def deepcopy
64
+ Marshal.load(Marshal.dump(self))
65
+ end
66
+
67
+ # Convert object to boolean.
68
+ #
69
+ # Example:
70
+ #
71
+ # <tt> "foo".to_bool #=> true</tt>
72
+ #
73
+ # <tt> false.to_bool #=> false</tt>
74
+ #
75
+ # <tt> nil.to_bool #=> nil</tt>
76
+ #
77
+ # <tt> true.to_bool #=> true</tt>
78
+ #
79
+ # Returns: Boolean or nil.
80
+ #
81
+ def to_bool
82
+ if [FalseClass, NilClass].include? self.class
83
+ self
84
+ else
85
+ true
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ module ObjectSpace
2
+ class << self
3
+ include Enumerable; alias each each_object
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ require 'ostruct'
2
+
3
+ class OpenStruct
4
+ # Gets the open struct hash.
5
+ #
6
+ # Returns: Hash
7
+ #
8
+ def to_hash
9
+ @table
10
+ end
11
+
12
+ # Get the YAML representation of the struct.
13
+ #
14
+ # Returns: YAML string
15
+ #
16
+ def to_yaml(*args)
17
+ require 'yaml'
18
+ to_hash.to_yaml(*args)
19
+ end
20
+ end
@@ -0,0 +1,134 @@
1
+ class String
2
+
3
+ # Check self or provider string is defined as an constant or constantize.
4
+ #
5
+ # Example: <tt>"String".class_exists? #=> true</tt>
6
+ #
7
+ # Returns: boolen
8
+ #
9
+ def class_exists?(class_name = nil)
10
+ class_name = self if class_name.nil?
11
+ (class_name.capitalize.constantize.is_a?(Class) ? true : false rescue false) or (class_name.classify.constantize.is_a?(Class) ? true : false rescue false)
12
+ end
13
+
14
+ # Generate a random string with a given length and seed.
15
+ #
16
+ # Example: <tt>String.random(4, 'abcdefg') #=> "cdeg"</tt>
17
+ #
18
+ # Returns: String
19
+ #
20
+ def self.random(length = 8, seed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
21
+ length ||= 8
22
+ s = ''; length.times { s << seed.shuffle[0] }; s
23
+ end
24
+
25
+ def random(length = 8, seed = nil)
26
+ seed = self if seed.nil?
27
+ self.class.random(length, self)
28
+ end
29
+
30
+ # Randomly shuffle a string.
31
+ #
32
+ # Example: <tt>'foobar'.shuffle #=> bofoar</tt>
33
+ #
34
+ # Returns: String
35
+ #
36
+ def shuffle
37
+ split(//u).sort_by { rand }.join('')
38
+ end
39
+
40
+ # Destructive version of String#shuffle.
41
+ #
42
+ # Returns: String or nil
43
+ #
44
+ def shuffle!
45
+ shuffled = shuffle
46
+ self[0..-1] = shuffled unless shuffled == self
47
+ end
48
+
49
+ # Split string into an array of characters. Should be multi-byte safe...
50
+ #
51
+ # Example: <tt>'foo'.chars #=> ["f", "o", "o"]</tt>
52
+ #
53
+ # Returns: Array
54
+ #
55
+ # 'foo'.chars.to_a # => ["f", "o", "o"]
56
+
57
+ # Capitalize words.
58
+ # Example: <tt>'The dog is stupid'.capitalize_words('is') #=> "The Dog is Stupid"</tt>
59
+ #
60
+ # Returns: String with words capitalized.
61
+ #
62
+ def capitalize_words(*disclude)
63
+ disclude = disclude.flatten
64
+ # flatten(level)
65
+ # Returns a new array that is a one-dimensional flattening of self (recursively).
66
+ gsub(/\w+/u) { |word| disclude.include?(word) ? word : word.capitalize }
67
+ end
68
+
69
+ # My unsubmitted answer to a previous RubyQuiz question. Basically #munge will
70
+ # take words, scramble only the middle contents of the word while the first and
71
+ # last letters remain intact.
72
+ #
73
+ # Example: <tt>'You look like a terrifying goblin'.munge #=> "You look lkie a tiifyenrrg goilbn"</tt>
74
+ #
75
+ # Returns: Munged string
76
+ #
77
+ def munge
78
+ gsub(/\w+/u) do |word|
79
+ if word.size > 2
80
+ word[0,1] << word[1...-1].shuffle << word[-1,1]
81
+ else
82
+ word
83
+ end
84
+ end
85
+ end
86
+
87
+ # Destructive version of String#munge.
88
+ #
89
+ # Returns: Munged string or nil.
90
+ #
91
+ def munge!
92
+ munged = munge
93
+ self[0..-1] = munged unless munged == self
94
+ end
95
+
96
+ # Get an array of "words".
97
+ #
98
+ # Example: <tt>"hello, world!".words #=> ["hello", "world"]</tt>
99
+ #
100
+ # Returns: Array
101
+ #
102
+ def words
103
+ scan(/\w+/u)
104
+ end
105
+
106
+ # Wrap string by characters and join them by a specified separator.
107
+ #
108
+ # Example: <tt>"1234".wrap(2) #=> "12\n34"</tt>
109
+ #
110
+ # Returns: String
111
+ #
112
+ def wrap(width = 80, separator = $/)
113
+ scan(/.{1,#{width}}/u).join(separator)
114
+ end
115
+
116
+ # Destructive version of String#wrap.
117
+ #
118
+ # Returns: String or nil
119
+ #
120
+ def wrap!(width = 80, separator = $/)
121
+ wrapped = wrap(width, separator)
122
+ self[0..-1] = wrapped unless wrapped == self
123
+ end
124
+
125
+ # Checks if a string is nothing but whitespace or is empty.
126
+ #
127
+ # Example: <tt>" ".nothing? #=> true</tt>
128
+ #
129
+ # Returns: True or false
130
+ #
131
+ alias nothing? blank?
132
+
133
+ alias +@ size
134
+ end
@@ -0,0 +1,2 @@
1
+ class Symbol
2
+ end
@@ -0,0 +1,4 @@
1
+ module ExtraLib
2
+ # version base on activeresource's version
3
+ VERSION = "0.1.1"
4
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ describe Array do
4
+ describe "#flipflop" do
5
+ let(:array) { [1, 2, 3, 4] }
6
+
7
+ it "should be defined" do
8
+ Array.method_defined?(:flipflop).should be_true
9
+ end
10
+
11
+ it "should return [4, 2, 3, 1]" do
12
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
13
+ array.flipflop.should eql([4, 2, 3, 1])
14
+ end
15
+ end
16
+ describe "#flipflop" do
17
+ let(:array) { [1, 2, 3, 4] }
18
+
19
+ it "should be defined" do
20
+ Array.method_defined?(:flipflop!).should be_true
21
+ end
22
+
23
+ it "should return [4, 2, 3, 1]" do
24
+ array.flipflop!.should eql([4, 2, 3, 1])
25
+ end
26
+
27
+ it "should destructive array self" do
28
+ array.flipflop!.should eql(array)
29
+ end
30
+ end
31
+ describe "#nothing" do
32
+ it "should be defined" do
33
+ Array.method_defined?(:nothing?).should be_true
34
+ end
35
+
36
+ it "should destructive array self" do
37
+ [" ", " ", ""].nothing?.should be_true
38
+ end
39
+ end
40
+ describe "#contains?" do
41
+ it "should be defined" do
42
+ Array.method_defined?(:contains?).should be_true
43
+ end
44
+
45
+ it "should destructive array self" do
46
+ ['foo', 1, :bar].contains?(Symbol).should be_true
47
+ end
48
+ end
49
+ describe "#replace_array" do
50
+ it "should be defined" do
51
+ Array.method_defined?(:replace_array).should be_true
52
+ end
53
+
54
+ it "should return array replace" do
55
+ [1, 5, 3, 4, 2].replace_array([5,3],["3", "5"]).should eql([1, "3", "5", 4, 2])
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe Class do
4
+ describe "#instances" do
5
+ it "should be defined" do
6
+ Class.method_defined?(:instances).should be_true
7
+ end
8
+
9
+ it "should include instance defined" do
10
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
11
+ test_class = TestClass.new
12
+ TestClass.instances.should include(test_class)
13
+ end
14
+ end
15
+
16
+ describe "#ancestor?" do
17
+ it "should be defined" do
18
+ Class.method_defined?(:ancestor?).should be_true
19
+ end
20
+
21
+ it "should has ParentClass defined" do
22
+ ChildrenClass.ancestor?(ParentClass).should be_true
23
+ end
24
+ end
25
+
26
+ describe "#parent?" do
27
+ it "should be defined" do
28
+ Class.method_defined?(:parent?).should be_true
29
+ end
30
+
31
+ it "should has ParentClass defined" do
32
+ ChildrenClass.parent?(ParentClass).should be_true
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+
3
+ describe Enumerable do
4
+ describe "#to_hash" do
5
+ it "should be defined" do
6
+ Enumerable.method_defined?(:to_hash).should be_true
7
+ end
8
+
9
+ it "should return an hash" do
10
+ [:foo, :bar, :baz, :qux].to_hash.should eql({:foo => :bar, :baz => :qux})
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe File do
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe Module do
4
+
5
+ end
@@ -0,0 +1,66 @@
1
+ require "spec_helper"
2
+
3
+ describe Numeric do
4
+ describe "#negative?" do
5
+ it "should be defined" do
6
+ Numeric.method_defined?(:negative?).should be_true
7
+ end
8
+
9
+ it "'-1.negative?' should return an true" do
10
+ -1.negative?.should be_true
11
+ end
12
+
13
+ it "'1.negative?' should return an false" do
14
+ 1.negative?.should be_false
15
+ end
16
+
17
+ it "'0.negative?' should return an false" do
18
+ 0.negative?.should be_false
19
+ end
20
+ end
21
+
22
+ describe "#positive?" do
23
+ it "should be defined" do
24
+ Numeric.method_defined?(:positive?).should be_true
25
+ end
26
+
27
+ it "'-1.positive?' should return an true" do
28
+ -1.positive?.should be_false
29
+ end
30
+
31
+ it "'1.positive?' should return an false" do
32
+ 1.positive?.should be_true
33
+ end
34
+
35
+ it "'0.positive?' should return an false" do
36
+ 0.positive?.should be_true
37
+ end
38
+ end
39
+ describe "#format" do
40
+ it "should be defined" do
41
+ Numeric.method_defined?(:format).should be_true
42
+ end
43
+
44
+ it "should return an string" do
45
+ (4569810.12).format.should eql("4,569,810.12")
46
+ end
47
+ end
48
+ describe "#crop" do
49
+ let (:num) { -2 }
50
+ it "should be defined" do
51
+ Numeric.method_defined?(:crop).should be_true
52
+ end
53
+
54
+ it "should return '0' if provider '(0..1)'" do
55
+ -2.crop(0..1).should eql(0)
56
+ end
57
+
58
+ it "should return '-2' if provider '(-3..1)'" do
59
+ -2.crop(-3..1).should eql(-2)
60
+ end
61
+
62
+ it "should return '-2' if provider '(-3..-2.5)'" do
63
+ -2.crop(-3..-2.5).should eql(-2.5)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,138 @@
1
+ require "spec_helper"
2
+
3
+ describe String do
4
+ describe "#class_exists?" do
5
+
6
+ it "should be defined" do
7
+ String.method_defined?(:class_exists?).should be_true
8
+ end
9
+
10
+ it "should be true" do
11
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
12
+ "String".class_exists?.should be_true
13
+ end
14
+ end
15
+ describe "String#random" do
16
+ it "should be defined" do
17
+ String.method_defined?(:random).should be_true
18
+ end
19
+ context "set length as 4" do
20
+ it "should be true" do
21
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
22
+ String.random(4, "abcdef").length.should eql(4)
23
+ end
24
+ end
25
+ end
26
+ describe "#random" do
27
+ let (:str) { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" }
28
+
29
+ it "should be defined" do
30
+ String.method_defined?(:random).should be_true
31
+ end
32
+ context "set length as 4" do
33
+ it "should be true" do
34
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
35
+ str.random(4).length.should eql(4)
36
+ end
37
+ it "random string's each char should from str" do
38
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
39
+ is_include_all = true
40
+ str.random(4).each_char{|c| is_include_all = false unless str.include?(c)}
41
+
42
+ expect(is_include_all).to be_true
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#shuffle" do
48
+ let (:str) { "abcdefghijklmnopqr" }
49
+
50
+ it "should be defined" do
51
+ String.method_defined?(:shuffle).should be_true
52
+ end
53
+ context "set length as 4" do
54
+ it "should be true" do
55
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
56
+ str.shuffle.length.should eql(str.length)
57
+ end
58
+ it "each char should from str" do
59
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
60
+ is_include_all = true
61
+ str.shuffle.each_char{|c| is_include_all = false unless str.include?(c)}
62
+
63
+ expect(is_include_all).to be_true
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "#capitalize_words" do
69
+ let (:str) { "The dog is stupid" }
70
+
71
+ it "should be defined" do
72
+ String.method_defined?(:capitalize_words).should be_true
73
+ end
74
+ context "String with words capitalized" do
75
+ it "should words capitalized" do
76
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
77
+ str.capitalize_words('is').should eql("The Dog is Stupid")
78
+ end
79
+ end
80
+ end
81
+
82
+ describe "#munge" do
83
+ let (:str) { "You look like a terrifying goblin" }
84
+
85
+ it "should be defined" do
86
+ String.method_defined?(:munge).should be_true
87
+ end
88
+ context "#munge will take words, scramble only the middle contents of the word while the first and last letters remain intact" do
89
+ it "should return munged string" do
90
+ is_munge_word = true
91
+ str_words = str.words
92
+ munge_words = str.munge.words
93
+ i = 0
94
+ munge_words.each{|w|
95
+
96
+ unless w.eql?(str_words[i])
97
+ if !(w[0].eql?(str_words[i][0]) and w[-1].eql?(str_words[i][-1]))
98
+ puts "#{[w, i, str_words[i]]}"
99
+ is_munge_word = false
100
+ break
101
+ end
102
+ end
103
+ i += 1
104
+ }
105
+
106
+ expect(is_munge_word).to be_true
107
+ end
108
+ end
109
+ end
110
+
111
+ describe "#wrap" do
112
+ let (:str) { "1234" }
113
+
114
+ it "should be defined" do
115
+ String.method_defined?(:wrap).should be_true
116
+ end
117
+ context "Wrap string by characters and join them by a specified separator" do
118
+ it "should be string insert specified separator" do
119
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
120
+ str.wrap(2).should eql("12\n34")
121
+ end
122
+ end
123
+ end
124
+
125
+ describe "#nothing" do
126
+ let (:str) { " " }
127
+
128
+ it "should be defined" do
129
+ String.method_defined?(:nothing?).should be_true
130
+ end
131
+ context "Wrap string by characters and join them by a specified separator" do
132
+ it "should be string insert specified separator" do
133
+ #array.flipflop.eql?([4, 3, 2, 1]).should be_true
134
+ str.nothing?.should be_true
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe ExtraLib::VERSION do
4
+ describe "#initialize" do
5
+ it "should be defined" do
6
+ defined?(ExtraLib::VERSION)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'extra_lib'
9
+ require 'rspec'
10
+ require 'rspec/autorun'
11
+
12
+
13
+ # Requires supporting ruby files with custom matchers and macros, etc,
14
+ # in spec/support/ and its subdirectories.
15
+ Dir['./spec/support/**/*.rb'].each {|f| require f}
16
+
17
+ RSpec.configure do |config|
18
+ config.mock_with :rspec
19
+ config.treat_symbols_as_metadata_keys_with_true_values = true
20
+ config.run_all_when_everything_filtered = true
21
+ config.filter_run :focus
22
+
23
+ # Run specs in random order to surface order dependencies. If you find an
24
+ # order dependency and want to debug it, you can fix the order by providing
25
+ # the seed, which is printed after each run.
26
+ # --seed 1234
27
+ config.order = 'random'
28
+
29
+ # https://www.relishapp.com/rspec/rspec-expectations/docs/syntax-configuration
30
+ # https://github.com/rspec/rspec-expectations/blob/master/Should.md
31
+ config.expect_with :rspec do |c|
32
+ c.syntax = [:should, :expect, :matches?]
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ class ParentClass
2
+ end
3
+
4
+ class ChildrenClass < ParentClass
5
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ class TestClass
2
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extra_lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - jhjguxin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeresource
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: debugger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.2.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.13.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.13.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: extra library for ruby from francis jiang
98
+ email:
99
+ - 864248765@qq.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - extra_lib.gemspec
111
+ - lib/extra_lib.rb
112
+ - lib/extra_lib/core_ext/array.rb
113
+ - lib/extra_lib/core_ext/class.rb
114
+ - lib/extra_lib/core_ext/enumerable.rb
115
+ - lib/extra_lib/core_ext/file.rb
116
+ - lib/extra_lib/core_ext/module.rb
117
+ - lib/extra_lib/core_ext/numeric.rb
118
+ - lib/extra_lib/core_ext/object.rb
119
+ - lib/extra_lib/core_ext/objectspace.rb
120
+ - lib/extra_lib/core_ext/openstruct.rb
121
+ - lib/extra_lib/core_ext/string.rb
122
+ - lib/extra_lib/core_ext/symbol.rb
123
+ - lib/extra_lib/version.rb
124
+ - spec/extra_lib/core_ext/array_spec.rb
125
+ - spec/extra_lib/core_ext/class_spec.rb
126
+ - spec/extra_lib/core_ext/enumerable_spec.rb
127
+ - spec/extra_lib/core_ext/file_spec.rb
128
+ - spec/extra_lib/core_ext/module_spec.rb
129
+ - spec/extra_lib/core_ext/numeric_spec.rb
130
+ - spec/extra_lib/core_ext/string_spec.rb
131
+ - spec/extra_lib/extra_lib_spec.rb
132
+ - spec/spec_helper.rb
133
+ - spec/support/ancestor_class.rb
134
+ - spec/support/parent_class.rb
135
+ - spec/support/test_class.rb
136
+ homepage: https://github.com/jhjguxin/extra_lib
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.0.3
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: some extra method for ruby base class, need 'activeresource'
160
+ test_files:
161
+ - spec/extra_lib/core_ext/array_spec.rb
162
+ - spec/extra_lib/core_ext/class_spec.rb
163
+ - spec/extra_lib/core_ext/enumerable_spec.rb
164
+ - spec/extra_lib/core_ext/file_spec.rb
165
+ - spec/extra_lib/core_ext/module_spec.rb
166
+ - spec/extra_lib/core_ext/numeric_spec.rb
167
+ - spec/extra_lib/core_ext/string_spec.rb
168
+ - spec/extra_lib/extra_lib_spec.rb
169
+ - spec/spec_helper.rb
170
+ - spec/support/ancestor_class.rb
171
+ - spec/support/parent_class.rb
172
+ - spec/support/test_class.rb