mongoid-enum 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NTJkYTBhNGI1MDQ1OTAyMDdmZDU1NGMxODFkNjI5YTBlNTczNTQwNA==
5
- data.tar.gz: !binary |-
6
- MzgxMTU4ZjJhYzA0ZTg5ZWZjY2QzOGM5YWM4N2NmNDZiODBjYThkZQ==
3
+ metadata.gz: c0380dc80b62126bdaec73b7b84f45eff8ecc607
4
+ data.tar.gz: 1ea999b331c3500038eaa3e22309ba67fd1f6b51
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZTExODM0ZDEwN2I1MGZhNzZmMjNmOGM3MjFkOGNlYWI2ZWFlNjY2NGI5YTEx
10
- ZDdiMTU2OTJjMmFmMTQzZThhODhkNmJkMDUzYTJlODI1YWExOWU0ZmU1OGYy
11
- ZTYwNmFlNDZiZGEwZGM0MmNkODNlM2IzYWViNTJlNGJjNjZmODY=
12
- data.tar.gz: !binary |-
13
- MWVkNDk1ODg1YWVjZWM5Y2U5NzQ3NmFlNjY4N2Y2N2Y2OTQyMzQ5Y2U5N2Zh
14
- MTExODgyNzQxYThiMGQxZjQwMzYzY2Q1MzFjZjIxZWE2NmMzZDE5ZmI2MDdl
15
- NDVjYmQwODBlYmJkZWViY2JjY2MyZTdhMjljMWYwYTk2NDYyODU=
6
+ metadata.gz: 63a8a27751a95d12b23fa52619810079782218ca18d72febcc6dcc1baff257d7aefe9f01eb27eb8a2918b0cac75d7de7a6c65f7c95f0e47d2124059a9b864249
7
+ data.tar.gz: 6a3d706e2115b79da8a51925a7d57ba385953b1d24c3ea196e563bb88ce24b914ce5fe08d346643747d5b02fa16c9ec7b3521e5d5d075feaabd580fc4657a85b
@@ -1,14 +1,14 @@
1
1
  script: "bundle exec rspec spec"
2
+
2
3
  language: ruby
4
+
3
5
  rvm:
4
6
  - 1.9.3
5
7
  - 2.0.0
8
+ - 2.1.0
9
+ - 2.1.1
10
+ - 2.1.2
6
11
  - jruby-19mode
7
- - rbx-19mode
8
- gemfile:
9
- - Gemfile
10
- notifications:
11
- recipients:
12
- - nicholas@bruning.com.au
12
+
13
13
  services:
