poppy 0.1.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: 06df0c8ad043e29c594f26c10d466b80ddce8534
4
+ data.tar.gz: 59760e9cee24a8117c30a7068b7c6b1e8b8a7030
5
+ SHA512:
6
+ metadata.gz: 333f67966160c02e2f8652bc1076d02d0db511c1048d589c6711ffad1ea43e549df8b2bab953fe1bbaccf90190bf9798d2a20b481651a83578468c4024ec96f1
7
+ data.tar.gz: 66eed5dd3f49a6cd716601fea630e5aec67e327fcacc92531012972b2d53c1fc09a311fc8abf4598d179fb70ff128777589b44df8ee0ff6a2dc68dc66bc7039e
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in poppy.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Damien Adermann
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,205 @@
1
+ #Poppy
2
+
3
+ ## Introduction
4
+ Simple Polymorphic Enumerations
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'poppy'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install poppy
21
+
22
+
23
+ ## Usage
24
+
25
+
26
+ ### Basic
27
+ ```ruby
28
+ class Bread < Poppy::Enum
29
+ values :white, :multigrain, :gluten_free
30
+ end
31
+ ```
32
+
33
+ #### Using an enum value
34
+ ```ruby
35
+ > bread = Bread::WHITE
36
+ > bread
37
+ => Bread::WHITE
38
+
39
+ > bread.humanize
40
+ => White
41
+ ```
42
+
43
+
44
+ #### Listing all enum values
45
+ ```
46
+ > Bread.list
47
+ => [Bread::WHITE, BREAD::MULTIGRAIN, Bread::GLUTEN_FREE]
48
+
49
+ > Bread.collection
50
+ => [['White', 'white'], ['Multigrain', 'multigrain'], ['Gluten free', 'gluten_free']
51
+ ```
52
+
53
+ ### Custom values
54
+ Each enumeration value is able to define its own behavior. This makes our enumerations much more polymorphic. By defining classes we can decide the behavior each enumeration value has. Each value added to the enumeration will be created by instantiating the matching class
55
+
56
+ E.g. Bread::WHITE is created from the value :white. It is instantiated by the class Bread::White.
57
+
58
+ If no matching class exists a class will be created before it is instantiated
59
+
60
+ ***Never instantiate a class directly. Enum::Value.new != Enum::VALUE***
61
+
62
+
63
+ ```ruby
64
+ class Bread < Poppy::Enum
65
+ values :white, :multigrain
66
+
67
+ private
68
+
69
+ class White
70
+ include Poppy::Value
71
+
72
+ def enjoyed_by_kids?
73
+ true
74
+ end
75
+ end
76
+
77
+ class Multigrain
78
+ include Poppy::Value
79
+
80
+ def enjoyed_by_kids?
81
+ false
82
+ end
83
+ end
84
+ end
85
+
86
+ white = Bread::WHITE
87
+ multi = Bread::MULTIGRAIN
88
+
89
+ puts white.enjoyed_by_kids? # true
90
+ puts multi.enoyed_by_kids? # false
91
+
92
+ ```
93
+
94
+ Each enumeration value is just a Poro. Each value has an interface of one method, `#humanize`. More methods can be added to build a more robust interface. If not using Poppy::Rails humanize can be omitted.
95
+
96
+ `Poppy::Value` by default, defines humanize to be the demodulized class name humanized and capitalized. You are free to implement this however you would like.
97
+
98
+ E.g.
99
+
100
+ ```ruby
101
+ class Bread < Poppy::Enum
102
+ values :white, :multigrain
103
+
104
+ class White
105
+ def humanize
106
+ 'Black'
107
+ end
108
+ end
109
+
110
+ class Multigrain
111
+ extend Poppy::Value
112
+ end
113
+ end
114
+
115
+ ```
116
+
117
+ ### Polymorphism
118
+
119
+
120
+ By giving each Enumeration value behavior we can remove switch statements from our codebase.
121
+
122
+ ```ruby
123
+ bread = Bread::WHITE
124
+
125
+ case bread
126
+ when Bread::WHITE
127
+ puts 'My kid will love this'
128
+ when Bread::MULTIGRAIN
129
+ puts 'My kid will play bread frisbee'
130
+ when Bread::GLUTEN_FREE
131
+ puts 'My kid will play bread frisbee'
132
+ end
133
+
134
+ ```
135
+
136
+ becomes
137
+
138
+ ```
139
+ if bread.enjoyed_by_kids?
140
+ puts 'My kid will love this'
141
+ else
142
+ puts 'My kid will play bread frisbee'
143
+ end
144
+ ```
145
+
146
+ ## ActiveRecord Integration
147
+
148
+ See the [Poppy Rails](#TODO) Gem for a simple integration with ActiveRecord for persistance. Enum arrays are also supported for postgreSQL databases.
149
+
150
+
151
+ ##API
152
+
153
+ ### Poppy::Enum
154
+ #### .value_for
155
+
156
+ ```ruby
157
+ > Bread.value_for(:white)
158
+ => Bread::WHITE
159
+ ```
160
+
161
+ #### .values
162
+
163
+ ```ruby
164
+ > Bread.list_keys
165
+ => [:white, :multigrain, :gluten_free]
166
+ ```
167
+
168
+ #### .list
169
+
170
+ ```ruby
171
+ > Bread.list
172
+ => [Bread::WHITE, BREAD::MULTIGRAIN, Bread::GLUTEN_FREE]
173
+ ```
174
+
175
+ #### .valid?
176
+
177
+ ```ruby
178
+ > Bread.valid?(Bread::WHITE)
179
+ => true
180
+ ```
181
+
182
+ ### .key_for
183
+
184
+ ```ruby
185
+ > Bread.key_for(Bread::WHITE)
186
+ => :white
187
+ ```
188
+
189
+ #### .collection
190
+ ```ruby
191
+ > Bread.collection
192
+ => [['White', 'white'], ['Multigrain', 'multigrain'], ['Gluten free', 'gluten_free']
193
+ ```
194
+ * element.first is defined by the humanized value
195
+
196
+ For use with the Rails form builder
197
+
198
+ ### Poppy::Value
199
+
200
+ #### #humanize
201
+ ```ruby
202
+ > Bread::WHITE.humanize
203
+ => White
204
+ ```
205
+
@@ -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
data/TODO.md ADDED
@@ -0,0 +1,6 @@
1
+ ##TODO
2
+
3
+ *Setup Travis
4
+ *Add badges
5
+ *Add i18n support
6
+ *Add rdoc or similar
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "poppy"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ require "poppy/version"
2
+
3
+ module Poppy
4
+ # Your code goes here...
5
+ end
6
+
7
+ require "poppy/enum"
8
+ require "poppy/value"
@@ -0,0 +1,103 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Poppy
4
+ class Enum
5
+ class << self
6
+ def values(*values)
7
+ self.value_keys = values
8
+ end
9
+
10
+ def list_keys
11
+ value_keys
12
+ end
13
+
14
+ def list
15
+ value_keys.map(&method(:enum_for))
16
+ end
17
+
18
+ def enum_for(value_key)
19
+ return unless valid_value_key?(value_key)
20
+ const_get(value_key.to_s.upcase)
21
+ end
22
+
23
+ def valid?(value)
24
+ list.include?(value)
25
+ end
26
+
27
+ def key_for(value)
28
+ return unless valid?(value)
29
+ key_value_map.invert[value]
30
+ end
31
+
32
+ def collection
33
+ value_keys.map(&method(:value_key_to_collection_item))
34
+ end
35
+
36
+ def const_missing(name_sym)
37
+ name = name_sym.to_s
38
+ if valid_class_name?(name)
39
+ create_poppy_class(name)
40
+ elsif valid_constant_name?(name)
41
+ create_poppy_value(name)
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ attr_accessor :value_keys
50
+
51
+ def key_value_map
52
+ @key_value_map ||= Hash[value_keys.map { |key| [key, enum_for(key)] }]
53
+ end
54
+
55
+ def value_key_to_collection_item(value_key)
56
+ [enum_for(value_key).humanize, value_key.to_s]
57
+ end
58
+
59
+ def class_for_value(value)
60
+ class_string = class_for_string_value(value)
61
+ ActiveSupport::Inflector.constantize(class_string)
62
+ end
63
+
64
+ def create_poppy_class(class_name)
65
+ const_set(class_name, Class.new.include(Poppy::Value))
66
+ end
67
+
68
+ def create_poppy_value(constant_name)
69
+ const_set(constant_name, class_for_value(constant_name.downcase).new)
70
+ end
71
+
72
+ def valid_value_key?(value_key)
73
+ value_keys.include?(value_key)
74
+ end
75
+
76
+ def valid_class_name?(class_name)
77
+ classy?(class_name) &&
78
+ valid_value_key?(class_name.downcase.to_sym)
79
+ end
80
+
81
+ def valid_constant_name?(constant_name)
82
+ screaming_snake_case?(constant_name) &&
83
+ valid_value_key?(constant_name.downcase.to_sym)
84
+ end
85
+
86
+ def classy?(string)
87
+ ActiveSupport::Inflector.classify(string.downcase) == string
88
+ end
89
+
90
+ def screaming_snake_case?(string)
91
+ ActiveSupport::Inflector.underscore(string).upcase == string
92
+ end
93
+
94
+ def class_for_string_value(value)
95
+ "#{self.to_s}::#{value.to_s.capitalize}"
96
+ end
97
+
98
+ def value_keys
99
+ @value_keys ||= []
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Poppy
4
+ module Value
5
+ def humanize
6
+ ActiveSupport::Inflector.underscore(value_name).gsub('_', ' ').capitalize
7
+ end
8
+
9
+ def to_s
10
+ name_segments = self.class.name.split('::')
11
+ name_segments[-1] = to_screaming_snake_case(name_segments[-1])
12
+ name_segments.join('::')
13
+ end
14
+
15
+ private
16
+
17
+ def to_screaming_snake_case(string)
18
+ ActiveSupport::Inflector.underscore(string).upcase
19
+ end
20
+
21
+ def value_name
22
+ ActiveSupport::Inflector.demodulize(self.class.name)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Poppy
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'poppy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "poppy"
8
+ spec.version = Poppy::VERSION
9
+ spec.authors = ["Damien Adermann"]
10
+ spec.email = ["damienadermann@gmail.com"]
11
+
12
+ spec.summary = %q{Simple Polymorphic Enumerations}
13
+ spec.description = %q{Simple polymorphic enumerations for ruby}
14
+ spec.homepage = "https://bitbucket.org/damienadermann/poppy"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "byebug"
26
+
27
+ spec.add_dependency "activesupport", "~> 4.0"
28
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poppy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Damien Adermann
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-06 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ description: Simple polymorphic enumerations for ruby
84
+ email:
85
+ - damienadermann@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - TODO.md
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/poppy.rb
102
+ - lib/poppy/enum.rb
103
+ - lib/poppy/value.rb
104
+ - lib/poppy/version.rb
105
+ - poppy.gemspec
106
+ homepage: https://bitbucket.org/damienadermann/poppy
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.5
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Simple Polymorphic Enumerations
130
+ test_files: []