mongoid_enum_mapper 0.5.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: 26b3b43fed1a77165990d18dff6678c7646228bb66ec14c1c8dc18dea6faf120
4
+ data.tar.gz: '06683302772ad3c1ea70ad16cd0e59eb33bdc0bd7146940c7e0b5f6e208f5d9b'
5
+ SHA512:
6
+ metadata.gz: 1be85951a46d41c97b9bf4c4bfdba4373f2838f4d0b070b454eb39348e1a03f09b8cf690a816e380cd69dd8dbfd7d8a7ac675d637842a8acbf445c6c2cc1c0a9
7
+ data.tar.gz: d4d75bf8d5513629704768e5916097ec69c9b76d0c77ae511a5a716f5536f4d8bd6f8f6944be5026e5f88c07deb2a2315eb8f35ab477ab45e4334ca82994ea92
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ sudo: false
2
+ env:
3
+ global:
4
+ - CC_TEST_REPORTER_ID=ccb61f4c66dd4eadf6646e35d63dc123133dc37dabc0bd5eb0c002c21abe5628
5
+ language: ruby
6
+ rvm:
7
+ - 2.2
8
+ - 2.3
9
+ - 2.4
10
+ before_install:
11
+ - gem install bundler
12
+ - gem update --system
13
+ - gem --version
14
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
15
+ - chmod +x ./cc-test-reporter
16
+ - ./cc-test-reporter before-build
17
+ script:
18
+ - bundle exec rake test
19
+ after_script:
20
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,10 @@
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_mapper.gemspec
6
+ gemspec
7
+
8
+ group :test do
9
+ gem "simplecov", "~> 0.15.1"
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Bernie Chiu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ [![Maintainability](https://api.codeclimate.com/v1/badges/919d0459a7ffdfa9a221/maintainability)](https://codeclimate.com/github/berniechiu/mongoid_enum_mapper/maintainability)
2
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/919d0459a7ffdfa9a221/test_coverage)](https://codeclimate.com/github/berniechiu/mongoid_enum_mapper/test_coverage)
3
+ [![Build Status](https://travis-ci.org/berniechiu/mongoid_enum_mapper.svg?branch=master)](https://travis-ci.org/berniechiu/mongoid_enum_mapper)
4
+
5
+ # Mongoid Enum Mapper
6
+
7
+ Inspired by [ActiveRecord::Enum](http://api.rubyonrails.org/v5.1/classes/ActiveRecord/Enum.html), the library supports a lightweight solution to map enum key value
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'mongoid_enum_mapper'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install mongoid_enum_mapper
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ # Allow Mongoid to have ActiveRecord like Enum type of Mapping
29
+
30
+ #=> OrderDelivery.rb Model
31
+ include Mongoid::EnumMapper
32
+ define_enum :status, { pending: 0, shipping: 1, shipped: 2 }
33
+
34
+ # Examples
35
+ od = OrderDelivery.new
36
+ od.status #=> Default :pending
37
+ od.status = :shipping #=> :shipping
38
+ od.status #=> :shipping
39
+ od[:status] #=> 1
40
+
41
+ od.status = :invalid #=> :invalid
42
+ od.status #=> :shipping
43
+
44
+ od.update(status: :pending)
45
+ od.status #=> :pending
46
+
47
+ OrderDelivery::STATUS #=> { pending: 0, shipping: 1, shipped: 2 }
48
+ OrderDelivery.update_all(OrderDelivery::STATUS[:shipping])
49
+ ```
50
+
51
+ ## Development
52
+
53
+ 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.
54
+
55
+ 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).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/berniechiu/mongoid_enum_mapper.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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 "enum_mapper"
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,7 @@
1
+ module Mongoid
2
+ module EnumMapper
3
+ end
4
+ end
5
+
6
+ require "mongoid/enum_mapper/version"
7
+ require "mongoid/enum_mapper/define_enum"
@@ -0,0 +1,60 @@
1
+ module Mongoid
2
+ module EnumMapper
3
+ extend ActiveSupport::Concern
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ def reload
10
+ super
11
+ reset_enum_mapping_cache
12
+ self
13
+ end
14
+
15
+ def reset_enum_mapping_cache(enum_field: nil, key: nil)
16
+ if enum_field
17
+ # Reset certain enum field
18
+ enum_mapper = self.class.const_get(enum_field.to_s.upcase)
19
+ key ||= enum_mapper.key(self[:"#{enum_field}"]) # If key not found, grep from current db value
20
+ return self[enum_field] unless enum_mapper[key]
21
+ instance_variable_set(:"@#{enum_field}", nil)
22
+ self[enum_field] = enum_mapper[key]
23
+ else
24
+ # Reset all enum field
25
+ self.class.instance_variable_get(:'@current_enums').each do |field|
26
+ reset_enum_mapping_cache(enum_field: field)
27
+ end
28
+ end
29
+ end
30
+
31
+ module ClassMethods
32
+ def define_enum(enum_field, enum_mapping = {})
33
+ field :"#{enum_field}", type: Integer, default: 0
34
+ set_enum_mapping_constant(enum_field, enum_mapping)
35
+ set_current_enum_fields(enum_field)
36
+
37
+ enum_value = :"@#{enum_field}"
38
+
39
+ define_method(enum_field) do
40
+ instance_variable_get(enum_value) ||
41
+ instance_variable_set(enum_value, enum_mapping.find { |_, value| value == self[enum_field] }.try(:[], 0))
42
+ end
43
+
44
+ define_method(:"#{enum_field}=") do |key|
45
+ reset_enum_mapping_cache(enum_field: enum_field, key: key)
46
+ end
47
+ end
48
+
49
+ def set_enum_mapping_constant(name, value)
50
+ const_name = name.to_s.upcase
51
+ const_set(const_name, value.freeze)
52
+ end
53
+
54
+ def set_current_enum_fields(name)
55
+ @current_enums ||= []
56
+ @current_enums << name.to_s
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module EnumMapper
3
+ VERSION = "0.5.0"
4
+ end
5
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mongoid/enum_mapper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid_enum_mapper"
8
+ spec.version = Mongoid::EnumMapper::VERSION
9
+ spec.authors = ["Bernie Chiu"]
10
+ spec.email = ["bernie_chiu@hotmail.com"]
11
+
12
+ spec.summary = %q{This library supports a lightweight enum suger for Mongoid field.}
13
+ spec.description = %q{Enum mapper for Mongoid field}
14
+ spec.homepage = "https://github.com/berniechiu/mongoid_enum_mapper"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ spec.add_development_dependency "pry-byebug", "~> 3.5", ">= 3.5.1"
37
+
38
+ spec.add_dependency "mongoid", "<= 5.2.1"
39
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_enum_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Bernie Chiu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-14 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.5.1
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '3.5'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.5.1
75
+ - !ruby/object:Gem::Dependency
76
+ name: mongoid
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "<="
80
+ - !ruby/object:Gem::Version
81
+ version: 5.2.1
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "<="
87
+ - !ruby/object:Gem::Version
88
+ version: 5.2.1
89
+ description: Enum mapper for Mongoid field
90
+ email:
91
+ - bernie_chiu@hotmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".travis.yml"
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - lib/enum_mapper.rb
105
+ - lib/mongoid/enum_mapper/define_enum.rb
106
+ - lib/mongoid/enum_mapper/version.rb
107
+ - mongoid_enum_mapper.gemspec
108
+ homepage: https://github.com/berniechiu/mongoid_enum_mapper
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ allowed_push_host: https://rubygems.org
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.7.4
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: This library supports a lightweight enum suger for Mongoid field.
133
+ test_files: []