easy_class_constants 0.0.1

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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_class_constants.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nina R.
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,53 @@
1
+ # EasyClassConstants
2
+
3
+ Gem adds class methods to create readable, writable and accessible constants.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'easy_class_constants'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install easy_class_constants
18
+
19
+ ## Usage
20
+
21
+ Adding constants to your class is easy:
22
+
23
+ class ECCObject
24
+ include EasyClassConstants
25
+
26
+ readable_constant :read_only, 'R'
27
+ writable_constant :write_only, 'W'
28
+ accessible_constant :read_write, 'RW'
29
+ hash_indexed_by_readable_constants(:mapping, [[:internal_error, 0, 'Internal Error - Try Again Later'],
30
+ [:network_error, 1, 'Network Error - Check With Your Admin']])
31
+
32
+ hash_indexed_by_readable_constants(:simple_mapping, [[:HELLO_WORLD, 'Hello World'],
33
+ [:HELLO_KITTY, 'Hello Kitty']])
34
+
35
+ end
36
+
37
+ Then you can call:
38
+
39
+ ECCTestObject.INTERNAL_ERROR #will return 0
40
+ ECCTestObject.SIMPLE_MAPPING[:HELLO_KITTY] # will return 'Hello Kitty'
41
+ ECCTestObject.SIMPLE_MAPPING.HELLO_KITTY # will return 'Hello Kitty'
42
+ ECCTestObject.READ_WRITE # will return 'RW'
43
+
44
+
45
+ # Credits
46
+
47
+ [Oha_extensions](https://github.com/bkr/oha_extensions) is maintained by [Bookrenter/Rafter](http://github.com/bkr) and is funded by [BookRenter.com](http://www.bookrenter.com "BookRenter.com").
48
+
49
+ ![BookRenter.com Logo](http://assets0.bookrenter.com/images/header/bookrenter_logo.gif "BookRenter.com")
50
+
51
+ # Copyright
52
+
53
+ Copyright (c) 2012 Bookrenter.com. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/easy_class_constants/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Bookrenter/Rafter"]
6
+ gem.email = ["cp@bookrenter.com"]
7
+ gem.description = %q{Gem adds class methods to create readable, writable and accessible constants.}
8
+ gem.summary = %q{Gem adds class methods to create readable, writable and accessible constants.}
9
+ gem.homepage = "https://github.com/bkr/easy_class_constants"
10
+
11
+ gem.add_dependency('rails', '> 0')
12
+ gem.add_development_dependency('mocha', '> 0')
13
+ gem.add_development_dependency('shoulda', "> 0")
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.name = "easy_class_constants"
19
+ gem.require_paths = ["lib"]
20
+ gem.version = EasyClassConstants::VERSION
21
+ end
@@ -0,0 +1,54 @@
1
+ require "easy_class_constants/version"
2
+
3
+ module EasyClassConstants
4
+
5
+ def self.included(receiver)
6
+ receiver.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def readable_constant(name, value)
11
+ name = name.to_s.upcase
12
+ self.send(:class_variable_set, "@@#{name}", value)
13
+ self.send(:cattr_reader, name)
14
+ end
15
+
16
+ def writable_constant(name, value)
17
+ name = name.to_s.upcase
18
+ self.send(:class_variable_set, "@@#{name}", value)
19
+ self.send(:cattr_writer, name)
20
+ end
21
+
22
+ def accessible_constant(name, value)
23
+ name = name.to_s.upcase
24
+ self.send(:class_variable_set, "@@#{name}", value)
25
+ self.send(:cattr_accessor, name)
26
+ end
27
+
28
+ def hash_indexed_by_readable_constants(hash_variable_name, hash_contents)
29
+ hash_variable_name = hash_variable_name.to_s.upcase
30
+ self.send(:class_variable_set, "@@#{hash_variable_name}", ActiveSupport::OrderedHash.new)
31
+ self.send(:cattr_reader, hash_variable_name)
32
+ hash_contents.each do |*args|
33
+ args.flatten!
34
+ constant_name = args[0].to_s.upcase
35
+
36
+ if args.size > 2
37
+ hash_index = args[1]
38
+ hash_value = args[2]
39
+ variable_value = hash_index
40
+ else
41
+ hash_index = args[0]
42
+ hash_value = args[1]
43
+ variable_value = hash_value
44
+ end
45
+
46
+
47
+ puts "WARNING: in .hash_indexed_by_readable_constants. Constant is already defined: #{constant_name}" if (self.send(:class_variable_get, "@@#{constant_name}").present? rescue false)
48
+ self.send(:class_variable_set, "@@#{constant_name}", variable_value)
49
+ self.send(:class_variable_get, "@@#{hash_variable_name}")[hash_index] = hash_value
50
+ self.send(:cattr_reader, constant_name)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module EasyClassConstants
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,75 @@
1
+ require "test_helper"
2
+
3
+ class EasyClassConstantsTest < Test::Unit::TestCase
4
+ class ECCTestObject
5
+ include EasyClassConstants
6
+
7
+ readable_constant :read_only, 'R'
8
+ writable_constant :write_only, 'W'
9
+ accessible_constant :read_write, 'RW'
10
+ hash_indexed_by_readable_constants(:mapping, [[:internal_error, 0, 'Internal Error - Try Again Later'],
11
+ [:network_error, 1, 'Network Error - Check With Your Admin']])
12
+
13
+ hash_indexed_by_readable_constants(:simple_mapping, [[:HELLO_WORLD, 'Hello World'],
14
+ [:HELLO_KITTY, 'Hello Kitty']])
15
+
16
+ end
17
+
18
+ context "hash_indexed_by_readable_constants" do
19
+ context "with 3 arg mapping" do
20
+ should "create readable constant from created hash" do
21
+ assert_equal 'Internal Error - Try Again Later', ECCTestObject.MAPPING[0]
22
+ assert_equal 'Network Error - Check With Your Admin', ECCTestObject.MAPPING[1]
23
+ end
24
+
25
+ should "create constants for each constant name" do
26
+ assert_equal 0, ECCTestObject.INTERNAL_ERROR
27
+ assert_equal 1, ECCTestObject.NETWORK_ERROR
28
+ end
29
+ end
30
+
31
+ context "with 2 arg mapping" do
32
+ should "create readable constant from created hash" do
33
+ assert_equal 'Hello World', ECCTestObject.SIMPLE_MAPPING[:HELLO_WORLD]
34
+ assert_equal 'Hello Kitty', ECCTestObject.SIMPLE_MAPPING[:HELLO_KITTY]
35
+ end
36
+
37
+ should "create constants for each constant name" do
38
+ assert_equal 'Hello World', ECCTestObject.HELLO_WORLD
39
+ assert_equal 'Hello Kitty', ECCTestObject.HELLO_KITTY
40
+ end
41
+ end
42
+ end
43
+
44
+ context "readable_constant" do
45
+ should "be readable" do
46
+ assert_equal 'R', ECCTestObject.READ_ONLY
47
+ end
48
+
49
+ should "not be writeable" do
50
+ assert_raise(NoMethodError) { ECCTestObject.READ_ONLY = 1 }
51
+ end
52
+ end
53
+
54
+ context "writable_constant" do
55
+ should "not be readable" do
56
+ assert_raise(NoMethodError) { ECCTestObject.WRITE_ONLY }
57
+ end
58
+
59
+ should "be writeable" do
60
+ ECCTestObject.WRITE_ONLY = 1
61
+ assert_equal 1, ECCTestObject.send(:class_variable_get, '@@WRITE_ONLY')
62
+ end
63
+ end
64
+
65
+ context "accessible_constant" do
66
+ should "be readable" do
67
+ assert_equal 'RW', ECCTestObject.READ_WRITE
68
+ end
69
+
70
+ should "be writeable" do
71
+ ECCTestObject.READ_WRITE = 1
72
+ assert_equal 1, ECCTestObject.READ_WRITE
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'test/unit'
12
+ require 'shoulda'
13
+ require 'mocha'
14
+ require 'rails'
15
+
16
+ require File.dirname(__FILE__) + '/../lib/easy_class_constants'
17
+
18
+ require 'easy_class_constants'
19
+
20
+ class Test::Unit::TestCase
21
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_class_constants
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bookrenter/Rafter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>'
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>'
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>'
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>'
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>'
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Gem adds class methods to create readable, writable and accessible constants.
63
+ email:
64
+ - cp@bookrenter.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - easy_class_constants.gemspec
75
+ - lib/easy_class_constants.rb
76
+ - lib/easy_class_constants/version.rb
77
+ - test/easy_class_constants_test.rb
78
+ - test/test_helper.rb
79
+ homepage: https://github.com/bkr/easy_class_constants
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Gem adds class methods to create readable, writable and accessible constants.
103
+ test_files:
104
+ - test/easy_class_constants_test.rb
105
+ - test/test_helper.rb