Skip to content
This repository
Browse code

Added actual code

  • Loading branch information...
commit 7a16ade293304f1cf3608f0f239558ad25de6521 1 parent 6a824fd
Peter Brown authored September 21, 2010
6  Rakefile
@@ -5,9 +5,9 @@ begin
5 5
   require 'jeweler'
6 6
   Jeweler::Tasks.new do |gem|
7 7
     gem.name = "classy_enum"
8  
-    gem.summary = %Q{TODO: one-line summary of your gem}
9  
-    gem.description = %Q{TODO: longer description of your gem}
10  
-    gem.email = "pete@lette.us"
  8
+    gem.summary = %Q{A class based enumerator utility for Ruby on Rails}
  9
+    gem.description = %Q{A utility that adds class based enum functionaltiy to ActiveRecord attributes}
  10
+    gem.email = "github@lette.us"
11 11
     gem.homepage = "http://github.com/beerlington/classy_enum"
12 12
     gem.authors = ["Peter Brown"]
13 13
     gem.add_development_dependency "rspec", ">= 1.2.9"
55  classy_enum.gemspec
... ...
@@ -0,0 +1,55 @@
  1
+# Generated by jeweler
  2
+# DO NOT EDIT THIS FILE DIRECTLY
  3
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
  4
+# -*- encoding: utf-8 -*-
  5
+
  6
+Gem::Specification.new do |s|
  7
+  s.name = %q{classy_enum}
  8
+  s.version = "0.0.0"
  9
+
  10
+  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
  11
+  s.authors = ["Peter Brown"]
  12
+  s.date = %q{2010-09-21}
  13
+  s.description = %q{A utility that adds class based enum functionaltiy to ActiveRecord attributes}
  14
+  s.email = %q{github@lette.us}
  15
+  s.extra_rdoc_files = [
  16
+    "LICENSE",
  17
+     "README.rdoc"
  18
+  ]
  19
+  s.files = [
  20
+    ".document",
  21
+     ".gitignore",
  22
+     "LICENSE",
  23
+     "README.rdoc",
  24
+     "Rakefile",
  25
+     "VERSION",
  26
+     "classy_enum.gemspec",
  27
+     "lib/classy_enum.rb",
  28
+     "spec/classy_enum_spec.rb",
  29
+     "spec/spec.opts",
  30
+     "spec/spec_helper.rb"
  31
+  ]
  32
