fake_enum 0.0.2

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /.bundle
7
+ /vendor/bundle
8
+ /log/*
9
+ /tmp/*
10
+ /db/*.sqlite3
11
+ /public/system/*
12
+ /coverage/
13
+ /spec/tmp/*
14
+ **.orig
15
+ rerun.txt
16
+ pickle-email-*.html
17
+
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fake_enum.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Flip Studio
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # FakeEnum
2
+
3
+ A gem which define enum classes with constants.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fake_enum'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fake_enum
18
+
19
+ ## Usage
20
+
21
+ To use enum, just create a class which extends fake_enum
22
+ ```ruby
23
+ class Status
24
+ extend FakeEnum
25
+
26
+ ACTIVE = 1
27
+ ARCHIVED = 2
28
+ DELETED = 3
29
+ end
30
+ class Task < ActiveRecord::Base
31
+ attr_accessible :status, :name, :description
32
+
33
+ validates :name, :description, :presence => true
34
+ validates :status, :inclusion => { :in => Status.to_a, :message => "%{value} is not a valid status" }
35
+ end
36
+ ```
37
+
38
+ ### Translations
39
+ fake_enum uses I18n for translations and leverages the namespace of the activerecord:
40
+ ```yml
41
+ en:
42
+ activerecord:
43
+ fake_enums:
44
+ status:
45
+ active: 'Active'
46
+ archived: 'Archived'
47
+ deleted: 'Deleted'
48
+ ```
49
+
50
+ ### Methods
51
+ fake_enum offers some methods to help you:
52
+
53
+ `Status::ACTIVE` or `Status.from_s 'ACTIVE'` will return `1`
54
+
55
+ `Status.to_s 2` will return the string which represents the enum 2: `'ARCHIVED'`
56
+
57
+ `Status.to_sym 3` will return the symbol which represents the enum 3: `:deleted`
58
+
59
+ `Status.localize 1` will return the translation for enum 1: `'Active'`
60
+
61
+ `Status.to_a` will return an array of enums: `[1, 2, 3]`
62
+
63
+ `Status.to_localized_a` will return an array of translated enums: `['Ative', 'Archived', 'Deleted']`
64
+
65
+ `Status.to_localized_hash` will return a hash with translated enums: `{ "Active" => "1", "Archived" => "2", "Deleted" => "3" }`
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/fake_enum.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fake_enum/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "fake_enum"
8
+ gem.version = FakeEnum::VERSION
9
+ gem.authors = ["Flip Studio"]
10
+ gem.email = ["contato@flipstudio.net"]
11
+ gem.description = "A gem which define enum classes with constants."
12
+ gem.summary = "Define enum classes with constants."
13
+ gem.homepage = "https://github.com/flipstudio/fake_enum"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,3 @@
1
+ module FakeEnum
2
+ VERSION = "0.0.2"
3
+ end
data/lib/fake_enum.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "fake_enum/version"
2
+
3
+ module FakeEnum
4
+ def to_s enum
5
+ self.constants[self.to_a.index(enum)].to_s
6
+ end
7
+
8
+ def to_sym enum
9
+ self.constants[self.to_a.index(enum)].to_s.underscore.to_sym
10
+ end
11
+
12
+ def localize enum
13
+ I18n.t(to_sym(enum), :scope => localization_scope)
14
+ end
15
+
16
+ def to_a
17
+ self.constants.map { |c| self.const_get c }
18
+ end
19
+
20
+ def to_localized_hash
21
+ Hash[self.to_a.map { |r| [localize(r), r.to_s] }]
22
+ end
23
+
24
+ def to_localized_a
25
+ to_a.map { |r| localize r }
26
+ end
27
+
28
+ def from_s string
29
+ self.const_get string
30
+ end
31
+
32
+ private
33
+ def localization_scope
34
+ [:activerecord, :fake_enums, self.name.underscore.to_sym]
35
+ end
36
+ end
Binary file
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fake_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Flip Studio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem which define enum classes with constants.
15
+ email:
16
+ - contato@flipstudio.net
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - fake_enum.gemspec
27
+ - lib/fake_enum.rb
28
+ - lib/fake_enum/version.rb
29
+ - pkg/fake_enum-0.0.1.gem
30
+ homepage: https://github.com/flipstudio/fake_enum
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Define enum classes with constants.
54
+ test_files: []