jiffy_enums 1.0.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.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +22 -0
  4. data/Gemfile +3 -0
  5. data/Gemfile.lock +101 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +61 -0
  8. data/Rakefile +31 -0
  9. data/app/assets/images/jiffy_enums/.gitkeep +0 -0
  10. data/app/assets/javascripts/jiffy_enum.js.coffee +2 -0
  11. data/app/assets/javascripts/jiffy_enums/.gitkeep +0 -0
  12. data/app/assets/javascripts/jiffy_enums.js.coffee +36 -0
  13. data/app/assets/stylesheets/jiffy_enums/.gitkeep +0 -0
  14. data/app/controllers/.gitkeep +0 -0
  15. data/app/helpers/.gitkeep +0 -0
  16. data/app/mailers/.gitkeep +0 -0
  17. data/app/models/.gitkeep +0 -0
  18. data/app/views/.gitkeep +0 -0
  19. data/config/routes.rb +2 -0
  20. data/jiffy_enums.gemspec +20 -0
  21. data/lib/jiffy_enum.rb +13 -0
  22. data/lib/jiffy_enums/engine.rb +4 -0
  23. data/lib/jiffy_enums/version.rb +3 -0
  24. data/lib/jiffy_enums.rb +69 -0
  25. data/lib/tasks/jiffy_enums_tasks.rake +4 -0
  26. data/script/rails +8 -0
  27. data/spec/dummy/README.rdoc +261 -0
  28. data/spec/dummy/Rakefile +7 -0
  29. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  30. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  31. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  32. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  33. data/spec/dummy/app/mailers/.gitkeep +0 -0
  34. data/spec/dummy/app/models/.gitkeep +0 -0
  35. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/spec/dummy/config/application.rb +59 -0
  37. data/spec/dummy/config/boot.rb +10 -0
  38. data/spec/dummy/config/database.yml +25 -0
  39. data/spec/dummy/config/environment.rb +5 -0
  40. data/spec/dummy/config/environments/development.rb +37 -0
  41. data/spec/dummy/config/environments/production.rb +67 -0
  42. data/spec/dummy/config/environments/test.rb +37 -0
  43. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  44. data/spec/dummy/config/initializers/inflections.rb +15 -0
  45. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  46. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  47. data/spec/dummy/config/initializers/session_store.rb +8 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +5 -0
  50. data/spec/dummy/config/routes.rb +58 -0
  51. data/spec/dummy/config.ru +4 -0
  52. data/spec/dummy/lib/assets/.gitkeep +0 -0
  53. data/spec/dummy/log/.gitkeep +0 -0
  54. data/spec/dummy/log/development.log +2 -0
  55. data/spec/dummy/log/test.log +7 -0
  56. data/spec/dummy/public/404.html +26 -0
  57. data/spec/dummy/public/422.html +26 -0
  58. data/spec/dummy/public/500.html +25 -0
  59. data/spec/dummy/public/favicon.ico +0 -0
  60. data/spec/dummy/script/rails +6 -0
  61. data/spec/jiffy_enums_spec.rb +89 -0
  62. data/spec/spec_helper.rb +9 -0
  63. metadata +168 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 90aee4a0455ac51197e626329305913fe71b81f9
