Permalink
Browse files

updated generator for simple enums

  • Loading branch information...
1 parent ef583c8 commit 3c46bc594a07d87360c57f021e5d430af6418504 @kristianmandrup committed Aug 6, 2012
View
@@ -68,11 +68,23 @@ The macros generates the same code (class structure) as shown above.
```ruby
enum :priority
enum :low
- enum :medium
+
+ enum :medium do
+ def send_email?
+ true
+ end
+ end
+
enum :high
end
```
+If you are simply defining an enum of values without functionality, simply use:
+
+```ruby
+enum_for :priority, [:low, :medium, :high]
+```
+
### 2. Customize the Enum
The generator creates a default setup, but each enum member can be changed to fit your needs.
@@ -1,9 +1,11 @@
class ClassyEnumGenerator < Rails::Generators::NamedBase
desc "Generate a ClassyEnum definition in app/enums/"
- argument :name, :type => :string, :required => true, :banner => 'EnumName'
- argument :values, :type => :array, :default => [], :banner => 'value1 value2 value3 etc...'
- class_option :macro, :type => :boolean, :default => false, :banner => 'Generate enums as macro calls'
+ argument :name, :type => :string, :required => true, :banner => 'EnumName'
+ argument :values, :type => :array, :default => [], :banner => 'value1 value2 value3 etc...'
+
+ class_option :macro, :type => :boolean, :default => false, :banner => 'Generate enums using macros'
+ class_option :simple, :type => :boolean, :default => false, :banner => 'Use simple macro enum'
source_root File.expand_path("../templates", __FILE__)
@@ -15,10 +17,18 @@ def copy_files # :nodoc:
protected
def template_name
- macro? 'macro_enum' : 'enum'
+ macro? "#{prefix}macro_enum" : 'enum'
end
def macro?
options[:macro]
end
+
+ def prefix
+ simple? ? 'simple_' : ''
+ end
+
+ def simple?
+ options[:simple]
+ end
end
@@ -5,3 +5,6 @@
enum :<%= arg.underscore %>
<%- end -%>
end
+
+# alternative (use --simple)
+# enum_for :color, [:red, :blue]
@@ -0,0 +1,3 @@
+require 'classy_enum/macros'
+
+enum_for :<%= class_name.underscore %>, <%= values.map(&:to_sym).inspect %>

0 comments on commit 3c46bc5

Please sign in to comment.