enumize_mongoid 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +112 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/enumize_mongoid.gemspec +24 -0
- data/lib/enumize_mongoid/version.rb +3 -0
- data/lib/enumize_mongoid.rb +86 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b94cd31450d34f8fdc6a003185b9e48dd0e36d47
|
4
|
+
data.tar.gz: 3373fb392dab45a699ea4dc92f5c4e9b7bed050c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 07141f242c59d805cf2f772ac01247c85e54bef658a63b07cd64e4f278ffad0ad227994ef07e2c39f846941abeaac68c5f1da717707a19d5ded191503671a4ee
|
7
|
+
data.tar.gz: 171a13e760de206817f68a5925d9eb9be3a256a59f9f5f76b4cd55f4e7c846e7e0d32fb3b663f576edad00eb61d9a3498598de19075adfaa8d73206b789bc5f9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# EnumizeMongoid
|
2
|
+
|
3
|
+
EnumizeMongoid is a gem aimed for Mongoid users, that lets you create your own classes/types representing/encapsulating an enum. There are other great gems out there dealing with enums for Mongoid (like [simple_enum](https://github.com/lwe/simple_enum)) but this one is approaching the problem from another angle.
|
4
|
+
|
5
|
+
Using this gem you are required to create your own classes for your enum types, but on the other hand you will NOT face inconsistencies like this (using simple_enum):
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
doc = Document.first
|
9
|
+
p Document.where(status: doc.status).count
|
10
|
+
# 0 - why? because doc.status returns a symbol, but the :status field is actually an Integer
|
11
|
+
```
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'enumize_mongoid'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install enumize_mongoid
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Include `EnumizeMongoid::Field` into your class representing the enum, and call `enumize` with the appropriate values:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Status
|
35
|
+
include EnumizeMongoid::Field
|
36
|
+
|
37
|
+
enumize([:bouncing, :still], create_constants: true)
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Set it as the field's type you want to use an enum for:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class RedBall
|
45
|
+
include Mongoid::Document
|
46
|
+
|
47
|
+
field :status, type: Status
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
After that you can use it in various flexible ways:
|
52
|
+
|
53
|
+
### Document creation
|
54
|
+
```ruby
|
55
|
+
RedBall.create(status: :bouncing) # value as symbol
|
56
|
+
RedBall.create(status: Status::BOUNCING) # value as constant
|
57
|
+
RedBall.create(status: 0) # value as Integer
|
58
|
+
RedBall.create(status: Status.new(:bouncing)) # value as an instance of Status
|
59
|
+
```
|
60
|
+
|
61
|
+
### Document querying
|
62
|
+
```ruby
|
63
|
+
rb = RedBall.create(status: :bouncing)
|
64
|
+
|
65
|
+
RedBall.where(status: :bouncing).count # 1
|
66
|
+
RedBall.where(status: Status::BOUNCING).count # 1
|
67
|
+
RedBall.where(status: 0).count # 1
|
68
|
+
RedBall.where(status: Status.new(:bouncing)).count # 1
|
69
|
+
```
|
70
|
+
|
71
|
+
### Comparing values
|
72
|
+
```ruby
|
73
|
+
RedBall.where(status: :bouncing).first
|
74
|
+
|
75
|
+
rb.status == :bouncing # true
|
76
|
+
rb.status == Status::BOUNCING # true
|
77
|
+
rb.status == 0 # true
|
78
|
+
rb.status == Status.new(:bouncing) # true
|
79
|
+
```
|
80
|
+
|
81
|
+
Of course this solves the problem with inconsistencies mentioned in the introduction:
|
82
|
+
```ruby
|
83
|
+
rb = RedBall.create(status: :bouncing)
|
84
|
+
p RedBall.where(status: rb.status).count
|
85
|
+
# 1 - rb.status is an instance of Status, which can be passed as part of a selector
|
86
|
+
```
|
87
|
+
|
88
|
+
#### How to use `enumize(values, create_constants: false)`
|
89
|
+
|
90
|
+
`values`:
|
91
|
+
* array of symbols: `[:bouncing, :still]` - In this case the order matters, as the index of the value will be the value saved in the database: `:bouncing` -> 0, `:still` -> 1
|
92
|
+
* hash: `{ bouncing: 2, still: 4 }` - Order does not matter, you decide what the values will be
|
93
|
+
|
94
|
+
`create_constants: true` will create:
|
95
|
+
* a `VALUES` constant: `Status::VALUES` -> `{ bouncing: 0, still: 1 }`
|
96
|
+
* a constant for each value:
|
97
|
+
* `Status::BOUNCING` -> `0`
|
98
|
+
* `Status::STILL` -> `0`
|
99
|
+
|
100
|
+
## Development
|
101
|
+
|
102
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
103
|
+
|
104
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
1. Fork it ( https://github.com/InnovativeTravel/enumize_mongoid/fork )
|
109
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
110
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
111
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
112
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "enumize_mongoid"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enumize_mongoid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enumize_mongoid"
|
8
|
+
spec.version = EnumizeMongoid::VERSION
|
9
|
+
spec.authors = ["Elod Peter"]
|
10
|
+
spec.email = ["bejmuller@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Enum Field for Mongoid}
|
13
|
+
spec.description = %q{Enum Field for Mongoid}
|
14
|
+
spec.homepage = "https://github.com/InnovativeTravel/enumize_mongoid"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_dependency "mongoid"
|
24
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "enumize_mongoid/version"
|
2
|
+
|
3
|
+
module EnumizeMongoid
|
4
|
+
module Field
|
5
|
+
def self.included(klass)
|
6
|
+
|
7
|
+
klass.instance_eval do
|
8
|
+
def enumize(values, create_constants: false)
|
9
|
+
@@value_map = create_value_map(values)
|
10
|
+
|
11
|
+
if create_constants
|
12
|
+
const_set('VALUES', @@value_map)
|
13
|
+
|
14
|
+
@@value_map.each do |key, value|
|
15
|
+
const_set(key.to_s.upcase, value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_value_map(opts)
|
21
|
+
return opts if opts.is_a? Hash
|
22
|
+
|
23
|
+
return Hash[opts.zip(0...opts.size)] if opts.is_a? Array
|
24
|
+
|
25
|
+
raise 'Not supported type'
|
26
|
+
end
|
27
|
+
|
28
|
+
def value_of(value)
|
29
|
+
@@value_map.invert[value]
|
30
|
+
end
|
31
|
+
|
32
|
+
# Get the object as it was stored in the database, and instantiate
|
33
|
+
# this custom class from it.
|
34
|
+
def demongoize(object)
|
35
|
+
new(value_of(object))
|
36
|
+
end
|
37
|
+
|
38
|
+
# Takes any possible object and converts it to how it would be
|
39
|
+
# stored in the database.
|
40
|
+
def mongoize(object)
|
41
|
+
case object
|
42
|
+
when self then object.mongoize
|
43
|
+
when Symbol then new(object).mongoize
|
44
|
+
else object
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Converts the object that was supplied to a criteria and converts it
|
49
|
+
# into a database friendly form.
|
50
|
+
def evolve(object)
|
51
|
+
case object
|
52
|
+
when self then object.mongoize
|
53
|
+
when Symbol then mongoize(object)
|
54
|
+
else object
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
klass.class_eval do
|
60
|
+
attr_reader :value
|
61
|
+
|
62
|
+
def initialize(value)
|
63
|
+
@value = value
|
64
|
+
end
|
65
|
+
|
66
|
+
def ==(obj)
|
67
|
+
case obj
|
68
|
+
when Symbol then obj == value
|
69
|
+
when self.class then obj.value == value
|
70
|
+
when Numeric then obj == @@value_map[value]
|
71
|
+
else false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Converts an object of this instance into a database friendly value.
|
76
|
+
def mongoize
|
77
|
+
@@value_map[value]
|
78
|
+
end
|
79
|
+
|
80
|
+
def to_s
|
81
|
+
"<#{self.class}|#{value}>"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumize_mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elod Peter
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-17 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: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mongoid
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Enum Field for Mongoid
|
56
|
+
email:
|
57
|
+
- bejmuller@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- enumize_mongoid.gemspec
|
71
|
+
- lib/enumize_mongoid.rb
|
72
|
+
- lib/enumize_mongoid/version.rb
|
73
|
+
homepage: https://github.com/InnovativeTravel/enumize_mongoid
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.7
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Enum Field for Mongoid
|
96
|
+
test_files: []
|