composed_of_ipaddr 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/composed_of_ipaddr.rb +43 -0
- data/test/composed_of_ipaddr_test.rb +28 -0
- data/test/lib/test_helper.rb +4 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fde8672a9f5a85fa9f0e142fd618f79f46170262
|
4
|
+
data.tar.gz: 60c9a88bd0da75f71d64f735728c67a942eeb8ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d833ebe182629d015a294464d3b01f8c6d77d5e285f5af25da39192610daa43ddc22e30cbbc0b01e1809a1664a4fb2e8183c9c19c1157e5a018dc8375d5ab26
|
7
|
+
data.tar.gz: 5736135a4b95e7e05d31c6cd444b7f768fddd1d50cdb14fabfd46d6e8b082f4ff527a2a264f8d21206b64e029980711c4d0cc9ed05b578a948c217c5fa3e555b
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'concerning'
|
3
|
+
require 'ipaddr'
|
4
|
+
|
5
|
+
# Store IPs as 4-byte integers in the db. Saves space, indexes well.
|
6
|
+
# Pardon these ugly implementation contortions. In our code, just work
|
7
|
+
# with :ipaddr using '1.2.3.4' strings and expect implicit coercion.
|
8
|
+
#
|
9
|
+
# Expects a column named 'ipv4' with type 'int unsigned'
|
10
|
+
concern :ComposedOfIpaddr do
|
11
|
+
included do
|
12
|
+
composed_of :ipaddr, :class_name => 'IPAddr', :mapping => %w(ipv4 to_i), :allow_nil => true,
|
13
|
+
:constructor => lambda { |ip| ::ComposedOfIpaddr.convert_to_ipaddr ip },
|
14
|
+
:converter => lambda { |ip| ::ComposedOfIpaddr.convert_to_ipaddr ip }
|
15
|
+
|
16
|
+
scope :by_ipaddr, lambda { |ip| where :ipv4 => ::ComposedOfIpaddr.convert_to_ipv4(ip) if ip }
|
17
|
+
end
|
18
|
+
|
19
|
+
# 0.0.0.0
|
20
|
+
def global?
|
21
|
+
ipv4.try :zero?
|
22
|
+
end
|
23
|
+
|
24
|
+
def per_ip?
|
25
|
+
!global?
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.convert_to_ipaddr(ip)
|
29
|
+
case ip
|
30
|
+
when IPAddr; ip
|
31
|
+
when Integer; IPAddr.new(ip, Socket::AF_INET)
|
32
|
+
else IPAddr.new(ip)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.convert_to_ipv4(ip)
|
37
|
+
if ip.respond_to? :map
|
38
|
+
ip.map { |i| convert_to_ipaddr i }.map &:to_i
|
39
|
+
else
|
40
|
+
convert_to_ipaddr(ip).to_i
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ComposedOfIpaddrTest < ActiveSupport::TestCase
|
4
|
+
class Model < ActiveRecord::Base
|
5
|
+
establish_connection :adapter => 'sqlite3', :database => 'test.sqlite3'
|
6
|
+
|
7
|
+
if !connection.table_exists?(table_name)
|
8
|
+
connection.create_table(table_name) { |t| t.column :ipv4, 'int unsigned' }
|
9
|
+
end
|
10
|
+
|
11
|
+
include ComposedOfIpaddr
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'converts and constructs' do
|
15
|
+
ip = IPAddr.new('1.2.3.4')
|
16
|
+
model = Model.new(:ipaddr => ip.to_s)
|
17
|
+
assert_equal ip, model.ipaddr
|
18
|
+
assert_equal ip.to_i, model.attributes['ipv4']
|
19
|
+
|
20
|
+
model.ipaddr = ip.to_i
|
21
|
+
assert_equal ip, model.ipaddr
|
22
|
+
|
23
|
+
model.save!
|
24
|
+
model.reload
|
25
|
+
assert_equal ip.to_i, model.attributes['ipv4']
|
26
|
+
assert_equal ip, model.ipaddr
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: composed_of_ipaddr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Basecamp
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: concerning
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.1'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.3'
|
83
|
+
description:
|
84
|
+
email: support@basecamp.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- "./lib/composed_of_ipaddr.rb"
|
90
|
+
- "./test/composed_of_ipaddr_test.rb"
|
91
|
+
- "./test/lib/test_helper.rb"
|
92
|
+
homepage: https://github.com/basecamp/composed_of_ipaddr
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: 1.8.7
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.2.0
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Compose an IPAddr from an underlying 4-byte integer
|
116
|
+
test_files:
|
117
|
+
- "./test/composed_of_ipaddr_test.rb"
|
118
|
+
- "./test/lib/test_helper.rb"
|