mongoid-enum_attribute 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e0c939d3c2c2a5677c00e67b188c2f1121a74b514481a26850ae44132d8b58b
4
+ data.tar.gz: 7a81b66ee67d78377af402636a31be30594f3558103fc9b4868b5373f6aba3ee
5
+ SHA512:
6
+ metadata.gz: eb7c9892acf4df44ea84e40019a7db574fe5ce5638707d621e527ca456655d4dede81f56c9c56d8ea6bad1aebf675bf34bd89d2891512fff3759a9f2ab3033cc
7
+ data.tar.gz: b799c8b3c833c15751ae2e5f8f792232717ebea7a895d63bfbb9b9ff23cce1c478d4ffe415f83d02ce516e970fb087608419b7098dde82962906c70559717fda
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,23 @@
1
+ language: ruby
2
+ cache: bundler
3
+ script: 'bundle exec rake'
4
+ rvm:
5
+ - 2.5.0
6
+ services:
7
+ - mongodb
8
+
9
+ notifications:
10
+ email:
11
+ recipients:
12
+ - tomas.celizna@gmail.com
13
+ on_failure: change
14
+ on_success: never
15
+
16
+ matrix:
17
+ include:
18
+ - rvm: 2.3.3
19
+ env: MONGOID_VERSION=5
20
+ - rvm: 2.3.3
21
+ env: MONGOID_VERSION=6
22
+ - rvm: 2.5.0
23
+ env: MONGOID_VERSION=7
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0
4
+
5
+ * first release
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mongoid-enum_attribute.gemspec
6
+ gemspec
7
+
8
+ case version = ENV['MONGOID_VERSION'] || '~> 7.0'
9
+ when /7/ then gem 'mongoid', '~> 7.0'
10
+ when /6/ then gem 'mongoid', '~> 6.0'
11
+ when /5/ then gem 'mongoid', '~> 5.1'
12
+ else gem 'mongoid', version
13
+ end
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Mongoid::EnumAttribute
2
+
3
+ [![Build Status](https://travis-ci.org/tomasc/mongoid-enum_attribute.svg)](https://travis-ci.org/tomasc/mongoid-enum_attribute) [![Gem Version](https://badge.fury.io/rb/mongoid-enum_attribute.svg)](http://badge.fury.io/rb/mongoid-enum_attribute) [![Coverage Status](https://img.shields.io/coveralls/tomasc/mongoid-enum_attribute.svg)](https://coveralls.io/r/tomasc/mongoid-enum_attribute)
4
+
5
+ Updated and tweaked version of the no-longer-maintained [mongoid_enum](https://github.com/thetron/mongoid-enum).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mongoid-enum_attribute'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mongoid-enum_attribute
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ class Payment
27
+ include Mongoid::Document
28
+ include Mongoid::Enum
29
+
30
+ enum :status, [:pending, :approved, :declined]
31
+ end
32
+ ```
33
+
34
+ Gives you getters,
35
+
36
+ ```ruby
37
+ payment.status
38
+ # => :pending
39
+ ```
40
+
41
+ setters,
42
+
43
+ ```ruby
44
+ payment.approved!
45
+ # => :approved
46
+ ```
47
+
48
+ conditionals,
49
+
50
+ ```ruby
51
+ payment.pending?
52
+ # => :false
53
+ ```
54
+
55
+ and scopes
56
+
57
+ ```ruby
58
+ Payment.approved
59
+ # => Mongoid::Criteria for payments where status is :approved
60
+ ```
61
+
62
+ ## Prefix / Suffix
63
+
64
+ You can use the `:prefix` and `:suffix` options, to prefix or suffix the methods
65
+ of the enum. If the passed value is `true`, the methods are prefixed/suffixed
66
+ with the name of the enum. It is also possible to supply a custom value:
67
+
68
+ ```ruby
69
+ enum :status, [:pending, :approved, :declined], prefix: true
70
+ enum :payments_status, [:pending, :approved, :declined], prefix: :payments
71
+
72
+ enum :status, [:pending, :approved, :declined], suffix: true
73
+ enum :payments_status, [:pending, :approved, :declined], suffix: :payments
74
+ ```
75
+
76
+ This will result in the following methods:
77
+
78
+ ```ruby
79
+ payment.status_declined! # prefix: true
80
+ payment.payments_declined! # prefix: :payments
81
+
82
+ payment.declined_status! # suffix: true
83
+ payment.declined_payments! # suffix: :payments
84
+ ```
85
+
86
+ If you want to change the behaviour app-wide you can use the configuration:
87
+
88
+ ```ruby
89
+ Mongoid::EnumAttribute.configure do |config|
90
+ config.field_name_prefix = '_' # prefix of the field used to store the values in database
91
+ config.prefix = nil # prefix of the ! & ? method
92
+ config.suffix = nil # suffix of the ! & ? method
93
+ end
94
+ ```
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mongoid-enum_attribute.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mongoid/enum/attribute"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ module Mongoid
2
+ module EnumAttribute
3
+ class Configuration
4
+ attr_accessor :field_name_prefix
5
+ attr_accessor :prefix
6
+ attr_accessor :suffix
7
+
8
+ def initialize
9
+ self.field_name_prefix = '_'
10
+ self.prefix = nil
11
+ self.suffix = nil
12
+ end
13
+ end
14
+
15
+ def self.configuration
16
+ @configuration ||= Configuration.new
17
+ end
18
+
19
+ def self.configure
20
+ yield(configuration) if block_given?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ module Mongoid
2
+ module EnumAttribute
3
+ module Validators
4
+ class MultipleValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, values)
6
+ values = Array(values)
7
+
8
+ if options[:allow_nil]
9
+ add_error_message(record, attribute) if !all_included?(values, options[:in])
10
+ else
11
+ add_error_message(record, attribute) if values.empty? || !all_included?(values, options[:in])
12
+ end
13
+ end
14
+
15
+ def add_error_message(record, attribute)
16
+ record.errors[attribute] << (options[:message] || "is not in #{options[:in].join(', ')}")
17
+ end
18
+
19
+ def all_included?(values, allowed)
20
+ (values - allowed).empty?
21
+ end
22
+
23
+ def self.kind
24
+ :custom
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module EnumAttribute
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,117 @@
1
+ require "mongoid/enum_attribute/configuration"
2
+ require "mongoid/enum_attribute/validators/multiple_validator"
3
+ require "mongoid/enum_attribute/version"
4
+
5
+ module Mongoid
6
+ module EnumAttribute
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def enum(name, values, options = {})
11
+ field_name = "#{Mongoid::EnumAttribute.configuration.field_name_prefix}#{name}"
12
+ options = default_options(values).merge(options)
13
+
14
+ set_values_constant(name, values)
15
+ create_field(field_name, options)
16
+
17
+ create_validations(field_name, values, options)
18
+ define_value_scopes_and_accessors(name, field_name, values, options)
19
+ define_field_accessor(name, field_name, options)
20
+ end
21
+
22
+ private
23
+
24
+ def default_options(values)
25
+ { multiple: false,
26
+ default: values.first,
27
+ required: true,
28
+ validate: true,
29
+ prefix: Mongoid::EnumAttribute.configuration.prefix,
30
+ suffix: Mongoid::EnumAttribute.configuration.suffix
31
+ }
32
+ end
33
+
34
+ def set_values_constant(name, values)
35
+ const_name = name.to_s.upcase
36
+ const_set(const_name, values)
37
+ end
38
+
39
+ def create_field(field_name, options)
40
+ type = options[:multiple] && Array || Symbol
41
+ field field_name, type: type, default: options[:default]
42
+ end
43
+
44
+ def create_validations(field_name, values, options)
45
+ if options[:multiple] && options[:validate]
46
+ validates(
47
+ field_name,
48
+ "mongoid/enum_attribute/validators/multiple".to_sym => {
49
+ in: values.map(&:to_sym),
50
+ allow_nil: !options[:required]
51
+ }
52
+ )
53
+ elsif options[:validate]
54
+ validates(
55
+ field_name,
56
+ inclusion: { in: values.map(&:to_sym) },
57
+ allow_nil: !options[:required]
58
+ )
59
+ end
60
+ end
61
+
62
+ def define_value_scopes_and_accessors(name, field_name, values, options)
63
+ values.each do |value|
64
+ scope(value, -> { where(field_name => value) })
65
+
66
+ accessor_name = value
67
+ accessor_name = apply_prefix(accessor_name, name, value, options[:prefix]) if options[:prefix]
68
+ accessor_name = apply_suffix(accessor_name, name, value, options[:suffix]) if options[:suffix]
69
+
70
+ if options[:multiple]
71
+ define_array_accessor(accessor_name, field_name, value)
72
+ else
73
+ define_string_accessor(accessor_name, field_name, value)
74
+ end
75
+ end
76
+ end
77
+
78
+ def apply_prefix(accessor_name, name, value, prefix)
79
+ return "#{prefix}_#{value}" if prefix.is_a?(String)
80
+ "#{name}_#{value}"
81
+ end
82
+
83
+ def apply_suffix(accessor_name, name, value, suffix)
84
+ return "#{value}_#{suffix}" if suffix.is_a?(String)
85
+ "#{value}_#{name}"
86
+ end
87
+
88
+ def define_field_accessor(name, field_name, options)
89
+ if options[:multiple]
90
+ define_array_field_accessor(name, field_name)
91
+ else
92
+ define_string_field_accessor(name, field_name)
93
+ end
94
+ end
95
+
96
+ def define_array_field_accessor(name, field_name)
97
+ class_eval "def #{name}=(vals) self.write_attribute(:#{field_name}, Array(vals).compact.map(&:to_sym)) end"
98
+ class_eval "def #{name}() self.send(:#{field_name}).map{ |i| i.try(:to_sym) } end"
99
+ end
100
+
101
+ def define_string_field_accessor(name, field_name)
102
+ class_eval "def #{name}=(val) self.write_attribute(:#{field_name}, val && val.to_sym || nil) end"
103
+ class_eval "def #{name}() self.send(:#{field_name}) end"
104
+ end
105
+
106
+ def define_array_accessor(accessor_name, field_name, value)
107
+ class_eval "def #{accessor_name}?() self.#{field_name}.include?(:#{value}) end"
108
+ class_eval "def #{accessor_name}!() update_attributes! :#{field_name} => (self.#{field_name} || []) + [:#{value}] end"
109
+ end
110
+
111
+ def define_string_accessor(accessor_name, field_name, value)
112
+ class_eval "def #{accessor_name}?() self.#{field_name} == :#{value} end"
113
+ class_eval "def #{accessor_name}!() update_attributes! :#{field_name} => :#{value} end"
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mongoid/enum_attribute/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid-enum_attribute"
8
+ spec.version = Mongoid::EnumAttribute::VERSION
9
+ spec.authors = ["Tomáš Celizna"]
10
+ spec.email = ["tomas.celizna@gmail.com"]
11
+
12
+ spec.summary = %q{Take on ActiveRecord::Enum in Mongoid}
13
+ spec.homepage = "https://github.com/tomasc/mongoid-enum_attribute"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'mongoid', '>= 5', '< 8'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "database_cleaner"
28
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-enum_attribute
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomáš Celizna
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.16'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.16'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '5.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '5.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: database_cleaner
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description:
90
+ email:
91
+ - tomas.celizna@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".travis.yml"
98
+ - CHANGELOG.md
99
+ - Gemfile
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - lib/mongoid/enum_attribute.rb
105
+ - lib/mongoid/enum_attribute/configuration.rb
106
+ - lib/mongoid/enum_attribute/validators/multiple_validator.rb
107
+ - lib/mongoid/enum_attribute/version.rb
108
+ - mongoid-enum_attribute.gemspec
109
+ homepage: https://github.com/tomasc/mongoid-enum_attribute
110
+ licenses: []
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.7.3
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Take on ActiveRecord::Enum in Mongoid
132
+ test_files: []