+  s.homepage = %q{http://github.com/beerlington/classy_enum}
  33
+  s.rdoc_options = ["--charset=UTF-8"]
  34
+  s.require_paths = ["lib"]
  35
+  s.rubygems_version = %q{1.3.7}
  36
+  s.summary = %q{A class based enumerator utility for Ruby on Rails}
  37
+  s.test_files = [
  38
+    "spec/classy_enum_spec.rb",
  39
+     "spec/spec_helper.rb"
  40
+  ]
  41
+
  42
+  if s.respond_to? :specification_version then
  43
+    current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
  44
+    s.specification_version = 3
  45
+
  46
+    if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
  47
+      s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
  48
+    else
  49
+      s.add_dependency(%q<rspec>, [">= 1.2.9"])
  50
+    end
  51
+  else
  52
+    s.add_dependency(%q<rspec>, [">= 1.2.9"])
  53
+  end
  54
+end
  55
+
2  init.rb
... ...
@@ -0,0 +1,2 @@
  1
+# Adds active_set_helper method to all active record classes
  2
+require 'classy_enum'
105  lib/classy_enum.rb
... ...
@@ -0,0 +1,105 @@
  1
+class ClassyEnumValue < Object 
  2
+ 
  3
+  attr_reader :to_s, :index
  4
+
  5
+  def initialize(option, index)
  6
+    @to_s = option.to_s.downcase
  7
+    @index = index + 1
  8
+  end
  9
+  
  10
+  def name
  11
+    to_s.titleize
  12
+  end
  13
+  
  14
+  def <=> other
  15
+    @index <=> other.index
  16
+  end
  17
+
  18
+end
  19
+
  20
+module ClassyEnum
  21
+    
  22
+  module ClassMethods
  23
+      
  24
+    def new(option)
  25
+      self::OPTION_HASH[option] || TypeError.new("Valid #{self} options are #{self.valid_options}")
  26
+    end
  27
+    
  28
+    def all
  29
+      self::OPTIONS.map {|e| self.new(e) }
  30
+    end
  31
+    
  32
+    # Uses the name field for select options
  33
+    def all_with_name
  34
+      self.all.map {|e| [e.name, e.to_s] }
  35
+    end
  36
+    
  37
+    def valid_options
  38
+      self::OPTIONS.map(&:to_s).join(', ')
  39
+    end
  40
+    
  41
+    # Alias of new
  42
+    def find(option)
  43
+      new(option)
  44
+    end
  45
+  
  46
+  end
  47
+  
  48
+  def self.included(other)
  49
+    other.extend ClassMethods
  50
+    
  51
+    other.const_set("OPTION_HASH", Hash.new)
  52
+
  53
+    other::OPTIONS.each do |option|
  54
+      klass = Class.new(ClassyEnumValue) {
  55
+        include other::Defaults if other.const_defined?("Defaults")
  56
+      }
  57
+
  58
+      Object.const_set("#{other}#{option.to_s.camelize}", klass)
  59
+    
  60
+      instance = klass.new(option, other::OPTIONS.index(option))
  61
+      
  62
+      other::OPTION_HASH[option] = other::OPTION_HASH[option.to_s.downcase] = instance
  63
+      
  64
+      ClassyEnum.const_set(option.to_s.upcase, instance)
  65
+    end
  66
+
  67
+  end
  68
+  
  69
+end
  70
+
  71
+module ClassyEnumAttributes
  72
+  module ClassMethods
  73
+    def classy_enum_attr(klass, method=nil)
  74
+      
  75
+      method ||= klass
  76
+      
  77
+      klass = klass.to_s.camelize.constantize
  78
+      
  79
+      # Add ActiveRecord validation to ensure it won't be saved unless it's an option
  80
+      self.send(:validates_inclusion_of, method, :in => klass.all)
  81
+      
  82
+      self.instance_eval do
  83
+        
  84
+        # Define getter method
  85
+        define_method method do
  86
+          klass.new(super)
  87
+        end
  88
+        
  89
+        # Define setter method
  90
+        define_method "#{method}=" do |value|
  91
+          super value.to_s
  92
+        end
  93
+        
  94
+      end
  95
+      
  96
+    end
  97
+  end
  98
+  
  99
+  def self.included(other)
  100
+    other.extend ClassMethods
  101
+  end
  102
+  
  103
+end
  104
+
  105
+ActiveRecord::Base.send :include, ClassyEnumAttributes
92  spec/classy_enum_spec.rb
... ...
@@ -1,7 +1,93 @@
1 1
 require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2 2
 
3  
-describe "ClassyEnum" do
4  
-  it "fails" do
5  
-    fail "hey buddy, you should probably rename this file and start specing for real"
  3
+module TestEnum 
  4
+  OPTIONS = [:one, :two, :three]
  5
+  
  6
+  module Defaults
  7
+    def do_something?
  8
+      false
  9
+    end
  10
+  end
  11
+
  12
+  include ClassyEnum
  13
+end
  14
+
  15
+class TestEnumTwo
  16
+  def do_something?
  17
+    true
6 18
   end
7 19
 end
  20
+
  21
+describe ClassyEnum do
  22
+
  23
+  TestEnum::OPTIONS.each do |option|
  24
+   it "should define a TestEnum#{option.to_s.capitalize} class" do
  25
+     Object.const_defined?("TestEnum#{option.to_s.capitalize}").should be_true
  26
+   end
  27
+  end
  28
+  
  29
+  it "should return an array of enums" do
  30
+    TestEnum.all.should == TestEnum::OPTIONS.map {|o| TestEnum.new(o) }
  31
+  end
  32
+  
  33
+  it "should return an array of enums for a select tag" do
  34
+    TestEnum.all_with_name.should == TestEnum::OPTIONS.map {|o| [TestEnum.new(o).name, TestEnum.new(o).to_s] }
  35
+  end
  36
+  
  37
+  it "should return a type error when adding an invalid option" do
  38
+    TestEnum.new(:invalid_option).class.should == TypeError
  39
+  end
  40
+  
  41
+  context "with a collection of enums" do
  42
+    before(:each) do
  43
+      @one = TestEnum.new(:one)
  44
+      @two = TestEnum.new(:two)
  45
+      @three = TestEnum.new(:three)
  46
+
  47
+      @unordered = [@one, @three, @two]
  48
+    end
  49
+    
  50
+    it "should sort the enums" do
  51
+      @unordered.sort.should == [@one, @two, @three]
  52
+    end
  53
+
  54
+    it "should find the max enum based on its order" do
  55
+      @unordered.max.should == @three
  56
+    end
  57
+  end
  58
+  
  59
+  it "should find an enum by symbol" do
  60
+    TestEnum.find(:one).class.should == TestEnumOne
  61
+  end
  62
+  
  63
+  it "should find an enum by string" do
  64
+    TestEnum.find("one").class.should == TestEnumOne
  65
+  end
  66
+  
  67
+end
  68
+
  69
+describe "An ClassyEnumValue" do
  70
+  before(:each) { @enum = TestEnum.new(:one) }
  71
+  
  72
+  it "should have a name" do
  73
+    @enum.name.should == "One"
  74
+  end
  75
+  
  76
+  it "should inherit the Default methods" do
  77
+    @enum.do_something?.should be_false
  78
+  end
  79
+  
  80
+  it "should create the same instance with a string or symbol" do
  81
+    @enum_string = TestEnum.new("one")
  82
+    
  83
+    @enum.should == @enum_string
  84
+  end
  85
+end
  86
+
  87
+describe "An ClassyEnumValue" do
  88
+  before(:each) { @enum = TestEnum.new(:two) }
  89
+  
  90
+  it "should override the Default methods" do
  91
+    @enum.do_something?.should be_true
  92
+  end
  93
+end
10  spec/spec_helper.rb
... ...
@@ -1,9 +1,7 @@
1 1
 $LOAD_PATH.unshift(File.dirname(__FILE__))
2 2
 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3  
-require 'classy_enum'
4  
-require 'spec'
5  
-require 'spec/autorun'
6 3
 
7  
-Spec::Runner.configure do |config|
8  
-  
9  
-end
  4
+require 'rubygems'
  5
+require 'active_record'
  6
+require 'classy_enum'
  7
+require 'spec'

0 notes on commit 7a16ade

Please sign in to comment.
Something went wrong with that request. Please try again.