object_sugar 0.0.12 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/object_sugar/object_sugar.rb +32 -10
- data/lib/object_sugar/version.rb +1 -1
- data/lib/object_sugar.rb +3 -3
- data/object_sugar.gemspec +10 -10
- metadata +5 -5
@@ -4,7 +4,7 @@ module ObjectSugar
|
|
4
4
|
# Setup
|
5
5
|
|
6
6
|
def self.setup!
|
7
|
-
|
7
|
+
extend_object_with_constant_helpers!
|
8
8
|
extend_class_and_module_with_constants!
|
9
9
|
extend_class_and_module_with_value_enums!
|
10
10
|
extend_class_and_module_with_bitwise_enums!
|
@@ -13,7 +13,7 @@ module ObjectSugar
|
|
13
13
|
##
|
14
14
|
# Extend object with helper methods to create class/module constants
|
15
15
|
|
16
|
-
def self.
|
16
|
+
def self.extend_object_with_constant_helpers!
|
17
17
|
Object.class_eval do
|
18
18
|
private
|
19
19
|
|
@@ -23,7 +23,9 @@ module ObjectSugar
|
|
23
23
|
# Also accepts hashes as key/value pairs for constant
|
24
24
|
#
|
25
25
|
# class Foo
|
26
|
-
#
|
26
|
+
# attr_accessor :my_constants
|
27
|
+
#
|
28
|
+
# object_constants :my_constants, :one, :two, :three => "tre", :define_predicates => true
|
27
29
|
# end
|
28
30
|
#
|
29
31
|
# Will create the following constants, values on class Foo
|
@@ -31,12 +33,19 @@ module ObjectSugar
|
|
31
33
|
# Foo::MyConstants::ONE => ONE
|
32
34
|
# Foo::MyConstants::TWO => TWO
|
33
35
|
# Foo::MyConstants::THREE => tre
|
36
|
+
#
|
37
|
+
# foo = Foo.new
|
38
|
+
# foo.my_constants_one? => false
|
39
|
+
# foo.my_consatnts = Foo::MyConstants::ONE
|
40
|
+
# foo.my_constants_one? => true
|
34
41
|
|
35
42
|
def create_constants(name, *args)
|
36
43
|
const = name.to_s.camelize.to_sym
|
37
44
|
klass = const_defined?(const) ? const_get(const) : const_set(name.to_s.camelize.to_sym, Class.new)
|
38
|
-
klass.extend
|
45
|
+
klass.extend(ObjectSugar::InstanceMethods)
|
39
46
|
|
47
|
+
define_predicates = args.last.is_a?(Hash) ? args.last.delete(:define_predicates) : false
|
48
|
+
|
40
49
|
args.flatten.each do |enum|
|
41
50
|
if enum.is_a?(Hash)
|
42
51
|
enum.each { |key, value| klass.const_set(key.to_s.underscore.upcase, value) }
|
@@ -46,6 +55,12 @@ module ObjectSugar
|
|
46
55
|
end
|
47
56
|
end
|
48
57
|
|
58
|
+
if define_predicates
|
59
|
+
klass.constants.each do |constant|
|
60
|
+
define_method("#{name}_#{constant.underscore}?") { send(name) == klass.const_get(constant) }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
49
64
|
klass
|
50
65
|
end
|
51
66
|
|
@@ -58,6 +73,8 @@ module ObjectSugar
|
|
58
73
|
klass = const_defined?(const) ? const_get(const) : const_set(name.to_s.camelize.to_sym, Class.new)
|
59
74
|
klass.extend ObjectSugar::InstanceMethods
|
60
75
|
|
76
|
+
define_predicates = args.extract_options!.delete(:define_predicates)
|
77
|
+
|
61
78
|
offset = klass.constants.size
|
62
79
|
|
63
80
|
args.flatten.each_with_index do |const, index|
|
@@ -66,6 +83,12 @@ module ObjectSugar
|
|
66
83
|
klass.const_set(const.to_s.underscore.upcase, value.to_i)
|
67
84
|
end
|
68
85
|
|
86
|
+
if define_predicates
|
87
|
+
klass.constants.each do |constant|
|
88
|
+
define_method("#{name}_#{constant.underscore}?") { send(name) == klass.const_get(constant) }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
69
92
|
klass
|
70
93
|
end
|
71
94
|
|
@@ -162,17 +185,16 @@ module ObjectSugar
|
|
162
185
|
##
|
163
186
|
# Find Name
|
164
187
|
|
165
|
-
def
|
166
|
-
|
167
|
-
_found ? _found.first : nil
|
188
|
+
def find_by_name(name)
|
189
|
+
find { |k,_| k == name }
|
168
190
|
end
|
169
191
|
|
170
192
|
##
|
171
193
|
# Find Value
|
172
194
|
|
173
|
-
def
|
174
|
-
|
175
|
-
_found ? _found.last : nil
|
195
|
+
def find_by_value(value)
|
196
|
+
find { |_,v| v == value }
|
176
197
|
end
|
177
198
|
end
|
199
|
+
|
178
200
|
end
|
data/lib/object_sugar/version.rb
CHANGED
data/lib/object_sugar.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
2
|
-
gem
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'activesupport'
|
3
3
|
require 'activesupport'
|
4
4
|
|
5
5
|
include ActiveSupport::Inflector
|
6
6
|
|
7
|
-
require File.join(File.dirname(__FILE__),
|
7
|
+
require File.join(File.dirname(__FILE__), 'object_sugar', 'object_sugar')
|
8
8
|
|
9
9
|
ObjectSugar.setup!
|
data/object_sugar.gemspec
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path(
|
3
|
-
require
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'object_sugar/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
6
|
+
s.name = 'object_sugar'
|
7
7
|
s.version = ObjectSugar::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = [
|
10
|
-
s.email = [
|
11
|
-
s.homepage =
|
9
|
+
s.authors = ['Robert Zotter']
|
10
|
+
s.email = ['robertzotter@gmail.com']
|
11
|
+
s.homepage = 'http://github.com/zapient/object-sugar'
|
12
12
|
s.summary = %q{Object sweetness}
|
13
13
|
s.description = %q{Object sweetness}
|
14
14
|
s.files = %w(Rakefile object_sugar.gemspec)
|
15
|
-
s.files += Dir.glob(
|
16
|
-
s.require_paths = [
|
15
|
+
s.files += Dir.glob('lib/**/*')
|
16
|
+
s.require_paths = ['lib']
|
17
17
|
|
18
|
-
s.add_dependency
|
19
|
-
end
|
18
|
+
s.add_dependency 'activesupport', '> 1.0'
|
19
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Robert Zotter
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-12-08 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -46,7 +46,7 @@ files:
|
|
46
46
|
- lib/object_sugar/version.rb
|
47
47
|
- lib/object_sugar.rb
|
48
48
|
has_rdoc: true
|
49
|
-
homepage: http://github.com/zapient/
|
49
|
+
homepage: http://github.com/zapient/object-sugar
|
50
50
|
licenses: []
|
51
51
|
|
52
52
|
post_install_message:
|