ip_as_int 0.0.1 → 0.0.2
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.
- data/Gemfile +0 -9
- data/README.md +67 -58
- data/ip_as_int.gemspec +7 -1
- data/lib/ip_as_int/version.rb +1 -1
- metadata +101 -7
- data/spec/ip_address_attribute_spec.rb~ +0 -112
data/Gemfile
CHANGED
|
@@ -3,12 +3,3 @@ source 'https://rubygems.org'
|
|
|
3
3
|
# Specify your gem's dependencies in ip_as_int.gemspec
|
|
4
4
|
gemspec
|
|
5
5
|
|
|
6
|
-
gem 'rspec'
|
|
7
|
-
gem 'rake'
|
|
8
|
-
gem 'activemodel'
|
|
9
|
-
# one-liner to install these properly: bash < <(curl -L https://raw.github.com/gist/1333785)
|
|
10
|
-
# gem 'linecache19', '0.5.13'
|
|
11
|
-
# gem 'ruby-debug-base19', '0.11.26'
|
|
12
|
-
gem 'debugger'
|
|
13
|
-
gem 'activerecord'
|
|
14
|
-
gem 'sqlite3'
|
data/README.md
CHANGED
|
@@ -2,91 +2,100 @@
|
|
|
2
2
|
|
|
3
3
|
IP address - integer conversion and activerecord support for ip attribute as integer column.
|
|
4
4
|
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
Add this line to your application's Gemfile:
|
|
8
|
-
|
|
9
|
-
gem 'ip_as_int'
|
|
10
|
-
|
|
11
|
-
And then execute:
|
|
12
|
-
|
|
13
|
-
$ bundle
|
|
14
|
-
|
|
15
|
-
Or install it yourself as:
|
|
16
|
-
|
|
17
|
-
$ gem install ip_as_int
|
|
18
|
-
|
|
19
5
|
## Usage
|
|
20
6
|
|
|
21
7
|
### IP address - integer conversion
|
|
22
8
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
=>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
>> ip_string = IpAsInt.int2ip(ip_int)
|
|
30
|
-
=> "192.168.0.1"
|
|
9
|
+
```ruby
|
|
10
|
+
require 'ip_as_int'
|
|
11
|
+
ip_string = '192.168.0.1'
|
|
12
|
+
ip_int = IpAsInt.ip2int(ip_string) # => 3232235521
|
|
13
|
+
ip_string = IpAsInt.int2ip(ip_int) # => "192.168.0.1"
|
|
14
|
+
```
|
|
31
15
|
|
|
32
16
|
Exceptions on invalid IP address
|
|
33
17
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
18
|
+
```ruby
|
|
19
|
+
IpAsInt.ip2int('192.168.0')
|
|
20
|
+
# => ArgumentError: Invalid IP: need 4 parts
|
|
21
|
+
IpAsInt.ip2int('192.168.0.s')
|
|
22
|
+
# => ArgumentError: Invalid IP: illegal format
|
|
23
|
+
IpAsInt.ip2int('192.168.0.256')
|
|
24
|
+
# => ArgumentError: Invalid IP: integer out of range
|
|
25
|
+
```
|
|
40
26
|
|
|
41
27
|
### ActiveRecord IP address attribute as integer column
|
|
42
28
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
29
|
+
```ruby
|
|
30
|
+
class Model < ActiveRecord::Base
|
|
31
|
+
include IpAsInt::IpAddressAttribute
|
|
32
|
+
ip_address :ip # :ip as integer column
|
|
33
|
+
search_methods :ip # to allow squeel to do Model.search(:ip => '192.168.0.1')
|
|
34
|
+
end
|
|
35
|
+
```
|
|
48
36
|
|
|
49
37
|
Attribute methods are defined to let the ip attribute assigned a string, conversion to integer column is done automatically.
|
|
50
38
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
=> "192.168.0.1"
|
|
39
|
+
```ruby
|
|
40
|
+
m = Model.create(:ip => '192.168.0.1') # => #<Model id: 1, ip: 3232235521>
|
|
41
|
+
m.valid? # => true
|
|
42
|
+
m.reload.ip # => "192.168.0.1"
|
|
43
|
+
```
|
|
57
44
|
|
|
58
45
|
a scope to retrieve IP by string is also provided unless `scope: false` option is given.
|
|
59
46
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
47
|
+
```ruby
|
|
48
|
+
Model.create(
|
|
49
|
+
[{ :ip => '192.168.0.1' },
|
|
50
|
+
{ :ip => '192.168.0.2' },
|
|
51
|
+
{ :ip => '192.168.0.3' }
|
|
52
|
+
])
|
|
53
|
+
# => [#<Model id: 3, ip: 3232235521>, #<Model id: 4, ip: 3232235522>, #<Model id: 5, ip: 3232235523>]
|
|
54
|
+
Model.ip('192.168.0.1', '192.168.0.2')
|
|
55
|
+
# => [#<Model id: 3, ip: 3232235521>, #<Model id: 4, ip: 3232235522>]
|
|
56
|
+
```
|
|
68
57
|
|
|
69
58
|
a validation of the ip address is set up unless `validation: false` option is given
|
|
70
59
|
|
|
71
|
-
|
|
72
|
-
|
|
60
|
+
```ruby
|
|
61
|
+
Model.create!(:ip => '192.168.0')
|
|
62
|
+
# => ActiveRecord::RecordInvalid: Validation failed: Ip has to be a valid IP address
|
|
63
|
+
```
|
|
73
64
|
|
|
74
65
|
## Further use
|
|
75
66
|
|
|
76
67
|
In fact, you can also use the `IpAsInt::IpAddressAttribute` module with active model only.
|
|
77
68
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
69
|
+
```ruby
|
|
70
|
+
class Model
|
|
71
|
+
include ActiveModel::Validations
|
|
72
|
+
include ::IpAsInt::IpAddressAttribute
|
|
73
|
+
ip_address :ip
|
|
74
|
+
end
|
|
75
|
+
```
|
|
83
76
|
|
|
84
77
|
The validator can also be used by itself on an IP attribute irrespective of underlying storage (i.e., even if you use a string column)
|
|
85
78
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
79
|
+
```ruby
|
|
80
|
+
class Model
|
|
81
|
+
# :ip string column
|
|
82
|
+
validates :ip, :ip_address => { :allow_blank => true }
|
|
83
|
+
end
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Installation
|
|
87
|
+
|
|
88
|
+
Add this line to your application's Gemfile:
|
|
89
|
+
|
|
90
|
+
gem 'ip_as_int'
|
|
91
|
+
|
|
92
|
+
And then execute:
|
|
93
|
+
|
|
94
|
+
$ bundle
|
|
95
|
+
|
|
96
|
+
Or install it yourself as:
|
|
97
|
+
|
|
98
|
+
$ gem install ip_as_int
|
|
90
99
|
|
|
91
100
|
## Contributing
|
|
92
101
|
|
data/ip_as_int.gemspec
CHANGED
|
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
|
|
|
8
8
|
gem.email = ["viktor.tron@gmail.com"]
|
|
9
9
|
gem.description = DESC
|
|
10
10
|
gem.summary = DESC
|
|
11
|
-
gem.homepage = ""
|
|
11
|
+
gem.homepage = "https://github.com/zelig/ip_as_int"
|
|
12
12
|
|
|
13
13
|
gem.files = `git ls-files`.split($\)
|
|
14
14
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
@@ -18,4 +18,10 @@ Gem::Specification.new do |gem|
|
|
|
18
18
|
gem.version = IpAsInt::VERSION
|
|
19
19
|
|
|
20
20
|
gem.add_development_dependency 'rspec'
|
|
21
|
+
gem.add_development_dependency 'rspec'
|
|
22
|
+
gem.add_development_dependency 'rake'
|
|
23
|
+
gem.add_development_dependency 'activemodel'
|
|
24
|
+
gem.add_development_dependency 'debugger'
|
|
25
|
+
gem.add_development_dependency 'activerecord'
|
|
26
|
+
gem.add_development_dependency 'sqlite3'
|
|
21
27
|
end
|
data/lib/ip_as_int/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ip_as_int
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2013-01-
|
|
12
|
+
date: 2013-01-28 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rspec
|
|
@@ -27,6 +27,102 @@ dependencies:
|
|
|
27
27
|
- - ! '>='
|
|
28
28
|
- !ruby/object:Gem::Version
|
|
29
29
|
version: '0'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rspec
|
|
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: rake
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ! '>='
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
type: :development
|
|
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
|
+
- !ruby/object:Gem::Dependency
|
|
63
|
+
name: activemodel
|
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
none: false
|
|
66
|
+
requirements:
|
|
67
|
+
- - ! '>='
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
70
|
+
type: :development
|
|
71
|
+
prerelease: false
|
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
73
|
+
none: false
|
|
74
|
+
requirements:
|
|
75
|
+
- - ! '>='
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '0'
|
|
78
|
+
- !ruby/object:Gem::Dependency
|
|
79
|
+
name: debugger
|
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
|
81
|
+
none: false
|
|
82
|
+
requirements:
|
|
83
|
+
- - ! '>='
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
type: :development
|
|
87
|
+
prerelease: false
|
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
89
|
+
none: false
|
|
90
|
+
requirements:
|
|
91
|
+
- - ! '>='
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '0'
|
|
94
|
+
- !ruby/object:Gem::Dependency
|
|
95
|
+
name: activerecord
|
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
|
97
|
+
none: false
|
|
98
|
+
requirements:
|
|
99
|
+
- - ! '>='
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '0'
|
|
102
|
+
type: :development
|
|
103
|
+
prerelease: false
|
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
105
|
+
none: false
|
|
106
|
+
requirements:
|
|
107
|
+
- - ! '>='
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
110
|
+
- !ruby/object:Gem::Dependency
|
|
111
|
+
name: sqlite3
|
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
|
113
|
+
none: false
|
|
114
|
+
requirements:
|
|
115
|
+
- - ! '>='
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
none: false
|
|
122
|
+
requirements:
|
|
123
|
+
- - ! '>='
|
|
124
|
+
- !ruby/object:Gem::Version
|
|
125
|
+
version: '0'
|
|
30
126
|
description: IP address - integer conversion and activerecord support for ip attribute
|
|
31
127
|
as integer column
|
|
32
128
|
email:
|
|
@@ -52,9 +148,8 @@ files:
|
|
|
52
148
|
- lib/ip_as_int/version.rb
|
|
53
149
|
- spec/ip2int_spec.rb
|
|
54
150
|
- spec/ip_address_attribute_spec.rb
|
|
55
|
-
- spec/ip_address_attribute_spec.rb~
|
|
56
151
|
- spec/spec_helper.rb
|
|
57
|
-
homepage:
|
|
152
|
+
homepage: https://github.com/zelig/ip_as_int
|
|
58
153
|
licenses: []
|
|
59
154
|
post_install_message:
|
|
60
155
|
rdoc_options: []
|
|
@@ -68,7 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
68
163
|
version: '0'
|
|
69
164
|
segments:
|
|
70
165
|
- 0
|
|
71
|
-
hash:
|
|
166
|
+
hash: -3385027163081094626
|
|
72
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
168
|
none: false
|
|
74
169
|
requirements:
|
|
@@ -77,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
77
172
|
version: '0'
|
|
78
173
|
segments:
|
|
79
174
|
- 0
|
|
80
|
-
hash:
|
|
175
|
+
hash: -3385027163081094626
|
|
81
176
|
requirements: []
|
|
82
177
|
rubyforge_project:
|
|
83
178
|
rubygems_version: 1.8.24
|
|
@@ -88,5 +183,4 @@ summary: IP address - integer conversion and activerecord support for ip attribu
|
|
|
88
183
|
test_files:
|
|
89
184
|
- spec/ip2int_spec.rb
|
|
90
185
|
- spec/ip_address_attribute_spec.rb
|
|
91
|
-
- spec/ip_address_attribute_spec.rb~
|
|
92
186
|
- spec/spec_helper.rb
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'ip_as_int'
|
|
3
|
-
require 'active_model'
|
|
4
|
-
|
|
5
|
-
describe ::IpAsInt::IpAddressAttribute do
|
|
6
|
-
|
|
7
|
-
def model
|
|
8
|
-
@model
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
shared_examples_for "accessors and validation" do
|
|
12
|
-
|
|
13
|
-
context "with validation" do
|
|
14
|
-
before :each do
|
|
15
|
-
@model = model_class(:ip).new
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it "defines attribute writer for ip_address, stores value as integer" do
|
|
19
|
-
model.ip = '192.168.0.1'
|
|
20
|
-
model.read_attribute(:ip).should == 3232235521
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it "defines attribute reader for ip_address, converts value from integer" do
|
|
24
|
-
model.write_attribute(:ip, 3232235521)
|
|
25
|
-
model.ip.should == '192.168.0.1'
|
|
26
|
-
model.should be_valid
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
it "nil preserved" do
|
|
30
|
-
model.ip = nil
|
|
31
|
-
model.read_attribute(:ip).should == nil
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it "invalid input preserved" do
|
|
35
|
-
model.ip = "invalid"
|
|
36
|
-
model.read_attribute(:ip).should == nil
|
|
37
|
-
model.ip.should == "invalid"
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it "validates correctly" do
|
|
41
|
-
model.ip = "invalid"
|
|
42
|
-
model.should_not be_valid
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
it "validates correctly even if underlying change" do
|
|
46
|
-
model.ip = "invalid"
|
|
47
|
-
model.write_attribute(:ip, 3232235521)
|
|
48
|
-
model.should_not be_valid
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
it "invalid on nil IP" do
|
|
52
|
-
model.ip = nil
|
|
53
|
-
model.should_not be_valid
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
context "without validation" do
|
|
59
|
-
before :each do
|
|
60
|
-
@model = model_class(:ip, :validation => false).new
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
it "valid on invalid IP" do
|
|
64
|
-
model.ip = "invalid"
|
|
65
|
-
model.should be_valid
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
it "valid on valid IP" do
|
|
69
|
-
model.ip = '192.168.0.1'
|
|
70
|
-
model.should be_valid
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
end
|
|
74
|
-
context "with validation with arguments" do
|
|
75
|
-
|
|
76
|
-
before :each do
|
|
77
|
-
@model = model_class(:ip, :validation => { :allow_nil => true }).new
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
it "invalid on invalid IP" do
|
|
81
|
-
model.ip = "invalid"
|
|
82
|
-
model.should_not be_valid
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
it "valid on valid IP" do
|
|
86
|
-
model.ip = '192.168.0.1'
|
|
87
|
-
model.should be_valid
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
it "valid on nil IP" do
|
|
91
|
-
model.ip = nil
|
|
92
|
-
model.should be_valid
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
context "activemodel integration" do
|
|
99
|
-
|
|
100
|
-
def model_class(*attrs)
|
|
101
|
-
Class.new do
|
|
102
|
-
include ActiveModel::Validations
|
|
103
|
-
include ::IpAsInt::IpAddressAttribute
|
|
104
|
-
ip_address(*attrs)
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
include_examples "accessors and validations"
|
|
109
|
-
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
end
|