4
+ data.tar.gz: c7b6795f2edf1c9f203446931e9cb00cae58a09a
5
+ SHA512:
6
+ metadata.gz: a109cd48b738870bcd6b6dfb2d39811e18fe551082175ef91549d79bc69b459c820ca99cba187e51904ac86e7db8c020aec671d67d04e62aeff2a03fa2849ace
7
+ data.tar.gz: cb93be43e463df3456f8863a2411a26b67c4f518971daed46a73d4a88b6d509f5716ca9816405bebc11d019449f62a5c376b6d81de842cb05d7eb0d9cffe004f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ test/dummy/db/*.sqlite3
5
+ test/dummy/log/*.log
6
+ test/dummy/tmp/
7
+ test/dummy/.sass-cache
8
+ .idea/
9
+
data/.travis.yml ADDED
@@ -0,0 +1,22 @@
1
+ language: ruby
2
+
3
+ before_install:
4
+ - gem install bundler
5
+
6
+ rvm:
7
+ - 1.9.3
8
+ - 2.0.0
9
+ - ruby-head
10
+ - jruby-19mode
11
+ - rbx-19mode
12
+
13
+ gemfile:
14
+ - Gemfile
15
+
16
+ matrix:
17
+ allow_failures:
18
+ - rvm: ruby-head
19
+
20
+ notifications:
21
+ email:
22
+ - ci-admins@identified.com
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,101 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jiffy_enums (0.0.1)
5
+ rails (~> 3.2.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ actionmailer (3.2.8)
11
+ actionpack (= 3.2.8)
12
+ mail (~> 2.4.4)
13
+ actionpack (3.2.8)
14
+ activemodel (= 3.2.8)
15
+ activesupport (= 3.2.8)
16
+ builder (~> 3.0.0)
17
+ erubis (~> 2.7.0)
18
+ journey (~> 1.0.4)
19
+ rack (~> 1.4.0)
20
+ rack-cache (~> 1.2)
21
+ rack-test (~> 0.6.1)
22
+ sprockets (~> 2.1.3)
23
+ activemodel (3.2.8)
24
+ activesupport (= 3.2.8)
25
+ builder (~> 3.0.0)
26
+ activerecord (3.2.8)
27
+ activemodel (= 3.2.8)
28
+ activesupport (= 3.2.8)
29
+ arel (~> 3.0.2)
30
+ tzinfo (~> 0.3.29)
31
+ activeresource (3.2.8)
32
+ activemodel (= 3.2.8)
33
+ activesupport (= 3.2.8)
34
+ activesupport (3.2.8)
35
+ i18n (~> 0.6)
36
+ multi_json (~> 1.0)
37
+ arel (3.0.2)
38
+ builder (3.0.0)
39
+ diff-lcs (1.2.4)
40
+ erubis (2.7.0)
41
+ hike (1.2.1)
42
+ i18n (0.6.0)
43
+ journey (1.0.4)
44
+ json (1.7.4)
45
+ mail (2.4.4)
46
+ i18n (>= 0.4.0)
47
+ mime-types (~> 1.16)
48
+ treetop (~> 1.4.8)
49
+ mime-types (1.19)
50
+ multi_json (1.3.6)
51
+ polyglot (0.3.3)
52
+ rack (1.4.1)
53
+ rack-cache (1.2)
54
+ rack (>= 0.4)
55
+ rack-ssl (1.3.2)
56
+ rack
57
+ rack-test (0.6.1)
58
+ rack (>= 1.0)
59
+ rails (3.2.8)
60
+ actionmailer (= 3.2.8)
61
+ actionpack (= 3.2.8)
62
+ activerecord (= 3.2.8)
63
+ activeresource (= 3.2.8)
64
+ activesupport (= 3.2.8)
65
+ bundler (~> 1.0)
66
+ railties (= 3.2.8)
67
+ railties (3.2.8)
68
+ actionpack (= 3.2.8)
69
+ activesupport (= 3.2.8)
70
+ rack-ssl (~> 1.3.2)
71
+ rake (>= 0.8.7)
72
+ rdoc (~> 3.4)
73
+ thor (>= 0.14.6, < 2.0)
74
+ rake (0.9.2.2)
75
+ rdoc (3.12)
76
+ json (~> 1.4)
77
+ rspec (2.13.0)
78
+ rspec-core (~> 2.13.0)
79
+ rspec-expectations (~> 2.13.0)
80
+ rspec-mocks (~> 2.13.0)
81
+ rspec-core (2.13.1)
82
+ rspec-expectations (2.13.0)
83
+ diff-lcs (>= 1.1.3, < 2.0)
84
+ rspec-mocks (2.13.1)
85
+ sprockets (2.1.3)
86
+ hike (~> 1.2)
87
+ rack (~> 1.0)
88
+ tilt (~> 1.1, != 1.3.0)
89
+ thor (0.15.4)
90
+ tilt (1.3.3)
91
+ treetop (1.4.10)
92
+ polyglot
93
+ polyglot (>= 0.3.1)
94
+ tzinfo (0.3.33)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ jiffy_enums!
101
+ rspec (~> 2.13.0)
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Identified
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # JiffyEnums
2
+
3
+ [![Build Status](https://api.travis-ci.org/Identified/jiffy_enums.png?branch=master)](https://travis-ci.org/Identified/jiffy_enums)
4
+ [![Code Climate](https://codeclimate.com/github/Identified/jiffy_enums.png)](https://codeclimate.com/github/Identified/jiffy_enums)
5
+
6
+ Java-style enums brought over to Ruby and CoffeeScript
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'jiffy_enums'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ ### In Ruby
21
+
22
+ ```ruby
23
+ Class MyEnums < JiffyEnums
24
+ define :ENUM1, 'enum 1'
25
+ define :ENUM2, 'enum 2'
26
+ define :ENUM3, 'enum 3'
27
+ end
28
+
29
+ MyEnums[:ENUM1].key
30
+ => :ENUM1
31
+ MyEnums[:ENUM1].value
32
+ => "enum 1"
33
+ MyEnums[:ENUM1].ordinal
34
+ => 1
35
+
36
+ MyEnums::ENUM2
37
+ => "enum 2"
38
+ ```
39
+
40
+ ### In CoffeeScript (e.g. in a Rails View)
41
+
42
+ ```coffeescript
43
+ Enums = {}
44
+ <%= MyEnums.to_coffee('Enums') %>
45
+
46
+ Enums.MyEnums.get('ENUM1').key
47
+ => "ENUM1"
48
+ Enums.MyEnums.get('ENUM1').value
49
+ => "enum 1"
50
+ Enums.MyEnums.get('ENUM1').ordinal
51
+ => 1
52
+ =>
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'JiffyEnums'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new('spec')
30
+
31
+ task :default => :spec
File without changes
@@ -0,0 +1,2 @@
1
+ class window.JiffyEnum
2
+ constructor: (@key, @value, @ordinal) ->
File without changes
@@ -0,0 +1,36 @@
1
+ #= require jiffy_enum
2
+ #= require_self
3
+
4
+ class window.JiffyEnums
5
+ constructor: (@hash) ->
6
+ that = this
7
+ _.each(@hash, (k,v) ->
8
+ that[v] = k
9
+ )
10
+
11
+ # equavalent to [] in ruby
12
+ get: (key) =>
13
+ @hash[key]
14
+
15
+ each: (func) =>
16
+ _.each(@hash, func)
17
+
18
+ all: (func) =>
19
+ _.values(@hash)
20
+
21
+ for_value: (value) =>
22
+ _.find( all(), (_enum) ->
23
+ value == _enum.value
24
+ )
25
+
26
+ for_ordinal: (ordinal) =>
27
+ _.find( all(), (_enum) ->
28
+ ordinal == _enum.ordinal
29
+ )
30
+
31
+ all_to_hash: () =>
32
+ hash = {}
33
+ each( (_enum, key) ->
34
+ hash[key] = _enum.value
35
+ )
36
+ hash
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,20 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'jiffy_enums/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'jiffy_enums'
7
+ s.version = JiffyEnums::VERSION
8
+ s.authors = ['Tejas Ravindra Mandke']
9
+ s.email = %w(tejas@identified.com)
10
+ s.homepage = 'https://github.com/Identified/jiffy_enums'
11
+ s.summary = 'Easy way to add enums'
12
+ s.description = 'Easy way to add enums'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
16
+ s.require_paths = %w(lib)
17
+
18
+ s.add_dependency 'rails', '~> 3.2.0'
19
+ s.add_development_dependency 'rspec', '~> 2.13.0'
20
+ end
data/lib/jiffy_enum.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'jiffy_enums'
2
+ require 'jiffy_enums/engine'
3
+
4
+ class JiffyEnum
5
+ extend Enumerable
6
+ extend JiffyEnums
7
+ attr_reader :key, :value, :ordinal
8
+ def initialize(key, value, ordinal)
9
+ @key = key
10
+ @value = value
11
+ @ordinal = ordinal
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ module JiffyEnums
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module JiffyEnums
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,69 @@
1
+ require 'jiffy_enum'
2
+
3
+ module JiffyEnums
4
+ # ordinals start from index 1 and NOT 0
5
+ def define(key, value = nil, ordinal = nil, &overrides)
6
+ @hash ||= {}
7
+ @array ||= []
8
+ ordinal ||= @array.size + 1
9
+ the_enum = self.new(key, value, ordinal )
10
+ @hash[key] = the_enum
11
+ @hash[key.to_s] = the_enum
12
+ @array[ordinal - 1] = the_enum
13
+ define_singleton_method(key) do
14
+ the_enum
15
+ end
16
+ the_enum.instance_eval(&overrides) unless overrides.nil?
17
+ end
18
+
19
+ def [] (key)
20
+ @hash[key]
21
+ end
22
+
23
+ def each
24
+ @array.each do |value|
25
+ yield value
26
+ end
27
+ end
28
+
29
+ def select *args, &block
30
+ all.select *args, &block
31
+ end
32
+
33
+ def all
34
+ @array.map{|x|x}.compact
35
+ end
36
+
37
+ def for_value (value)
38
+ all.select do |enum|
39
+ enum.value == value
40
+ end.first
41
+ end
42
+
43
+ def for_ordinal (ordinal)
44
+ @array[ordinal - 1]
45
+ end
46
+
47
+ def all_to_hash
48
+ hash = {}
49
+ each do | value|
50
+ hash[value.key] = value.value
51
+ end
52
+ hash
53
+ end
54
+
55
+ def to_coffee(baseHashStr)
56
+ final_coffee = ''
57
+ final_variable = self.name.split('::').inject(baseHashStr) do |result,part|
58
+ final_coffee += "#{result}.#{part} ||= {}\n"
59
+ "#{result}.#{part}"
60
+ end
61
+
62
+ final_coffee = final_coffee + "#{final_variable} = new JiffyEnums #{
63
+ @array.map do |enum|
64
+ "#{enum.key}:new JiffyEnum(\"#{enum.key}\", \"#{enum.value}\", #{enum.ordinal})"
65
+ end.join(", ")
66
+ }\n"
67
+ final_coffee
68
+ end
69
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :jiffy_enums do
3
+ # # Task goes here
4
+ # end
data/script/rails ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/jiffy_enums/engine', __FILE__)
6
+
7
+ require 'rails/all'
8
+ require 'rails/engine/commands'