mongoid-ip 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +1 -0
- data/gemfiles/mongoid-3.1.gemfile +6 -0
- data/gemfiles/mongoid-4.0.gemfile +5 -0
- data/lib/mongoid/ip.rb +85 -0
- data/lib/mongoid/ip/concern.rb +8 -0
- data/lib/mongoid/ip/rails_admin.rb +53 -0
- data/lib/mongoid/ip/version.rb +3 -0
- data/mongoid-ip.gemspec +29 -0
- data/spec/mongoid_ip_spec.rb +36 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/support/models.rb +13 -0
- data/spec/support/mongoid.yml +6 -0
- metadata +165 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a3b46da34dd92bcd1d738a6b4f7f7e21eec4feb1
|
4
|
+
data.tar.gz: 7f3a06a015bf4db5c95e33b8a8156b2de651bd45
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7557b2096e33844be046206e25ef5f04f5aed87bb0b4b0a16e6d4ae251b2740dd92ca1736f0afe5230569169baf811ed5e5f674038f40de71cd8af6b9358ef03
|
7
|
+
data.tar.gz: e031d2623b0274213ef8e845aa0ac170cdbf8edc8a2f119cb9b6b301781644dcea257582d514d53052efbddc4cade67f54a1ebc0b14bdc1be902688efc4bf229
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
mongoid_ip
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 glebtv
|
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,35 @@
|
|
1
|
+
# Mongoid::Ip
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/rs-pro/mongoid-ip.svg)](https://travis-ci.org/rs-pro/mongoid-ip)
|
4
|
+
|
5
|
+
Storing IP in MongoDB done right - as integers
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'mongoid-ip'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mongoid-ip
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Do:
|
24
|
+
|
25
|
+
field :ip, type: Mongoid::Ip
|
26
|
+
|
27
|
+
And than just use it as you would a regular text string (only change is sort order).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it ( http://github.com/<my-github-username>/mongoid-ip/fork )
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mongoid/ip.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "mongoid/ip/version"
|
2
|
+
require "ipaddr"
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
class Ip
|
6
|
+
def initialize(data)
|
7
|
+
@data = data
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
@data.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
'"' + to_s + '"'
|
16
|
+
end
|
17
|
+
|
18
|
+
def ==(other)
|
19
|
+
if other.class == self.class
|
20
|
+
other.to_s == to_s
|
21
|
+
elsif other.class == String
|
22
|
+
@data == other
|
23
|
+
else
|
24
|
+
false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def coerce(something)
|
29
|
+
[self, something]
|
30
|
+
end
|
31
|
+
|
32
|
+
def mongoize
|
33
|
+
IPAddr.new(@data, Socket::AF_INET).to_i
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def method_missing(name, *args, &block)
|
39
|
+
@data.send(name, *args, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
class << self
|
43
|
+
|
44
|
+
# Get the object as it was stored in the database, and instantiate
|
45
|
+
# this custom class from it.
|
46
|
+
def demongoize(object)
|
47
|
+
if object.is_a?(Mongoid::Ip)
|
48
|
+
object.to_s
|
49
|
+
elsif object.is_a?(Fixnum)
|
50
|
+
IPAddr.new(object, Socket::AF_INET).to_s
|
51
|
+
else
|
52
|
+
object
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Takes any possible object and converts it to how it would be
|
57
|
+
# stored in the database.
|
58
|
+
def mongoize(object)
|
59
|
+
case
|
60
|
+
when object.is_a?(Mongoid::Ip) then object.mongoize
|
61
|
+
when object.is_a?(Fixnum) then object
|
62
|
+
when object.is_a?(String) then Mongoid::Ip.new(object).mongoize
|
63
|
+
else object
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Converts the object that was supplied to a criteria and converts it
|
68
|
+
# into a database friendly form.
|
69
|
+
def evolve(object)
|
70
|
+
case object
|
71
|
+
when Mongoid::Ip then object.mongoize
|
72
|
+
when Fixnum then object
|
73
|
+
when String then Mongoid::Ip.new(object).mongoize
|
74
|
+
else object
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
require "mongoid/ip/concern"
|
82
|
+
|
83
|
+
if Object.const_defined?("RailsAdmin")
|
84
|
+
require "mongoid/ip/rails_admin"
|
85
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
|
2
|
+
require 'rails_admin/adapters/mongoid'
|
3
|
+
begin
|
4
|
+
require 'rails_admin/adapters/mongoid/property'
|
5
|
+
rescue Exception => e
|
6
|
+
end
|
7
|
+
|
8
|
+
module RailsAdmin
|
9
|
+
module Adapters
|
10
|
+
module Mongoid
|
11
|
+
class Property
|
12
|
+
alias_method :type_without_mongoid_ip, :type
|
13
|
+
def type
|
14
|
+
if property.type.to_s == 'Mongoid::Ip'
|
15
|
+
:mongoid_ip
|
16
|
+
else
|
17
|
+
type_without_mongoid_ip
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'rails_admin/config/fields/types/text'
|
26
|
+
module RailsAdmin
|
27
|
+
module Config
|
28
|
+
module Fields
|
29
|
+
module Types
|
30
|
+
class MongoidIp < RailsAdmin::Config::Fields::Types::Text
|
31
|
+
# Register field type for the type loader
|
32
|
+
RailsAdmin::Config::Fields::Types::register(self)
|
33
|
+
|
34
|
+
register_instance_option :pretty_value do
|
35
|
+
if value.respond_to?(:data)
|
36
|
+
IPAddr.new(value.data, Socket::AF_INET).to_s
|
37
|
+
else
|
38
|
+
value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
register_instance_option :formatted_value do
|
43
|
+
if value.respond_to?(:data)
|
44
|
+
IPAddr.new(value.data, Socket::AF_INET).to_s
|
45
|
+
else
|
46
|
+
value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/mongoid-ip.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid/ip/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid-ip"
|
8
|
+
spec.version = MongoidIp::VERSION
|
9
|
+
spec.authors = ["glebtv"]
|
10
|
+
spec.email = ["glebtv@gmail.com"]
|
11
|
+
spec.summary = %q{Storing IP in MongoDB done right - as integers}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = "https://github.com/rs-pro/mongoid-ip"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'simplecov'
|
26
|
+
spec.add_development_dependency 'database_cleaner'
|
27
|
+
spec.add_development_dependency 'glebtv-mongoid-rspec'
|
28
|
+
spec.add_development_dependency 'ffaker'
|
29
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Mongoid::Ip do
|
6
|
+
it 'correctly stores data' do
|
7
|
+
100.times do
|
8
|
+
text = Faker::Internet.ip_v4_address
|
9
|
+
c = Dummy.new(ip: text)
|
10
|
+
c.ip.should eq text
|
11
|
+
c.save.should be_true
|
12
|
+
c.ip.should eq text
|
13
|
+
c.read_attribute(:i).should eq IPAddr.new(text, Socket::AF_INET).to_i
|
14
|
+
Dummy.find(c.id).ip.should eq text
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'correctly migrates data from string field' do
|
19
|
+
text = Faker::Internet.ip_v4_address
|
20
|
+
c = Dummy.new(migrate_test: text)
|
21
|
+
c.save.should be_true
|
22
|
+
Migrated.first.migrate_test.should eq text
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'correctly looks up string in DB' do
|
26
|
+
text = Faker::Internet.ip_v4_address
|
27
|
+
c = Dummy.new(ip: text)
|
28
|
+
c.ip.should eq text
|
29
|
+
c.save.should be_true
|
30
|
+
c.ip.should eq text
|
31
|
+
|
32
|
+
f = Dummy.where(ip: text).first
|
33
|
+
f.should_not be_nil
|
34
|
+
f.ip.should eq text
|
35
|
+
end
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
|
7
|
+
require 'simplecov'
|
8
|
+
SimpleCov.start
|
9
|
+
|
10
|
+
require 'bundler/setup'
|
11
|
+
require 'mongoid'
|
12
|
+
require 'database_cleaner'
|
13
|
+
require 'mongoid-rspec'
|
14
|
+
require 'ffaker'
|
15
|
+
require 'mongoid/ip'
|
16
|
+
|
17
|
+
# Requires supporting files with custom matchers and macros, etc,
|
18
|
+
# in ./support/ and its subdirectories.
|
19
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
20
|
+
|
21
|
+
Mongoid.configure do |config|
|
22
|
+
ENV["MONGOID_ENV"] = "test"
|
23
|
+
Mongoid.load!("spec/support/mongoid.yml")
|
24
|
+
end
|
25
|
+
|
26
|
+
RSpec.configure do |config|
|
27
|
+
config.before :suite do
|
28
|
+
DatabaseCleaner.strategy = :truncation
|
29
|
+
end
|
30
|
+
config.after :each do
|
31
|
+
DatabaseCleaner.clean
|
32
|
+
end
|
33
|
+
config.include Mongoid::Matchers
|
34
|
+
config.mock_with :rspec
|
35
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Dummy
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
include StoreIp
|
5
|
+
field :i1, type: Mongoid::Ip
|
6
|
+
field :migrate_test, type: String
|
7
|
+
end
|
8
|
+
|
9
|
+
class Migrated
|
10
|
+
include Mongoid::Document
|
11
|
+
store_in collection: 'dummies'
|
12
|
+
field :migrate_test, type: Mongoid::Ip
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-ip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- glebtv
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: database_cleaner
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: glebtv-mongoid-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: ffaker
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: ''
|
112
|
+
email:
|
113
|
+
- glebtv@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".ruby-gemset"
|
120
|
+
- ".ruby-version"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- gemfiles/mongoid-3.1.gemfile
|
127
|
+
- gemfiles/mongoid-4.0.gemfile
|
128
|
+
- lib/mongoid/ip.rb
|
129
|
+
- lib/mongoid/ip/concern.rb
|
130
|
+
- lib/mongoid/ip/rails_admin.rb
|
131
|
+
- lib/mongoid/ip/version.rb
|
132
|
+
- mongoid-ip.gemspec
|
133
|
+
- spec/mongoid_ip_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/support/models.rb
|
136
|
+
- spec/support/mongoid.yml
|
137
|
+
homepage: https://github.com/rs-pro/mongoid-ip
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
requirements: []
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 2.2.2
|
158
|
+
signing_key:
|
159
|
+
specification_version: 4
|
160
|
+
summary: Storing IP in MongoDB done right - as integers
|
161
|
+
test_files:
|
162
|
+
- spec/mongoid_ip_spec.rb
|
163
|
+
- spec/spec_helper.rb
|
164
|
+
- spec/support/models.rb
|
165
|
+
- spec/support/mongoid.yml
|