acts_as_enum_type 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 73358290c8cc82f530de2c398da9a67cbb61613a
4
+ data.tar.gz: d8de2bf1520d3dd4ec5c71636aa4a6c90667d66c
5
+ SHA512:
6
+ metadata.gz: ec52189d2753a52211ae79bdcaca72934a9b4265691e71dc241f672e73911aeea0a816580cc02d55ec429c5997375c2138403a93b5c50df2dbafb0925b0ab0b1
7
+ data.tar.gz: 4f6aab4c34260c5635547ddd761d76df5675afc4659bf93973c9dd8f720be3926aa60e16f2a35d699ce987b5049c2cb3975d4181bf11684a81fbb16f6e9885e5
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at xiesubin@xiesubinMBP. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_enum.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # ActsAsEnum
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/acts_as_enum`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'acts_as_enum'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install acts_as_enum
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/acts_as_enum. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ # acts_as_enum
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_enum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_enum_type"
8
+ spec.version = ActsAsEnum::VERSION
9
+ spec.authors = ["diandanbao"]
10
+ spec.email = ["admin@isumeng.com"]
11
+
12
+ spec.summary = %q{ActiveRecord plugin to validate values of column in specified word}
13
+ spec.description = %q{ActiveRecord plugin to validate values of column in specified word}
14
+ spec.homepage = "http://www.diandanbao.com"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency 'activerecord', ">= 4.0", "< 5.1"
30
+ spec.add_dependency 'activesupport', ">= 4.0", "< 5.1"
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.11"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acts_as_enum"
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,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,3 @@
1
+ module ActsAsEnum
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,89 @@
1
+ require "acts_as_enum/version"
2
+ require 'active_support/concern'
3
+ require 'active_record'
4
+
5
+ module ActsAsEnum
6
+ extend ActiveSupport::Concern
7
+ included do
8
+ include ActiveModel::Validations
9
+ end
10
+
11
+ module ClassMethods
12
+ def type_columns(type_columns)
13
+ @type_columns = type_columns
14
+ end
15
+
16
+ def type_columns
17
+ @type_columns ||= []
18
+ end
19
+
20
+ def acts_as_enum_hash(column, hash)
21
+ type_values = []
22
+ type_names = []
23
+ hash.each{ |k,v|
24
+ type_values << k
25
+ type_names << v
26
+ }
27
+ acts_as_enum(column, type_values, type_names)
28
+ end
29
+
30
+ def acts_as_enum(column, type_values=[], type_names=[])
31
+ type_columns << column
32
+ type_values.map!(&:to_sym)
33
+ type_names = type_values if type_names.blank? || type_names.empty?
34
+ raise "acts_as_enum values size not equal to names size." if type_values.size != type_names.size
35
+
36
+ define_singleton_method "#{column}_collection".to_sym do
37
+ type_names.zip(type_values)
38
+ end
39
+
40
+ define_singleton_method "#{column}_collection_hash".to_sym do
41
+ type_names.zip(type_values).map{|item| { name: item[0], value: item[1] }}
42
+ end
43
+
44
+ define_singleton_method "#{column}_collection_as_select_data".to_sym do
45
+ type_names.zip(type_values).map{|item| { name: item[0], id: item[1] }}
46
+ end
47
+
48
+ define_singleton_method "#{column}_values".to_sym do
49
+ type_values
50
+ end
51
+
52
+ define_singleton_method "#{column}_value".to_sym do |type_name|
53
+ (Hash[type_names.zip(type_values)][type_name] rescue nil)
54
+ end
55
+
56
+ define_singleton_method "#{column}_names".to_sym do
57
+ type_names
58
+ end
59
+
60
+ define_singleton_method "#{column}_name".to_sym do |type_value|
61
+ (Hash[type_values.zip(type_names)][type_value.to_sym] rescue nil)
62
+ end
63
+
64
+ self.class_eval do
65
+ validates column, inclusion: {:in => type_values.map{|value| [value, value.to_s]}.flatten}, :allow_blank => true
66
+ if respond_to?(:table_name) && ActiveRecord::Base.connection.table_exists?(table_name) && ActiveRecord::Base.connection.column_exists?(table_name, column)
67
+ scope "by_#{column}".to_sym, ->(column_param){ where(column.to_sym => column_param) if column_param.present? }
68
+ end
69
+ end
70
+
71
+ type_values.each do |type_value|
72
+ define_method "is_#{type_value}?".to_sym do
73
+ self.send(column).try(:to_sym) == type_value.to_sym
74
+ end
75
+
76
+ define_method "is_not_#{type_value}?".to_sym do
77
+ self.send(column).try(:to_sym) != type_value.to_sym
78
+ end
79
+ end
80
+
81
+ unless self.instance_methods.include?("#{column}_name".to_sym)
82
+ define_method "#{column}_name".to_sym do
83
+ (Hash[type_values.zip(type_names)][(self.send(column).to_sym)] rescue nil)
84
+ end
85
+ end
86
+
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_enum_type
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - diandanbao
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '4.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '5.1'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '4.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '5.1'
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.11'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.11'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '10.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '10.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '3.0'
95
+ description: ActiveRecord plugin to validate values of column in specified word
96
+ email:
97
+ - admin@isumeng.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - ".gitignore"
103
+ - ".rspec"
104
+ - ".travis.yml"
105
+ - CODE_OF_CONDUCT.md
106
+ - Gemfile
107
+ - README.md
108
+ - Rakefile
109
+ - acts_as_enum.gemspec
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/acts_as_enum.rb
113
+ - lib/acts_as_enum/version.rb
114
+ homepage: http://www.diandanbao.com
115
+ licenses: []
116
+ metadata:
117
+ allowed_push_host: https://rubygems.org
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.8
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: ActiveRecord plugin to validate values of column in specified word
138
+ test_files: []