rcore-ext 0.1.1

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.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
@@ -0,0 +1,3 @@
1
+ ## v0.1.0
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rcore-ext.gemspec
4
+ gemspec
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rcore-ext (0.1.0)
5
+ base32
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ base32 (0.2.0)
11
+ diff-lcs (1.2.1)
12
+ rake (10.0.3)
13
+ rspec (2.13.0)
14
+ rspec-core (~> 2.13.0)
15
+ rspec-expectations (~> 2.13.0)
16
+ rspec-mocks (~> 2.13.0)
17
+ rspec-core (2.13.1)
18
+ rspec-expectations (2.13.0)
19
+ diff-lcs (>= 1.1.3, < 2.0)
20
+ rspec-mocks (2.13.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ rake
27
+ rcore-ext!
28
+ rspec
@@ -0,0 +1,75 @@
1
+ # Ruby Core Extensions
2
+
3
+ The `rcore_ext` gem extends several core classes
4
+
5
+
6
+ ## Installation
7
+
8
+ To install `rcore-ext`, run `gem install rcore-ext` or
9
+
10
+ add the following to your `Gemfile`:
11
+
12
+ gem 'rcore-ext'
13
+
14
+ Then bundle install:
15
+
16
+ bundle install
17
+
18
+ ## Usage
19
+
20
+ Examples:
21
+
22
+ ### Numeric methods
23
+
24
+ ```ruby
25
+ "1".is_integer? #=> true
26
+ "1.0".is_integer? #=> false
27
+ "not_numeric".is_integer? #=> false
28
+ ```
29
+
30
+ ```ruby
31
+ "1".is_numeric? #=> true
32
+ "1.0".is_numeric? #=> true
33
+ "not_numeric".is_numeric? #=> false
34
+ ```
35
+
36
+ ```ruby
37
+ "1".is_float? #=> false
38
+ "1.0".is_float? #=> true
39
+ "not_numeric".is_float? #=> false
40
+ ```
41
+
42
+ ```ruby
43
+ "1".to_numeric #=> 1
44
+ "1".to_numeric.class #=> Fixnum
45
+ "1.0".to_numeric #=> 1.0
46
+ "1.0".to_numeric.class #=> Float
47
+ ("1" * 10).to_numeric #=> 1111111111
48
+ ("1" * 10).to_numeric.class #=> Bignum
49
+ "not_numeric".to_numeric #=> nil
50
+ ```
51
+
52
+ ### Decode/encode methods
53
+
54
+ ```ruby
55
+ "hello world!".decode_string(:hex) #=> 68656c6c6f20776f726c6421
56
+ "hi".decode_string(:bin) #=> 0110100001101001
57
+
58
+ "NBSWY3DPEB3W64TMMQQQ====".decode_string(:base32) #=> hello world!
59
+ "aGVsbG8gd29ybGQh\n".decode_string(:base64) #=> hello world!
60
+
61
+ "aGVsbG8gd29ybGQh".decode_string(:base64, strict: true) #=> hello world!
62
+ ```
63
+
64
+ ```ruby
65
+ "68656c6c6f20776f726c6421".decode_string(:hex) #=> hello world!
66
+ "0110100001101001".decode_string(:bin) #=> hi
67
+
68
+ "hello world!".decode_string(:base32) #=> NBSWY3DPEB3W64TMMQQQ====
69
+ "hello world!".decode_string(:base64) #=> aGVsbG8gd29ybGQh\n
70
+
71
+ "hello world!".decode_string(:base64, strict: true) #=> aGVsbG8gd29ybGQh
72
+ ```
73
+
74
+
75
+ ## Feel free for pull requests!
@@ -0,0 +1,3 @@
1
+ require 'rcore_ext/version'
2
+ require 'rcore_ext/string/numeric'
3
+ require 'rcore_ext/string/format'
@@ -0,0 +1,104 @@
1
+ require 'base64'
2
+ require 'base32'
3
+
4
+ class String
5
+ @@common_formats = {bin: 'B*', hex: 'H*'}
6
+
7
+ # Decode string with various formats.
8
+ # Supported formats: :bin, :hex, :base32, :base64
9
+ #
10
+ # You can also pass options for Base64 to use
11
+ # * :strict
12
+ # * :url_safe
13
+ #
14
+ # "hello world!".decode_string(:hex) #=> 68656c6c6f20776f726c6421
15
+ # "hi".decode_string(:bin) #=> 0110100001101001
16
+ #
17
+ # "NBSWY3DPEB3W64TMMQQQ====".decode_string(:base32) #=> hello world!
18
+ # "aGVsbG8gd29ybGQh\n".decode_string(:base64) #=> hello world!
19
+ #
20
+ # "aGVsbG8gd29ybGQh".decode_string(:base64, strict: true) #=> hello world!
21
+ def decode_string(format, options={})
22
+ raise ArgumentError, "Format can't be empty" if format.nil?
23
+ format = format.to_sym
24
+ @options = options || {}
25
+
26
+ case format
27
+ when :base32
28
+ base32_decode
29
+ when :base64
30
+ base64_decode
31
+ else
32
+ raise ArgumentError, "Unsupported format: #{format}" unless @@common_formats.has_key?(format)
33
+ common_decode(format)
34
+ end
35
+ end
36
+
37
+ # Encode string with various formats.
38
+ # Supported formats: :bin, :hex, :base32, :base64
39
+ #
40
+ # You can also pass options for Base64 to use
41
+ # * :strict
42
+ # * :url_safe
43
+ #
44
+ # "68656c6c6f20776f726c6421".decode_string(:hex) #=> hello world!
45
+ # "0110100001101001".decode_string(:bin) #=> hi
46
+ #
47
+ # "hello world!".decode_string(:base32) #=> NBSWY3DPEB3W64TMMQQQ====
48
+ # "hello world!".decode_string(:base64) #=> aGVsbG8gd29ybGQh\n
49
+ #
50
+ # "hello world!".decode_string(:base64, strict: true) #=> aGVsbG8gd29ybGQh
51
+ def encode_string(format, options={})
52
+ raise ArgumentError, "Format can't be empty" if format.nil?
53
+ format = format.to_sym
54
+ @options = options || {}
55
+
56
+ case format
57
+ when :base32
58
+ base32_encode
59
+ when :base64
60
+ base64_encode
61
+ else
62
+ raise ArgumentError, "Unsupported format: #{format}" unless @@common_formats.has_key?(format)
63
+ common_encode(format)
64
+ end
65
+ end
66
+
67
+ private
68
+ def common_encode(format)
69
+ [self].pack(@@common_formats[format])
70
+ end
71
+
72
+ def common_decode(format)
73
+ self.unpack(@@common_formats[format]).first
74
+ end
75
+
76
+ def base32_encode
77
+ Base32.encode(self)
78
+ end
79
+
80
+ def base32_decode
81
+ Base32.decode(self)
82
+ end
83
+
84
+ def base64_encode
85
+ if @options[:strict]
86
+ Base64.strict_encode64(self)
87
+ elsif @options[:url_safe]
88
+ Base64.urlsafe_encode64(self)
89
+ else
90
+ Base64.encode64(self)
91
+ end
92
+ end
93
+
94
+ def base64_decode
95
+ if @options[:strict]
96
+ Base64.strict_decode64(self)
97
+ elsif @options[:url_safe]
98
+ Base64.urlsafe_decode64(self)
99
+ else
100
+ Base64.decode64(self)
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,43 @@
1
+ class String
2
+ # Checks a string on an Integer value
3
+ #
4
+ # "1".is_integer? #=> true
5
+ # "1.0".is_integer? #=> false
6
+ # "not_numeric".is_integer? #=> false
7
+ def is_integer?
8
+ true if Integer(self) rescue false
9
+ end
10
+
11
+ # Checks a string on a Numeric value
12
+ #
13
+ # "1".is_numeric? #=> true
14
+ # "1.0".is_numeric? #=> true
15
+ # "not_numeric".is_numeric? #=> false
16
+ def is_numeric?
17
+ true if Float(self) rescue false
18
+ end
19
+
20
+ # Checks a string on a Float value
21
+ #
22
+ # "1".is_float? #=> false
23
+ # "1.0".is_float? #=> true
24
+ # "not_numeric".is_float? #=> false
25
+ def is_float?
26
+ return is_numeric? unless is_integer?
27
+ false
28
+ end
29
+
30
+ # Converts a string to a Numeric values (Float, Fixnum or Bignum).
31
+ #
32
+ # "1".to_numeric #=> 1
33
+ # "1".to_numeric.class #=> Fixnum
34
+ # "1.0".to_numeric #=> 1.0
35
+ # "1.0".to_numeric.class #=> Float
36
+ # ("1" * 10).to_numeric #=> 1111111111
37
+ # ("1" * 10).to_numeric.class #=> Bignum
38
+ # "not_numeric".to_numeric #=> nil
39
+ def to_numeric
40
+ return Integer(self) if is_integer?
41
+ return Float(self) if is_float?
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module RCoreExt
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $: << File.expand_path("../lib", __FILE__)
3
+
4
+ require "rcore_ext/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.authors = ["Bakhtiyor Homidov"]
8
+ s.email = ["bakhtiyor.h@gmail.com"]
9
+ s.homepage = "https://github.com/hbakhtiyor/rcore-ext"
10
+ s.description = %q{Ruby Core Extensions in a gem}
11
+ s.summary = %q{Ruby Core Extension}
12
+
13
+ s.rubyforge_project = "rcore-ext"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
17
+ s.require_paths = ["lib"]
18
+ s.name = "rcore-ext"
19
+ s.version = RCoreExt::VERSION
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_development_dependency "rake"
23
+
24
+ s.add_runtime_dependency "base32"
25
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+ require 'rcore_ext/string/format'
3
+
4
+ describe "String formats" do
5
+ let(:plain_text) { "plain text" }
6
+ let(:url_safe) { "https://github.com/hbakhtiyor/rcore-ext" }
7
+
8
+ describe "respond to encode/decode methods" do
9
+ it "should respond to encode method" do
10
+ "".should respond_to :encode_string
11
+ end
12
+
13
+ it "should respond to decode method" do
14
+ "".should respond_to :decode_string
15
+ end
16
+ end
17
+
18
+ describe "raise exceptions" do
19
+ describe "in encode method" do
20
+ it "should raise exception with empty format" do
21
+ expect { "".encode_string }.to raise_error ArgumentError
22
+ end
23
+
24
+ it "should raise exception with unsupported format" do
25
+ expect { "".encode_string(:unsupported_format) }.to raise_error ArgumentError
26
+ end
27
+ end
28
+
29
+ describe "in decode method" do
30
+ it "should raise exception with empty format" do
31
+ expect { "".decode_string }.to raise_error ArgumentError
32
+ end
33
+
34
+ it "should raise exception with unsupported format" do
35
+ expect { "".decode_string(:unsupported_format) }.to raise_error ArgumentError
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "bin format" do
41
+ let(:bin_string) { "01110000011011000110000101101001011011100010000001110100011001010111100001110100" }
42
+
43
+ describe "decoding functionality" do
44
+ it "should only contain [01] characters" do
45
+ plain_text.decode_string(:bin).should =~ /^[01]+$/
46
+ end
47
+
48
+ it "should decode correctly" do
49
+ plain_text.decode_string(:bin).should == bin_string
50
+ end
51
+ end
52
+
53
+ describe "encoding functionality" do
54
+ it "should encode correctly" do
55
+ bin_string.encode_string(:bin).should == plain_text
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "hex format" do
61
+ let(:hex_string) { "706c61696e2074657874" }
62
+
63
+ describe "decoding functionality" do
64
+ it "should only contain [0-9a-f] characters" do
65
+ plain_text.decode_string(:hex).should =~ /^[0-9a-f]+$/i
66
+ end
67
+
68
+ it "should decode correctly" do
69
+ plain_text.decode_string(:hex).should == hex_string
70
+ end
71
+ end
72
+
73
+ describe "encoding functionality" do
74
+ it "should encode correctly" do
75
+ hex_string.encode_string(:hex).should == plain_text
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "base32 format" do
81
+ let(:base32_string) { "OBWGC2LOEB2GK6DU" }
82
+
83
+ describe "decoding functionality" do
84
+ it "should decode correctly" do
85
+ base32_string.decode_string(:base32).should == plain_text
86
+ end
87
+ end
88
+
89
+ describe "encoding functionality" do
90
+ it "should only contain [2-7a-z=] characters" do
91
+ plain_text.encode_string(:base32).should =~ /^[2-7a-z=]+$/i
92
+ end
93
+
94
+ it "should encode correctly" do
95
+ plain_text.encode_string(:base32).should == base32_string
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "base64 format" do
101
+ let(:base64_string) { "cGxhaW4gdGV4dA==\n" }
102
+ let(:base64_strict_string) { "cGxhaW4gdGV4dA==" }
103
+ let(:base64_urlsafe_string) { "aHR0cHM6Ly9naXRodWIuY29tL2hiYWtodGl5b3IvcmNvcmUtZXh0" }
104
+
105
+ describe "decoding functionality" do
106
+ it "should decode correctly" do
107
+ base64_string.decode_string(:base64).should == plain_text
108
+ end
109
+
110
+ it "should decode with strict option" do
111
+ base64_strict_string.decode_string(:base64, strict: true).should == plain_text
112
+ end
113
+
114
+ it "should decode with url_safe option" do
115
+ base64_urlsafe_string.decode_string(:base64, url_safe: true).should == url_safe
116
+ end
117
+ end
118
+
119
+ describe "encoding functionality" do
120
+ it "should only contain [0-9a-z=+/] characters" do
121
+ plain_text.encode_string(:base64).should =~ /^[0-9a-z=\/\+]+$/i
122
+ end
123
+
124
+ it "should encode correctly" do
125
+ plain_text.encode_string(:base64).should == base64_string
126
+ end
127
+
128
+ it "should decode with strict option" do
129
+ plain_text.encode_string(:base64, strict: true).should == base64_strict_string
130
+ end
131
+
132
+ it "should decode with url_safe option" do
133
+ url_safe.encode_string(:base64, url_safe: true).should == base64_urlsafe_string
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'rcore_ext/string/numeric'
3
+
4
+ describe "String numeric" do
5
+
6
+ let(:fix_num) { {act: "1", exp: 1} }
7
+ let(:float) { {act: "1.0", exp: 1.0} }
8
+ let(:not_numeric) { {act: "1not_numeric1", exp: nil} }
9
+ let(:big_num) { {act: "1" * 10, exp: 1111111111} }
10
+
11
+ describe "respond to numeric methods" do
12
+ it "should respond to is_integer? method" do
13
+ "".should respond_to :is_integer?
14
+ end
15
+
16
+ it "should respond to is_float? method" do
17
+ "".should respond_to :is_float?
18
+ end
19
+
20
+ it "should respond to is_numeric? method" do
21
+ "".should respond_to :is_numeric?
22
+ end
23
+
24
+ it "should respond to to_numeric method" do
25
+ "".should respond_to :to_numeric
26
+ end
27
+ end
28
+
29
+ describe "is_integer? method functionality" do
30
+ it "should be an integer value" do
31
+ fix_num[:act].is_integer?.should be_true
32
+ end
33
+
34
+ it "a big number should be an integer value" do
35
+ big_num[:act].is_integer?.should be_true
36
+ end
37
+
38
+ it "should not be an integer value" do
39
+ not_numeric[:act].is_integer?.should be_false
40
+ end
41
+
42
+ it "a float value should not be an integer value" do
43
+ float[:act].is_integer?.should be_false
44
+ end
45
+ end
46
+
47
+ describe "is_numeric? method functionality" do
48
+ it "should be a numeric value" do
49
+ fix_num[:act].is_numeric?.should be_true
50
+ end
51
+
52
+ it "a big number should be a numeric value" do
53
+ big_num[:act].is_numeric?.should be_true
54
+ end
55
+
56
+ it "should not be a numeric value" do
57
+ not_numeric[:act].is_numeric?.should be_false
58
+ end
59
+
60
+ it "a float value should be a numeric value" do
61
+ float[:act].is_numeric?.should be_true
62
+ end
63
+ end
64
+
65
+ describe "is_float? method functionality" do
66
+ it "an integer value should not be a float value" do
67
+ fix_num[:act].is_float?.should be_false
68
+ end
69
+
70
+ it "should not be a float value" do
71
+ not_numeric[:act].is_float?.should be_false
72
+ end
73
+
74
+ it "should be a float value" do
75
+ float[:act].is_float?.should be_true
76
+ end
77
+ end
78
+
79
+ describe "to_numeric method functionality" do
80
+ it "should be converted to an integer value" do
81
+ fix_num[:act].to_numeric.should == fix_num[:exp]
82
+ end
83
+
84
+ it "should be converted to a float value" do
85
+ float[:act].to_numeric.should == float[:exp]
86
+ end
87
+
88
+ it "should be converted to a big number value" do
89
+ big_num[:act].to_numeric.should == big_num[:exp]
90
+ end
91
+
92
+ it "should not be converted to numeric value" do
93
+ not_numeric[:act].to_numeric.should == not_numeric[:exp]
94
+ end
95
+ end
96
+ end
File without changes
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rcore-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bakhtiyor Homidov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: base32
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Ruby Core Extensions in a gem
63
+ email:
64
+ - bakhtiyor.h@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - CHANGELOG.md
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - README.md
74
+ - lib/rcore_ext.rb
75
+ - lib/rcore_ext/string/format.rb
76
+ - lib/rcore_ext/string/numeric.rb
77
+ - lib/rcore_ext/version.rb
78
+ - rcore-ext.gemspec
79
+ - spec/rcore_ext/string/format_spec.rb
80
+ - spec/rcore_ext/string/numeric_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/hbakhtiyor/rcore-ext
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project: rcore-ext
102
+ rubygems_version: 1.8.23
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Ruby Core Extension
106
+ test_files: []