14
14
  - mongodb
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Build
4
4
  Status](https://travis-ci.org/thetron/mongoid-enum.png)](https://travis-ci.org/thetron/mongoid-enum)
5
+ [![Code Climate](https://codeclimate.com/github/thetron/mongoid-enum.png)](https://codeclimate.com/github/thetron/mongoid-enum)
5
6
 
6
7
  Heavily inspired by [DHH's
7
8
  ActiveRecord::Enum](https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5), this little library is
@@ -16,25 +17,27 @@ and a few other bits-and-bobs.
16
17
 
17
18
  Add this to your Gemfile:
18
19
 
19
- gem "mongoid-enum"
20
+ ```ruby
21
+ gem "mongoid-enum"
22
+ ```
20
23
 
21
24
  And then run `bundle install`.
22
25
 
23
26
 
24
27
  # Usage
25
28
 
26
- ````
29
+ ```ruby
27
30
  class Payment
28
31
  include Mongoid::Document
29
32
  include Mongoid::Enum
30
33
 
31
- enum :status, [:pending, :approved, :declined],
34
+ enum :status, [:pending, :approved, :declined]
32
35
  end
33
- ````
36
+ ```
34
37
 
35
38
  Aaaaaaand then you get things like:
36
39
 
37
- ````
40
+ ```ruby
38
41
  payment = Payment.create
39
42
 
40
43
  payment.status
@@ -49,7 +52,7 @@ payment.pending?
49
52
  Payment.approved
50
53
  # => Mongoid::Criteria for payments with status == :approved
51
54
 
52
- ````
55
+ ```
53
56
 
54
57
  # Features
55
58
 
@@ -64,13 +67,13 @@ convenience.
64
67
  ## Accessors
65
68
 
66
69
  Your enums will get getters-and-setters with the same name. So using the
67
- 'Payment' example above:
70
+ `Payment` example above:
68
71
 
69
- ````
72
+ ```ruby
70
73
  payment.status = :declined
71
74
  payment.status
72
75
  # => :declined
73
- ````
76
+ ```
74
77
 
75
78
  And you also get bang(!) and query(?) methods for each of the values in
76
79
  your enum (see [this example](#usage).
@@ -82,10 +85,10 @@ For each enum, you'll also get a constant named after it. This is to
82
85
  help you elsewhere in your app, should you need to display, or leverage
83
86
  the list of values. Using the above example:
84
87
 
85
- ````
88
+ ```ruby
86
89
  Payment::STATUS
87
90
  # => [:pending, :approved, :declined]
88
- ````
91
+ ```
89
92
 
90
93
 
91
94
  ## Validations
@@ -99,11 +102,11 @@ disable this behaviour (see [below](#options)).
99
102
  A scope added for each of your enum's values. Using the example above,
100
103
  you'd automatically get:
101
104
 
102
- ````
105
+ ```ruby
103
106
  Payment.pending # => Mongoid::Criteria
104
107
  Payment.approved # => Mongoid::Criteria
105
108
  Payment.declined # => Mongoid::Criteria
106
- ````
109
+ ```
107
110
 
108
111
 
109
112
  # Options
@@ -114,25 +117,37 @@ If not specified, the default will be the first in your list of values
114
117
  (`:pending` in the example above). You can override this with the
115
118
  `:default` option:
116
119
 
117
- enum :roles, [:manager, :administrator], :default => ""
118
-
120
+ ```ruby
121
+ enum :roles, [:manager, :administrator], :default => ""
122
+ ```
119
123
 
120
124
  ## Multiple values
121
125
 
122
126
  Sometimes you'll need to store multiple values from your list, this
123
127
  couldn't be easier:
124
128
 
125
- enum, :roles => [:basic, :manager, :administrator], :multiple => true
129
+ ```ruby
130
+ enum :roles, [:basic, :manager, :administrator], :multiple => true
131
+
132
+ # ...
126
133
 
127
- user = User.create
128
- user.roles << :basic
129
- user.roles << :manager
130
- user.save!
134
+ user = User.create
135
+ user.roles << :basic
136
+ user.roles << :manager
137
+ user.save!
131
138
 
132
- user.manager? # => true
133
- user.administrator? # => false
134
- user.roles # => [:basic, :manager]
139
+ user.manager? # => true
140
+ user.administrator? # => false
141
+ user.roles # => [:basic, :manager]
142
+ ```
135
143
 
144
+ Since the underlying datatype for storing values is an array, if you
145
+ need to specify default(s), ensure you use an array:
146
+
147
+ ```ruby
148
+ enum :roles, [:noob, :author, :editor], :multiple => true, :default => [:author, :editor] # two defaults
149
+ enum :roles, [:noob, :author, :editor], :multiple => true, :default => [] # no default
150
+ ```
136
151
 
137
152
  ## Validations
138
153
 
@@ -141,8 +156,9 @@ your field are always from your list of options. If you need more
141
156
  complex validations, or you just want to throw caution to the wind, you
142
157
  can turn them off:
143
158
 
144
- enum :status => [:up, :down], :validate => false
145
-
159
+ ```ruby
160
+ enum :status, [:up, :down], :validate => false
161
+ ```
146
162
 
147
163
  # Issues and Feature Requests
148
164
 
@@ -5,38 +5,69 @@ module Mongoid
5
5
  module Enum
6
6
  extend ActiveSupport::Concern
7
7
  module ClassMethods
8
+
8
9
  def enum(name, values, options = {})
9
10
  field_name = :"_#{name}"
10
- const_name = name.to_s.upcase
11
- multiple = options[:multiple] || false
12
- default = options[:default].nil? && values.first || options[:default]
13
- required = options[:required].nil? || options[:required]
14
- validate = options[:validate].nil? || options[:validate]
11
+ options = default_options(values).merge(options)
15
12
 
16
- const_set const_name, values
13
+ set_values_constant name, values
17
14
 
18
- type = multiple && Array || Symbol
19
- field field_name, :type => type, :default => default
15
+ create_field field_name, options
20
16
  alias_attribute name, field_name
21
17
 
22
- if multiple && validate
23
- validates field_name, :'mongoid/enum/validators/multiple' => { :in => values, :allow_nil => !required }
18
+ create_validations field_name, values, options
19
+ define_value_scopes_and_accessors field_name, values, options
20
+ end
21
+
22
+ private
23
+ def default_options(values)
24
+ {
25
+ :multiple => false,
26
+ :default => values.first,
27
+ :required => true,
28
+ :validate => true
29
+ }
30
+ end
31
+
32
+ def set_values_constant(name, values)
33
+ const_name = name.to_s.upcase
34
+ const_set const_name, values
35
+ end
36
+
37
+ def create_field(field_name, options)
38
+ type = options[:multiple] && Array || Symbol
39
+ field field_name, :type => type, :default => options[:default]
40
+ end
41
+
42
+ def create_validations(field_name, values, options)
43
+ if options[:multiple] && options[:validate]
44
+ validates field_name, :'mongoid/enum/validators/multiple' => { :in => values, :allow_nil => !options[:required] }
24
45
  elsif validate
25
- validates field_name, :inclusion => {:in => values}, :allow_nil => !required
46
+ validates field_name, :inclusion => {:in => values}, :allow_nil => !options[:required]
26
47
  end
48
+ end
27
49
 
50
+ def define_value_scopes_and_accessors(field_name, values, options)
28
51
  values.each do |value|
29
- scope value, where(field_name => value)
52
+ scope value, ->{ where(field_name => value) }
30
53
 
31
- if multiple
32
- class_eval "def #{value}?() self.#{field_name}.include?(:#{value}) end"
33
- class_eval "def #{value}!() update_attributes! :#{field_name} => (self.#{field_name} || []) + [:#{value}] end"
54
+ if options[:multiple]
55
+ define_array_accessor(field_name, value)
34
56
  else
35
- class_eval "def #{value}?() self.#{field_name} == :#{value} end"
36
- class_eval "def #{value}!() update_attributes! :#{field_name} => :#{value} end"
57
+ define_string_accessor(field_name, value)
37
58
  end
38
59
  end
39
60
  end
61
+
62
+ def define_array_accessor(field_name, value)
63
+ class_eval "def #{value}?() self.#{field_name}.include?(:#{value}) end"
64
+ class_eval "def #{value}!() update_attributes! :#{field_name} => (self.#{field_name} || []) + [:#{value}] end"
65
+ end
66
+
67
+ def define_string_accessor(field_name, value)
68
+ class_eval "def #{value}?() self.#{field_name} == :#{value} end"
69
+ class_eval "def #{value}!() update_attributes! :#{field_name} => :#{value} end"
70
+ end
40
71
  end
41
72
  end
42
73
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Enum
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "mongoid", "~>3.1"
21
+ spec.add_runtime_dependency "mongoid", "~> 4.0"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Bruning
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-06 00:00:00.000000000 Z
11
+ date: 2014-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '3.1'
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: '3.1'
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  requirements: []
155
155
  rubyforge_project:
156
- rubygems_version: 2.1.1
156
+ rubygems_version: 2.2.2
157
157
  signing_key:
158
158
  specification_version: 4
159
159
  summary: Sweet enum sugar for your Mongoid documents