Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gitbourahlavignal
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VIGNAL Nicolas
gitbourahlavignal
Commits
4fbd9d98
Commit
4fbd9d98
authored
10 years ago
by
Gauthier Delacroix
Browse files
Options
Downloads
Patches
Plain Diff
Add Mongoid support
parent
dae93ad0
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
ipaddress.gemspec
+2
-0
2 additions, 0 deletions
ipaddress.gemspec
lib/ipaddress.rb
+1
-0
1 addition, 0 deletions
lib/ipaddress.rb
lib/ipaddress/mongoid.rb
+75
-0
75 additions, 0 deletions
lib/ipaddress/mongoid.rb
test/ipaddress/mongoid_test.rb
+76
-0
76 additions, 0 deletions
test/ipaddress/mongoid_test.rb
with
154 additions
and
0 deletions
ipaddress.gemspec
+
2
−
0
View file @
4fbd9d98
...
...
@@ -32,9 +32,11 @@ Gem::Specification.new do |s|
"lib/ipaddress/ipv4.rb"
,
"lib/ipaddress/ipv6.rb"
,
"lib/ipaddress/prefix.rb"
,
"lib/ipaddress/mongoid.rb"
,
"test/ipaddress/ipv4_test.rb"
,
"test/ipaddress/ipv6_test.rb"
,
"test/ipaddress/prefix_test.rb"
,
"test/ipaddress/mongoid_test.rb"
,
"test/ipaddress_test.rb"
,
"test/test_helper.rb"
]
...
...
This diff is collapsed.
Click to expand it.
lib/ipaddress.rb
+
1
−
0
View file @
4fbd9d98
...
...
@@ -14,6 +14,7 @@
require
'ipaddress/ipv4'
require
'ipaddress/ipv6'
require
'ipaddress/mongoid'
if
defined?
(
Mongoid
)
module
IPAddress
...
...
This diff is collapsed.
Click to expand it.
lib/ipaddress/mongoid.rb
0 → 100644
+
75
−
0
View file @
4fbd9d98
module
IPAddress
#
# Mongoid field serialization
#
# IPAddress objects are converted to String
#
# IPAddress.mongoize IPAddress.parse("172.16.10.1")
# #=> "172.16.10.1"
#
# Prefix will be removed from host adresses
#
# IPAddress.mongoize "172.16.10.1/32"
# #=> "172.16.10.1"
#
# Prefix will be kept for network addresses
#
# IPAddress.mongoize "172.16.10.1/24"
# #=> "172.16.10.1/24"
#
# IPv6 addresses will be stored uncompressed to ease DB search and sorting
#
# IPAddress.mongoize "2001:db8::8:800:200c:417a"
# #=> "2001:0db8:0000:0000:0008:0800:200c:417a"
# IPAddress.mongoize "2001:db8::8:800:200c:417a/64"
# #=> "2001:0db8:0000:0000:0008:0800:200c:417a/64"
#
# Invalid addresses will be serialized as nil
#
# IPAddress.mongoize "invalid"
# #=> nil
# IPAddress.mongoize ""
# #=> nil
# IPAddress.mongoize 1
# #=> nil
# IPAddress.mongoize nil
# #=> nil
#
def
self
.
mongoize
(
ipaddress
)
ipaddress
=
self
.
parse
(
ipaddress
)
unless
ipaddress
.
is_a?
(
IPAddress
)
if
ipaddress
.
bits
.
length
==
ipaddress
.
prefix
ipaddress
.
address
elsif
ipaddress
.
is_a?
(
IPAddress
::
IPv6
)
ipaddress
.
to_string_uncompressed
else
ipaddress
.
to_string
end
rescue
ArgumentError
nil
end
#
# Mongoid field deserialization
#
def
self
.
demongoize
(
string
)
parse
(
string
)
rescue
ArgumentError
nil
end
#
# Delegates to IPAddress.mongoize
#
def
self
.
evolve
(
ipaddress
)
mongoize
(
ipaddress
)
end
#
# Sends self object to IPAddress#mongoize
#
def
mongoize
IPAddress
.
mongoize
(
self
)
end
end
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test/ipaddress/mongoid_test.rb
0 → 100644
+
76
−
0
View file @
4fbd9d98
require
'test_helper'
require
'ipaddress/mongoid'
class
MongoidTest
<
Test
::
Unit
::
TestCase
def
setup
@valid_host4
=
"172.16.10.1"
@valid_host6
=
"2001:0db8:0000:0000:0008:0800:200c:417a"
@valid_host6_compressed
=
IPAddress
::
IPv6
.
compress
(
@valid_host6
)
@valid_network4
=
"
#{
@valid_host4
}
/24"
@valid_network6
=
"
#{
@valid_host6
}
/96"
@valid_network6_compressed
=
"
#{
@valid_host6_compressed
}
/96"
@host4
=
IPAddress
.
parse
(
@valid_host4
)
@host6
=
IPAddress
.
parse
(
@valid_host6
)
@network4
=
IPAddress
.
parse
(
@valid_network4
)
@network6
=
IPAddress
.
parse
(
@valid_network6
)
@invalid_values
=
[
nil
,
""
,
1
,
"invalid"
]
end
def
test_mongoize
# Instance method should be delegated to class method
assert_equal
@host4
.
mongoize
,
IPAddress
.
mongoize
(
@host4
)
assert_equal
@network4
.
mongoize
,
IPAddress
.
mongoize
(
@network4
)
# Hosts addresses should be stored without prefix
assert_equal
@valid_host4
,
IPAddress
.
mongoize
(
@host4
)
assert_equal
@valid_host6
,
IPAddress
.
mongoize
(
@host6
)
assert_equal
@valid_host4
,
IPAddress
.
mongoize
(
"
#{
@host4
}
/32"
)
assert_equal
@valid_host6
,
IPAddress
.
mongoize
(
"
#{
@host6
}
/128"
)
# Network addresses should be stored with their prefix
assert_equal
@valid_network4
,
IPAddress
.
mongoize
(
@network4
)
assert_equal
@valid_network6
,
IPAddress
.
mongoize
(
@network6
)
# IPv6 addresses should always be stored uncompressed
assert_equal
@valid_host6
,
IPAddress
.
mongoize
(
@valid_host6_compressed
)
assert_equal
@valid_network6
,
IPAddress
.
mongoize
(
@valid_network6_compressed
)
@invalid_values
.
each
do
|
invalid_value
|
# Invalid address should not raise error
assert_nothing_raised
{
IPAddress
.
mongoize
(
invalid_value
)}
# Invalid addresses should serialize to nil
assert_equal
nil
,
IPAddress
.
mongoize
(
invalid_value
)
end
end
def
test_demongoize
# Valid stored values should be loaded with expected IPAddress type
assert_instance_of
IPAddress
::
IPv4
,
IPAddress
.
demongoize
(
@valid_host4
)
assert_instance_of
IPAddress
::
IPv6
,
IPAddress
.
demongoize
(
@valid_host6
)
assert_instance_of
IPAddress
::
IPv4
,
IPAddress
.
demongoize
(
@valid_network4
)
assert_instance_of
IPAddress
::
IPv6
,
IPAddress
.
demongoize
(
@valid_network6
)
# Valid stored values should be loaded as the original IPAddress object
assert_equal
@host4
,
IPAddress
.
demongoize
(
@valid_host4
)
assert_equal
@host6
,
IPAddress
.
demongoize
(
@valid_host6
)
assert_equal
@network4
,
IPAddress
.
demongoize
(
@valid_network4
)
assert_equal
@network6
,
IPAddress
.
demongoize
(
@valid_network6
)
@invalid_values
.
each
do
|
invalid_value
|
# Invalid stored values should not raise error
assert_nothing_raised
{
IPAddress
.
demongoize
(
invalid_value
)}
# Invalid stored value should be loaded as nil
assert_equal
nil
,
IPAddress
.
demongoize
(
invalid_value
)
end
end
def
test_evolve
# evolve should delegate to mongoize
assert_equal
IPAddress
.
mongoize
(
@valid_host4
),
IPAddress
.
evolve
(
@valid_host4
)
assert_equal
IPAddress
.
mongoize
(
@valid_network4
),
IPAddress
.
evolve
(
@valid_network4
)
end
end
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment