enumerable_constants 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in enumerable_constants.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,12 @@
1
+ # Enumerable Contstants
2
+
3
+ This gem can be used for situations where enumerating over constant values is desired.
4
+ This facilitates keeping the code DRY by keeping the constant values defined in one place, and
5
+ still having the ability to enumerate over their values wherever they are needed.
6
+
7
+ ## Example use cases
8
+
9
+ * defining constants for possible field values in an AR object while using the values in a
10
+ `validates_inclusion_of` validation
11
+ * defining constants for select box values in a view helper and using this gem to allow them to be
12
+ enumerated over in the select tag
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "enumerable_constants/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "enumerable_constants"
7
+ s.version = EnumerableConstants::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Chad Boyd"]
10
+ s.email = ["hoverlover@gmail.com"]
11
+ s.homepage = "http://genuitytech.com"
12
+ s.summary = %q{Provides the ability to enumerate over constants.}
13
+
14
+ s.rubyforge_project = "enumerable_constants"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,37 @@
1
+ # This module can be used for situations where enumerating over constant values is desired.
2
+ # This facilitates keeping the code DRY by keeping the constant values defined in one place, and
3
+ # still having the ability to enumerate over their values wherever they are needed.
4
+ #
5
+ # ## Example use cases
6
+ #
7
+ # * defining constants for possible field values in an AR object while using the values in a
8
+ # `validates_inclusion_of` validation
9
+ # * defining constants for select box values in a view helper and using this module to allow them to be
10
+ # enumerated over in the select tag
11
+ #
12
+ module EnumerableConstants
13
+
14
+ def EnumerableConstants.included(mod)
15
+ class << mod
16
+ include Enumerable
17
+
18
+ def enumerable(*consts)
19
+ @enumerable_consts = consts unless consts.empty?
20
+ end
21
+
22
+ # Yields each constant value to &block. By default, all constants defined by the
23
+ # including class are yielded. If specific constants are to be yielded, call
24
+ # EnumerableConstants#enumerable(*consts) before calling this method.
25
+
26
+ def each(&block)
27
+ consts = @enumerable_consts || self.constants.grep(/[^a-z][A-Z_]+/)
28
+ consts.each do |const|
29
+ yield self.const_get(const)
30
+ end
31
+ end
32
+
33
+ # #each_constant is provided as syntactic sugar for situations where it may be more appropriate than #each
34
+ alias_method :each_constant, :each
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module EnumerableConstants
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumerable_constants
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Chad Boyd
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-26 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ - hoverlover@gmail.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - README
30
+ - Rakefile
31
+ - enumerable_constants.gemspec
32
+ - lib/enumerable_constants.rb
33
+ - lib/enumerable_constants/version.rb
34
+ has_rdoc: true
35
+ homepage: http://genuitytech.com
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ requirements: []
56
+
57
+ rubyforge_project: enumerable_constants
58
+ rubygems_version: 1.6.2
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Provides the ability to enumerate over constants.
62
+ test_files: []
63
+