enumish 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd34d003bdef7eaf92786ab4e6e1bd25a1ca7cab
4
+ data.tar.gz: '092c70c046c113815500f169ffa669b84a225a72'
5
+ SHA512:
6
+ metadata.gz: 4fe669e7dc94447fb27d92907bfc40cff10fa88baf82e97c9cb72a92efc3549dfdc217cbdf0f3d7063325578c7d35acd3dcb651d357dedb1b0a867fc7cfb1bd2
7
+ data.tar.gz: c5d7069c7aa3c1f67c26262559af0f9932afee34ec3a39e618f3066342ea2eda9c115071248b2fa132725e3f7bdcdc2f09db914b640a11236e58e24667d2277a
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in enumish.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Carl Mercier
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.
@@ -0,0 +1,122 @@
1
+ # Enumish
2
+
3
+ Enumish is "Enum for ActiveRecord".
4
+
5
+ It gives you the ability to create Enum-like values directly in the database.
6
+
7
+ ## Simple Example
8
+
9
+ Given a model:
10
+
11
+ ````ruby
12
+ class Color < ActiveRecord
13
+ include Enumish
14
+ end
15
+ ````
16
+
17
+ You could do things such as:
18
+
19
+ ````ruby
20
+ color = Color.blue # gets the color blue from the database
21
+ color.blue? # => true
22
+ color.red? # => false
23
+ color.to_s # => "blue"
24
+ color.to_sym # => :blue
25
+ color.id # returns the ID from the database
26
+
27
+ Color.not_a_color # raises MethodNotFound
28
+ ````
29
+
30
+ ## A-bit-more-advanced-but-still-simple Example
31
+
32
+ You can combine Enumish with any other ActiveRecord functionality or Gems. For example,
33
+ here's a model that uses Enumish and RankedModel, as well as some custom code.
34
+
35
+ ````ruby
36
+ class Color < ActiveRecord
37
+ include RankedModel
38
+ ranks :position
39
+ include Enumish
40
+
41
+ class << self
42
+ def select_options
43
+ Color.rank(:position).where(enabled: true).map do |s|
44
+ { s.human_description => s.id }
45
+ end
46
+ end
47
+ end
48
+
49
+ def description
50
+ "#{self.to_s} is a beautiful color!"
51
+ end
52
+ end
53
+ ````
54
+
55
+ Such a model allows you, for example, to have a relationship between two models such as:
56
+
57
+ ````ruby
58
+ class Car < ActiveRecord
59
+ has_one :color
60
+ end
61
+
62
+ car = Car.new
63
+ car.color = Color.blue
64
+ car.save!
65
+
66
+ car.color.blue? # => true
67
+ car.color.red? # => false
68
+ car.color == Color.blue # => true
69
+ car.color_id # => Integer
70
+
71
+ # NOTE: table `cars` has column `color_id` of type `integer`
72
+ ````
73
+
74
+ The method `.select_options` above could be used to populate an HTML `select` element.
75
+ The value of each element would be the `id` of the corresponding `Color` record.
76
+
77
+ ## Migration
78
+
79
+ For each model that includes Enumish, add a column named `short` of type `string`. This
80
+ column will be used to store the "short" representation of the record. It will also be used
81
+ as a method name, so make sure it only contains characters that are valid in a Ruby method name.
82
+
83
+ ````ruby
84
+ class CreateColors < ActiveRecord::Migration[5.0]
85
+ def change
86
+ create_table :colors do |t|
87
+ t.string :short, null: false # << This!
88
+ t.string :long, null: false
89
+ t.boolean :enabled, default: true, null: false
90
+ t.integer :position, null: false
91
+
92
+ t.timestamps
93
+ end
94
+ end
95
+ end
96
+ ````
97
+
98
+ If you'd rather name the column something else, you can override the `enum_id` method in your
99
+ model:
100
+
101
+ ````ruby
102
+ class Color < ActiveRecord
103
+ include Enumish
104
+
105
+ def enum_id
106
+ :enum_value
107
+ end
108
+ end
109
+ ````
110
+
111
+ ## Installation
112
+
113
+ Add this line to your application's Gemfile:
114
+
115
+ ```ruby
116
+ gem 'enumish'
117
+ ```
118
+
119
+ ## License
120
+
121
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
122
+
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "enumish"
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__)
@@ -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,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'enumish/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "enumish"
8
+ spec.version = Enumish::VERSION
9
+ spec.authors = ["Carl Mercier"]
10
+ spec.email = ["foss@carlmercier.com"]
11
+
12
+ spec.summary = %q{Enum for ActiveRecord}
13
+ spec.description = %q{Create Enum-like values directly in the database for flexibility, power and supremacy.}
14
+ spec.homepage = "https://www.github.com/cmer/enumish"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "temping", "~> 3.9"
28
+ spec.add_development_dependency "sqlite3"
29
+ spec.add_runtime_dependency "activesupport", "~> 5.0"
30
+ spec.add_runtime_dependency "activerecord", "~> 5.0"
31
+
32
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "enumish/version"
4
+ require "active_support"
5
+
6
+ module Enumish
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def method_missing(method_id, *args, &block)
11
+ if !method_id.to_s.match(/\?$/)
12
+ obj = self.where(enum_id => method_id.to_s).first
13
+ return obj if obj.present?
14
+ end
15
+
16
+ super method_id, *args, &block
17
+ end
18
+
19
+ def enum_id
20
+ :short
21
+ end
22
+ end
23
+
24
+ # Allow calls such as object.friendly? or model.attitude.friendly?
25
+ def method_missing(method_id, *args, &block)
26
+ if method_id.to_s.match(/\?$/) && args.empty? && block.nil?
27
+ self.send(self.class.enum_id.to_s) == method_id.to_s.sub(/\?$/, "")
28
+ else
29
+ super
30
+ end
31
+ end
32
+
33
+ def to_s
34
+ self.send(self.class.enum_id).to_s
35
+ end
36
+
37
+ def to_sym
38
+ self.send(self.class.enum_id).to_sym
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Enumish
2
+ VERSION = "0.9.0"
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Carl Mercier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-11 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: temping
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.0'
111
+ description: Create Enum-like values directly in the database for flexibility, power
112
+ and supremacy.
113
+ email:
114
+ - foss@carlmercier.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - enumish.gemspec
129
+ - lib/enumish.rb
130
+ - lib/enumish/version.rb
131
+ homepage: https://www.github.com/cmer/enumish
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.6.11
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Enum for ActiveRecord
155
+ test_files: []