enumerative 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ enumerative
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p392
data/README.md CHANGED
@@ -64,3 +64,19 @@ Use the enumeration:
64
64
  vehicle.color.key # => black
65
65
  vehicle.color.value # => Black
66
66
  vehicle.color.to_s # => black
67
+
68
+ ### Generator
69
+
70
+ #### Generate an enumeration, its spec and the translations for the en.yml file
71
+
72
+ To the default app/models/enumerations directory:
73
+
74
+ rails generate enumerative:enumeration color black:Black blue:Blue green:Green
75
+
76
+ To a specific directory:
77
+
78
+ rails generate enumerative:enumeration color some/path black:Black blue:Blue green:Green
79
+
80
+ Namespaced:
81
+
82
+ rails generate enumerative:enumeration namespace/color black:Black blue:Blue green:Green
@@ -1,5 +1,5 @@
1
1
  module Enumerative
2
2
 
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
 
5
5
  end
@@ -0,0 +1,67 @@
1
+ require 'rails/generators'
2
+
3
+ module Enumerative
4
+
5
+ class EnumerationGenerator < Rails::Generators::Base
6
+
7
+ desc "Description:\n Creates the specified enumeration with values, a spec and an entry in the en.yml file."
8
+
9
+ argument :enumeration_name, :type => :string, :required => true
10
+ argument :values, :type => :array
11
+
12
+ def self.source_root
13
+ File.join File.dirname(__FILE__),
14
+ 'templates'
15
+ end
16
+
17
+ def install_enumeration
18
+ template "enumeration.rb", File.join( 'app', path, "#{file_name}.rb" )
19
+ end
20
+
21
+ def install_enumeration_spec
22
+ template "enumeration_spec.rb", File.join( 'spec', path, "#{file_name}_spec.rb" )
23
+ end
24
+
25
+ def insert_translation
26
+ text = []
27
+ text << " #{file_name}:"
28
+ text += keys_and_values.sort { |a,b| a.first <=> b.first }.map { |k,v| " #{k}: #{quote_if_numeric( v )}" }
29
+ inject_into_file 'config/locales/en.yml', after: "enumerations:\n" do
30
+ text.join( "\n" ) + "\n"
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def quote_if_numeric( val )
37
+ return "'#{val}'" if val =~ /^[0-9]+$/
38
+ val
39
+ end
40
+
41
+ def path
42
+ @path ||= first_value_is_path? ?
43
+ values.shift :
44
+ File.join( 'models', 'enumerations' )
45
+ end
46
+
47
+ def first_value_is_path?
48
+ !values.first.include?( ":" )
49
+ end
50
+
51
+ def keys_and_values
52
+ values.map do |key_and_value|
53
+ key_and_value.split( ":" )
54
+ end.sort { |a,b| a.first <=> b.first }
55
+ end
56
+
57
+ def file_name
58
+ enumeration_name.underscore
59
+ end
60
+
61
+ def enumeration_class
62
+ enumeration_name.camelcase
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,17 @@
1
+ class <%= enumeration_class %>
2
+
3
+ def self.valid_keys
4
+ %w(
5
+ <% keys_and_values.each do |k,v| -%>
6
+ <%= k %>
7
+ <% end -%>
8
+ )
9
+ end
10
+
11
+ include Enumerative::Enumeration
12
+
13
+ <% keys_and_values.each do |k,v| -%>
14
+ # <%= k.upcase %>
15
+ <% end -%>
16
+
17
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'support/enumeration_sharedspec'
3
+
4
+ describe <%= enumeration_class %> do
5
+
6
+ it_should_behave_like 'an Enumeration'
7
+
8
+ def self.keys
9
+ %w(
10
+ <% keys_and_values.sort { |a,b| a.first <=> b.first }.each do |k,v| -%>
11
+ <%= k %>
12
+ <% end -%>
13
+ )
14
+ end
15
+
16
+ self.keys.each do |key|
17
+
18
+ const = key.upcase
19
+
20
+ it "should have #{const}" do
21
+ described_class.const_get( const ).should be_valid
22
+ end
23
+
24
+ end
25
+
26
+ it "should have the correct select-box values " do
27
+ described_class.to_select.should == [
28
+ <% keys_and_values.sort { |a,b| a.first <=> b.first }.each do |k,v| -%>
29
+ ["<%= v -%>", "<%= k -%>"],
30
+ <% end -%>
31
+ ]
32
+ end
33
+
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerative
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-14 00:00:00.000000000 Z
13
+ date: 2013-07-26 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Tools for defining and using enumerations.
16
16
  email:
@@ -20,7 +20,8 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - .gitignore
23
- - .rvmrc
23
+ - .ruby-gemset
24
+ - .ruby-version
24
25
  - Gemfile
25
26
  - LICENSE
26
27
  - README.md
@@ -31,6 +32,9 @@ files:
31
32
  - lib/enumerative/enumeration_sharedspec.rb
32
33
  - lib/enumerative/has_enumeration.rb
33
34
  - lib/enumerative/version.rb
35
+ - lib/generators/enumerative/enumeration/enumeration_generator.rb
36
+ - lib/generators/enumerative/enumeration/templates/enumeration.rb
37
+ - lib/generators/enumerative/enumeration/templates/enumeration_spec.rb
34
38
  - spec/support/enumeration_sharedspec.rb
35
39
  homepage: https://github.com/ninja-loss/enumerative
36
40
  licenses: []
@@ -52,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
56
  version: '0'
53
57
  requirements: []
54
58
  rubyforge_project:
55
- rubygems_version: 1.8.24
59
+ rubygems_version: 1.8.25
56
60
  signing_key:
57
61
  specification_version: 3
58
62
  summary: Tools for enumerations.
data/.rvmrc DELETED
@@ -1,4 +0,0 @@
1
- rvm ruby-1.9.2-p320@enumerative --create
2
- if [ `uname -s` = "Darwin" ]; then
3
- rvm current
4
- fi