ip_as_int 0.0.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.
- data/.env +9 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rvmrc +2 -0
- data/Gemfile +14 -0
- data/LICENSE +22 -0
- data/README.md +102 -0
- data/Rakefile +5 -0
- data/TODO.md +1 -0
- data/ip_as_int.gemspec +21 -0
- data/lib/ip_as_int/ip2int.rb +27 -0
- data/lib/ip_as_int/ip_address_attribute.rb +68 -0
- data/lib/ip_as_int/ip_address_validator.rb +13 -0
- data/lib/ip_as_int/version.rb +3 -0
- data/lib/ip_as_int.rb +9 -0
- data/spec/ip2int_spec.rb +26 -0
- data/spec/ip_address_attribute_spec.rb +167 -0
- data/spec/ip_address_attribute_spec.rb~ +112 -0
- data/spec/spec_helper.rb +17 -0
- metadata +92 -0
data/.env
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
MAGICK_HOME="/usr/local/ImageMagick" && export
|
2
|
+
PATH=$MAGICK_HOME/bin:$PATH && export
|
3
|
+
DYLD_LIBRARY_PATH=$MAGICK_HOME/lib:$DYLD_LIBRARY_PATH && export
|
4
|
+
|
5
|
+
MYSQL_HOME="/usr/local/mysql" && export
|
6
|
+
PATH=$MYSQL_HOME/bin:$PATH && export
|
7
|
+
DYLD_LIBRARY_PATH=$MYSQL_HOME/lib:$DYLD_LIBRARY_PATH && export
|
8
|
+
|
9
|
+
PATH=/usr/local/git/bin/:$PATH && export
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in ip_as_int.gemspec
|
4
|
+
gemspec
|
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/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 zelig
|
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,102 @@
|
|
1
|
+
# IpAsInt
|
2
|
+
|
3
|
+
IP address - integer conversion and activerecord support for ip attribute as integer column.
|
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
|
+
## Usage
|
20
|
+
|
21
|
+
### IP address - integer conversion
|
22
|
+
|
23
|
+
>> require 'ip_as_int'
|
24
|
+
=> true
|
25
|
+
>> ip_string = '192.168.0.1'
|
26
|
+
=> "192.168.0.1"
|
27
|
+
>> ip_int = IpAsInt.ip2int(ip_string)
|
28
|
+
=> 3232235521
|
29
|
+
>> ip_string = IpAsInt.int2ip(ip_int)
|
30
|
+
=> "192.168.0.1"
|
31
|
+
|
32
|
+
Exceptions on invalid IP address
|
33
|
+
|
34
|
+
>> IpAsInt.ip2int('192.168.0')
|
35
|
+
ArgumentError: Invalid IP: need 4 parts
|
36
|
+
>> IpAsInt.ip2int('192.168.0.s')
|
37
|
+
ArgumentError: Invalid IP: illegal format
|
38
|
+
>> IpAsInt.ip2int('192.168.0.256')
|
39
|
+
ArgumentError: Invalid IP: integer out of range
|
40
|
+
|
41
|
+
### ActiveRecord IP address attribute as integer column
|
42
|
+
|
43
|
+
class Model < ActiveRecord::Base
|
44
|
+
include IpAsInt::IpAddressAttribute
|
45
|
+
ip_address :ip # :ip as integer column
|
46
|
+
search_methods :ip # to allow squeel to do Model.search(:ip => '192.168.0.1')
|
47
|
+
end
|
48
|
+
|
49
|
+
Attribute methods are defined to let the ip attribute assigned a string, conversion to integer column is done automatically.
|
50
|
+
|
51
|
+
>> m = Model.create(:ip => '192.168.0.1')
|
52
|
+
=> #<Model id: 1, ip: 3232235521>
|
53
|
+
>> m.valid?
|
54
|
+
=> true
|
55
|
+
>> m.reload.ip
|
56
|
+
=> "192.168.0.1"
|
57
|
+
|
58
|
+
a scope to retrieve IP by string is also provided unless `scope: false` option is given.
|
59
|
+
|
60
|
+
>> Model.create(
|
61
|
+
?> [{ :ip => '192.168.0.1' },
|
62
|
+
?> { :ip => '192.168.0.2' },
|
63
|
+
?> { :ip => '192.168.0.3' }
|
64
|
+
>> ])
|
65
|
+
=> [#<Model id: 3, ip: 3232235521>, #<Model id: 4, ip: 3232235522>, #<Model id: 5, ip: 3232235523>]
|
66
|
+
>> Model.ip('192.168.0.1', '192.168.0.2')
|
67
|
+
=> [#<Model id: 3, ip: 3232235521>, #<Model id: 4, ip: 3232235522>]
|
68
|
+
|
69
|
+
a validation of the ip address is set up unless `validation: false` option is given
|
70
|
+
|
71
|
+
>> Model.create!(:ip => '192.168.0')
|
72
|
+
ActiveRecord::RecordInvalid: Validation failed: Ip has to be a valid IP address
|
73
|
+
|
74
|
+
## Further use
|
75
|
+
|
76
|
+
In fact, you can also use the `IpAsInt::IpAddressAttribute` module with active model only.
|
77
|
+
|
78
|
+
class Model
|
79
|
+
include ActiveModel::Validations
|
80
|
+
include ::IpAsInt::IpAddressAttribute
|
81
|
+
ip_address :ip
|
82
|
+
end
|
83
|
+
|
84
|
+
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
|
+
|
86
|
+
class Model
|
87
|
+
# :ip string column
|
88
|
+
validates :ip, :ip_address => { :allow_blank => true }
|
89
|
+
end
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
1. Fork it
|
94
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
95
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
96
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
97
|
+
5. Create new Pull Request
|
98
|
+
|
99
|
+
## Credits
|
100
|
+
|
101
|
+
- http://basic70tech.wordpress.com/2007/04/13/32-bit-ip-address-to-dotted-notation-in-ruby/
|
102
|
+
- http://norbauer.com/notebooks/code/notes/storing-ip-addresses-as-integers
|
data/Rakefile
ADDED
data/TODO.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
nothing?
|
data/ip_as_int.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/ip_as_int/version', __FILE__)
|
3
|
+
|
4
|
+
DESC = %q{IP address - integer conversion and activerecord support for ip attribute as integer column}
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.authors = ["zelig"]
|
8
|
+
gem.email = ["viktor.tron@gmail.com"]
|
9
|
+
gem.description = DESC
|
10
|
+
gem.summary = DESC
|
11
|
+
gem.homepage = ""
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split($\)
|
14
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
|
+
gem.name = "ip_as_int"
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.version = IpAsInt::VERSION
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module IpAsInt::Ip2int
|
2
|
+
|
3
|
+
def ip2int(ip_string)
|
4
|
+
ip2a(ip_string).pack('C*').unpack('N').first
|
5
|
+
#ip_string.split('.').inject(0) { |total,value| (total << 8 ) + value.to_i }
|
6
|
+
end
|
7
|
+
|
8
|
+
def int2ip(ip_int)
|
9
|
+
if ip_int
|
10
|
+
[ip_int].pack('N').unpack('C*').join('.')
|
11
|
+
#[24, 16, 8, 0].collect {|b| (address >> b) & 255}.join('.')IpAsInt::
|
12
|
+
else
|
13
|
+
""
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def ip2a(ip_string)
|
18
|
+
ip_a = ip_string.split('.')
|
19
|
+
raise(ArgumentError, "Invalid IP: need 4 parts") unless ip_a.length == 4
|
20
|
+
rexp = /^\d+$/
|
21
|
+
raise(ArgumentError, "Invalid IP: illegal format") unless ip_a.all? { |x| rexp.match(x) }
|
22
|
+
ip_a = ip_a.map(&:to_i)
|
23
|
+
raise(ArgumentError, "Invalid IP: integer out of range") unless ip_a.all? { |x| (0..255).include? x }
|
24
|
+
ip_a
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
#require 'activemodel/validations'
|
2
|
+
module IpAsInt
|
3
|
+
module IpAddressAttribute
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend IpAddressAttributeClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module IpAddressAttributeClassMethods
|
10
|
+
|
11
|
+
def ip_address(*attrs)
|
12
|
+
|
13
|
+
options = attrs.extract_options!
|
14
|
+
options = { :scope => true, :validation => true }.merge(options)
|
15
|
+
|
16
|
+
unless new.respond_to?(:read_attribute)
|
17
|
+
define_method :read_attribute do |attr|
|
18
|
+
instance_variable_get("@#{attr}_raw")
|
19
|
+
end
|
20
|
+
define_method :write_attribute do |attr,val|
|
21
|
+
instance_variable_set("@#{attr}_raw", val)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
attrs.each do |attr|
|
26
|
+
|
27
|
+
define_method "#{attr}=" do |ip_string|
|
28
|
+
instance_variable_set("@#{attr}", ip_string)
|
29
|
+
ip_int = begin
|
30
|
+
if ip_string
|
31
|
+
instance_variable_set("@#{attr}_invalid",false)
|
32
|
+
::IpAsInt.ip2int(ip_string)
|
33
|
+
end
|
34
|
+
rescue ArgumentError
|
35
|
+
instance_variable_set("@#{attr}_invalid",true)
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
write_attribute attr, ip_int
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method attr do
|
42
|
+
if ip_string = instance_variable_get("@#{attr}")
|
43
|
+
ip_string
|
44
|
+
else
|
45
|
+
ip_int = read_attribute(attr)
|
46
|
+
instance_variable_set("@#{attr}",::IpAsInt.int2ip(ip_int)) if ip_int
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
if options[:scope] && respond_to?(:where)
|
53
|
+
attrs.each do |attr|
|
54
|
+
scope attr, (lambda { |*ip_strings|
|
55
|
+
ip_int = ip_strings.map { |ip_string| ::IpAsInt.ip2int(ip_string) }
|
56
|
+
where(attr => ip_int)
|
57
|
+
})
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
if options[:validation]
|
62
|
+
validates(*(attrs << { :ip_address => options[:validation] }))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_model/validator'
|
2
|
+
class IpAddressValidator < ActiveModel::EachValidator
|
3
|
+
def validate_each(record, attr, value)
|
4
|
+
invalid = instance_variable_get("@#{attr}_invalid")
|
5
|
+
invalid = begin
|
6
|
+
::IpAsInt.ip2a(value)
|
7
|
+
false
|
8
|
+
rescue
|
9
|
+
true
|
10
|
+
end if invalid.nil?
|
11
|
+
record.errors.add(attr,"has to be a valid IP address") if invalid
|
12
|
+
end
|
13
|
+
end
|
data/lib/ip_as_int.rb
ADDED
data/spec/ip2int_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ip_as_int'
|
3
|
+
|
4
|
+
describe ::IpAsInt do
|
5
|
+
|
6
|
+
it "converts ip to int correctly" do
|
7
|
+
IpAsInt.ip2int('192.168.0.1').should == 3232235521
|
8
|
+
end
|
9
|
+
|
10
|
+
it "converts ip to int correctly" do
|
11
|
+
IpAsInt.int2ip(3232235521).should == '192.168.0.1'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "throws ArgumentError if ip address is invalid" do
|
15
|
+
lambda { IpAsInt.ip2int('192.168.0.OI') }.should raise_error(ArgumentError, "Invalid IP: illegal format")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "throws ArgumentError if ip address is invalid" do
|
19
|
+
lambda { IpAsInt.ip2int('192.168.0.') }.should raise_error(ArgumentError, "Invalid IP: need 4 parts")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "throws ArgumentError if ip address is invalid" do
|
23
|
+
lambda { IpAsInt.ip2int('192.168.0.257') }.should raise_error(ArgumentError, "Invalid IP: integer out of range")
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ip_as_int'
|
3
|
+
require 'active_model'
|
4
|
+
require 'active_record'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
describe ::IpAsInt::IpAddressAttribute do
|
8
|
+
|
9
|
+
def model
|
10
|
+
@model
|
11
|
+
end
|
12
|
+
|
13
|
+
shared_examples_for "accessors and validation" do
|
14
|
+
|
15
|
+
context "with validation" do
|
16
|
+
before :each do
|
17
|
+
@model = model_class(:ip).new
|
18
|
+
end
|
19
|
+
|
20
|
+
it "defines attribute writer for ip_address, stores value as integer" do
|
21
|
+
model.ip = '192.168.0.1'
|
22
|
+
model.read_attribute(:ip).should == 3232235521
|
23
|
+
end
|
24
|
+
|
25
|
+
it "defines attribute reader for ip_address, converts value from integer" do
|
26
|
+
model.write_attribute(:ip, 3232235521)
|
27
|
+
model.ip.should == '192.168.0.1'
|
28
|
+
model.should be_valid
|
29
|
+
end
|
30
|
+
|
31
|
+
it "nil preserved" do
|
32
|
+
model.ip = nil
|
33
|
+
model.read_attribute(:ip).should == nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "invalid input preserved" do
|
37
|
+
model.ip = "invalid"
|
38
|
+
model.read_attribute(:ip).should == nil
|
39
|
+
model.ip.should == "invalid"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "validates correctly" do
|
43
|
+
model.ip = "invalid"
|
44
|
+
model.should_not be_valid
|
45
|
+
end
|
46
|
+
|
47
|
+
it "validates correctly even if underlying change" do
|
48
|
+
model.ip = "invalid"
|
49
|
+
model.write_attribute(:ip, 3232235521)
|
50
|
+
model.should_not be_valid
|
51
|
+
end
|
52
|
+
|
53
|
+
it "invalid on nil IP" do
|
54
|
+
model.ip = nil
|
55
|
+
model.should_not be_valid
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
context "without validation" do
|
61
|
+
before :each do
|
62
|
+
@model = model_class(:ip, :validation => false).new
|
63
|
+
end
|
64
|
+
|
65
|
+
it "valid on invalid IP" do
|
66
|
+
model.ip = "invalid"
|
67
|
+
model.should be_valid
|
68
|
+
end
|
69
|
+
|
70
|
+
it "valid on valid IP" do
|
71
|
+
model.ip = '192.168.0.1'
|
72
|
+
model.should be_valid
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
context "with validation with arguments" do
|
77
|
+
|
78
|
+
before :each do
|
79
|
+
@model = model_class(:ip, :validation => { :allow_nil => true }).new
|
80
|
+
end
|
81
|
+
|
82
|
+
it "invalid on invalid IP" do
|
83
|
+
model.ip = "invalid"
|
84
|
+
model.should_not be_valid
|
85
|
+
end
|
86
|
+
|
87
|
+
it "valid on valid IP" do
|
88
|
+
model.ip = '192.168.0.1'
|
89
|
+
model.should be_valid
|
90
|
+
end
|
91
|
+
|
92
|
+
it "valid on nil IP" do
|
93
|
+
model.ip = nil
|
94
|
+
model.should be_valid
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "activemodel integration" do
|
101
|
+
|
102
|
+
def model_class(*attrs)
|
103
|
+
Class.new do
|
104
|
+
include ActiveModel::Validations
|
105
|
+
include ::IpAsInt::IpAddressAttribute
|
106
|
+
ip_address(*attrs)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
include_examples "accessors and validation"
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
context "activerecord integration" do
|
115
|
+
|
116
|
+
ActiveRecord::Base.establish_connection({'adapter' => 'sqlite3', 'database' => ":memory:" })
|
117
|
+
ActiveRecord::Base.logger = Logger.new("#{File.dirname(__FILE__)}/../../active_record.log")
|
118
|
+
|
119
|
+
def model_class(*attrs)
|
120
|
+
ActiveRecord::Base.connection.create_table(:testmodels, :force=>true) do |t|
|
121
|
+
attrs.each do |attr|
|
122
|
+
t.integer attr unless attr.is_a?(Hash)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
Class.new(ActiveRecord::Base) do
|
126
|
+
self.table_name = 'testmodels'
|
127
|
+
include ::IpAsInt::IpAddressAttribute
|
128
|
+
ip_address(*attrs)
|
129
|
+
def write_attribute(*)
|
130
|
+
super
|
131
|
+
end
|
132
|
+
def read_attribute(*)
|
133
|
+
super
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
include_examples "accessors and validation"
|
139
|
+
|
140
|
+
context "persistence and scoping" do
|
141
|
+
|
142
|
+
before :each do
|
143
|
+
@model_class = model_class(:ip, :validation => { :allow_nil => true })
|
144
|
+
@model = @model_class.new
|
145
|
+
end
|
146
|
+
|
147
|
+
it "survives persistence in db" do
|
148
|
+
model.ip = '192.168.0.1'
|
149
|
+
model.save.should be_true
|
150
|
+
model.reload.ip.should == '192.168.0.1'
|
151
|
+
end
|
152
|
+
|
153
|
+
it "supports scope for retrieval from db by ip strings" do
|
154
|
+
models = @model_class.create(
|
155
|
+
[{ :ip => '192.168.0.1' },
|
156
|
+
{ :ip => '192.168.0.2' },
|
157
|
+
{ :ip => '192.168.0.3' }
|
158
|
+
])
|
159
|
+
@model_class.ip('192.168.0.1', '192.168.0.2').should == models[0..1]
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
end
|
@@ -0,0 +1,112 @@
|
|
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
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
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
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ip_as_int
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- zelig
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-11 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
|
+
description: IP address - integer conversion and activerecord support for ip attribute
|
31
|
+
as integer column
|
32
|
+
email:
|
33
|
+
- viktor.tron@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .env
|
39
|
+
- .gitignore
|
40
|
+
- .rspec
|
41
|
+
- .rvmrc
|
42
|
+
- Gemfile
|
43
|
+
- LICENSE
|
44
|
+
- README.md
|
45
|
+
- Rakefile
|
46
|
+
- TODO.md
|
47
|
+
- ip_as_int.gemspec
|
48
|
+
- lib/ip_as_int.rb
|
49
|
+
- lib/ip_as_int/ip2int.rb
|
50
|
+
- lib/ip_as_int/ip_address_attribute.rb
|
51
|
+
- lib/ip_as_int/ip_address_validator.rb
|
52
|
+
- lib/ip_as_int/version.rb
|
53
|
+
- spec/ip2int_spec.rb
|
54
|
+
- spec/ip_address_attribute_spec.rb
|
55
|
+
- spec/ip_address_attribute_spec.rb~
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
homepage: ''
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
hash: 2770053615365876423
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: 2770053615365876423
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: IP address - integer conversion and activerecord support for ip attribute
|
87
|
+
as integer column
|
88
|
+
test_files:
|
89
|
+
- spec/ip2int_spec.rb
|
90
|
+
- spec/ip_address_attribute_spec.rb
|
91
|
+
- spec/ip_address_attribute_spec.rb~
|
92
|
+
- spec/spec_helper.rb
|