object_sugar 0.0.10 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,178 @@
1
+ module ObjectSugar
2
+
3
+ ##
4
+ # Setup
5
+
6
+ def self.setup!
7
+ extend_object_with_constant_constant_helpers!
8
+ extend_class_and_module_with_constants!
9
+ extend_class_and_module_with_value_enums!
10
+ extend_class_and_module_with_bitwise_enums!
11
+ end
12
+
13
+ ##
14
+ # Extend object with helper methods to create class/module constants
15
+
16
+ def self.extend_object_with_constant_constant_helpers!
17
+ Object.class_eval do
18
+ private
19
+
20
+ ##
21
+ # Creates a class +name+ and creates constants for each +args+
22
+ #
23
+ # Also accepts hashes as key/value pairs for constant
24
+ #
25
+ # class Foo
26
+ # object_constants :my_constants, :one, :two, :three => "tre"
27
+ # end
28
+ #
29
+ # Will create the following constants, values on class Foo
30
+ #
31
+ # Foo::MyConstants::ONE => ONE
32
+ # Foo::MyConstants::TWO => TWO
33
+ # Foo::MyConstants::THREE => tre
34
+
35
+ def create_constants(name, *args)
36
+ const = name.to_s.camelize.to_sym
37
+ klass = const_defined?(const) ? const_get(const) : const_set(name.to_s.camelize.to_sym, Class.new)
38
+ klass.extend ObjectSugar::InstanceMethods
39
+
40
+ args.flatten.each do |enum|
41
+ if enum.is_a?(Hash)
42
+ enum.each { |key, value| klass.const_set(key.to_s.underscore.upcase, value) }
43
+ else
44
+ key, value = Array.new(2, enum.to_s.underscore.upcase)
45
+ klass.const_set(key, value)
46
+ end
47
+ end
48
+
49
+ klass
50
+ end
51
+
52
+ ##
53
+ # Creates a class +name+ on self and creates a simple enumeration whereas
54
+ # each subsequent argument in +args+ has a value +factor+^index
55
+
56
+ def create_enum(name, factor, *args)
57
+ const = name.to_s.camelize.to_sym
58
+ klass = const_defined?(const) ? const_get(const) : const_set(name.to_s.camelize.to_sym, Class.new)
59
+ klass.extend ObjectSugar::InstanceMethods
60
+
61
+ offset = klass.constants.size
62
+
63
+ args.flatten.each_with_index do |const, index|
64
+ value = (factor == 1) ? (factor * (index + offset)) : (factor.power!(index + offset))
65
+
66
+ klass.const_set(const.to_s.underscore.upcase, value.to_i)
67
+ end
68
+
69
+ klass
70
+ end
71
+
72
+ ##
73
+ # Create a constant with a name of the pluralized version of +name+
74
+ # that returns all of the class constants of +name+
75
+ #
76
+ # class Foo
77
+ # object_constants :my_constants, :one, :two, :three
78
+ # end
79
+ #
80
+ # Will add the following constant on class Foo
81
+ #
82
+ # Foo::MY_CONSTANTS
83
+ #
84
+ # Which would return
85
+ # => ONE, TWO, THREE
86
+
87
+ def create_pluralized_constant(name)
88
+ pluralized = ActiveSupport::Inflector.pluralize(name.to_s).upcase.to_sym
89
+ constants = const_get(name.to_s.camelize.to_sym).constants
90
+
91
+ const_set(pluralized, constants)
92
+ end
93
+ end
94
+ end
95
+
96
+ ##
97
+ # Extend with #object_constants
98
+
99
+ def self.extend_class_and_module_with_constants!
100
+ [Class, Module].each do |object|
101
+ object.class_eval do
102
+ def object_constants(name, *args)
103
+ create_constants(name, *args)
104
+ create_pluralized_constant(name)
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ ##
111
+ # Extend with #value_enums
112
+
113
+ def self.extend_class_and_module_with_value_enums!
114
+ [Class, Module].each do |object|
115
+ object.class_eval do
116
+ def value_enums(name, *args)
117
+ create_enum(name, 1, *args)
118
+ create_pluralized_constant(name)
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ ##
125
+ # Extend with #bitwise_enums
126
+
127
+ def self.extend_class_and_module_with_bitwise_enums!
128
+ [Class, Module].each do |object|
129
+ object.class_eval do
130
+ def bitwise_enums(name, *args)
131
+ create_enum(name, 2, *args )
132
+ create_pluralized_constant(name)
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ module InstanceMethods
139
+ include Enumerable
140
+
141
+ ##
142
+ # Iterate over each const name and value
143
+
144
+ def each(&block)
145
+ constants.each { |c| yield(c, const_get(c)) }
146
+ end
147
+
148
+ ##
149
+ # Names
150
+
151
+ def names
152
+ map(&:first)
153
+ end
154
+
155
+ ##
156
+ # Values
157
+
158
+ def values
159
+ map(&:last)
160
+ end
161
+
162
+ ##
163
+ # Find Name
164
+
165
+ def find_name(&block)
166
+ _found = find(&block)
167
+ _found ? _found.first : nil
168
+ end
169
+
170
+ ##
171
+ # Find Value
172
+
173
+ def find_value(&block)
174
+ _found = find(&block)
175
+ _found ? _found.last : nil
176
+ end
177
+ end
178
+ end
@@ -1,3 +1,3 @@
1
1
  module ObjectSugar
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.12"
3
3
  end
data/lib/object_sugar.rb CHANGED
@@ -1,180 +1,9 @@
1
- module ObjectSugar
1
+ require "rubygems"
2
+ gem "activesupport"
3
+ require 'activesupport'
2
4
 
3
- ##
4
- # Setup
5
+ include ActiveSupport::Inflector
5
6
 
6
- def self.setup!
7
- extend_object_with_constant_constant_helpers!
8
- extend_class_and_module_with_constants!
9
- extend_class_and_module_with_value_enums!
10
- extend_class_and_module_with_bitwise_enums!
11
- end
12
-
13
- ##
14
- # Extend object with helper methods to create class/module constants
15
-
16
- def self.extend_object_with_constant_constant_helpers!
17
- Object.class_eval do
18
- private
19
-
20
- ##
21
- # Creates a class +name+ and creates constants for each +args+
22
- #
23
- # Also accepts hashes as key/value pairs for constant
24
- #
25
- # class Foo
26
- # object_constants :my_constants, :one, :two, :three => "tre"
27
- # end
28
- #
29
- # Will create the following constants, values on class Foo
30
- #
31
- # Foo::MyConstants::ONE => ONE
32
- # Foo::MyConstants::TWO => TWO
33
- # Foo::MyConstants::THREE => tre
34
-
35
- def create_constants(name, *args)
36
- const = name.to_s.camelize.to_sym
37
- klass = const_defined?(const) ? const_get(const) : const_set(name.to_s.camelize.to_sym, Class.new)
38
- klass.extend ObjectSugar::InstanceMethods
39
-
40
- args.flatten.each do |enum|
41
- if enum.is_a?(Hash)
42
- enum.each { |key, value| klass.const_set(key.to_s.underscore.upcase, value) }
43
- else
44
- key, value = Array.new(2, enum.to_s.underscore.upcase)
45
- klass.const_set(key, value)
46
- end
47
- end
48
-
49
- klass
50
- end
51
-
52
- ##
53
- # Creates a class +name+ on self and creates a simple enumeration whereas
54
- # each subsequent argument in +args+ has a value +factor+^index
55
-
56
- def create_enum(name, factor, *args)
57
- const = name.to_s.camelize.to_sym
58
- klass = const_defined?(const) ? const_get(const) : const_set(name.to_s.camelize.to_sym, Class.new)
59
- klass.extend ObjectSugar::InstanceMethods
60
-
61
- offset = klass.constants.size
62
-
63
- args.flatten.each_with_index { |const, index|
64
- value = (factor == 1) ? (factor * (index + offset)) : (factor.power!(index + offset))
65
-
66
- klass.const_set(const.to_s.underscore.upcase, value.to_i)
67
- }
68
-
69
- klass
70
- end
71
-
72
- ##
73
- # Create a constant with a name of the pluralized version of +name+
74
- # that returns all of the class constants of +name+
75
- #
76
- # class Foo
77
- # object_constants :my_constants, :one, :two, :three
78
- # end
79
- #
80
- # Will add the following constant on class Foo
81
- #
82
- # Foo::MY_CONSTANTS
83
- #
84
- # Which would return
85
- # => ONE, TWO, THREE
86
-
87
- def create_pluralized_constant(name)
88
- pluralized = ActiveSupport::Inflector.pluralize(name.to_s).upcase.to_sym
89
- constants = const_get(name.to_s.camelize.to_sym).constants
90
-
91
- const_set(pluralized, constants)
92
- end
93
- end
94
- end
95
-
96
- ##
97
- # Extend with #object_constants
98
-
99
- def self.extend_class_and_module_with_constants!
100
- [Class, Module].each do |object|
101
- object.class_eval do
102
- def object_constants(name, *args)
103
- create_constants(name, *args)
104
- create_pluralized_constant(name)
105
- end
106
- end
107
- end
108
- end
109
-
110
- ##
111
- # Extend with #value_enums
112
-
113
- def self.extend_class_and_module_with_value_enums!
114
- [Class, Module].each do |object|
115
- object.class_eval do
116
- def value_enums(name, *args)
117
- create_enum(name, 1, *args)
118
- create_pluralized_constant(name)
119
- end
120
- end
121
- end
122
- end
123
-
124
- ##
125
- # Extend with #bitwise_enums
126
-
127
- def self.extend_class_and_module_with_bitwise_enums!
128
- [Class, Module].each do |object|
129
- object.class_eval do
130
- def bitwise_enums(name, *args)
131
- create_enum(name, 2, *args )
132
- create_pluralized_constant(name)
133
- end
134
- end
135
- end
136
- end
137
-
138
- module InstanceMethods
139
- include Enumerable
140
-
141
- ##
142
- # Iterate over each const name and value
143
-
144
- def each(&block)
145
- constants.each { |c| yield(c, const_get(c)) }
146
- end
147
-
148
- ##
149
- # Names
150
-
151
- def names
152
- map(&:first)
153
- end
154
-
155
- ##
156
- # Values
157
-
158
- def values
159
- map(&:last)
160
- end
161
-
162
- ##
163
- # Find Name
164
-
165
- def find_name(&block)
166
- _found = find(&block)
167
- _found ? _found.first : nil
168
- end
169
-
170
- ##
171
- # Find Value
172
-
173
- def find_value(&block)
174
- _found = find(&block)
175
- _found ? _found.last : nil
176
- end
177
- end
178
- end
7
+ require File.join(File.dirname(__FILE__), "object_sugar", "object_sugar")
179
8
 
180
9
  ObjectSugar.setup!
data/object_sugar.gemspec CHANGED
@@ -3,19 +3,17 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
  require "object_sugar/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "object_sugar"
7
- s.version = ObjectSugar::VERSION
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = ["Robert Zotter"]
10
- s.email = ["robertzotter@ioffer.com"]
11
- s.homepage = ""
12
- s.summary = %q{Object sweetness}
13
- s.description = %q{Object sweetness}
14
-
15
- s.rubyforge_project = "object_sugar"
16
-
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
6
+ s.name = "object_sugar"
7
+ s.version = ObjectSugar::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Robert Zotter"]
10
+ s.email = ["robertzotter@gmail.com"]
11
+ s.homepage = "http://github.com/zapient/Object-Sugar"
12
+ s.summary = %q{Object sweetness}
13
+ s.description = %q{Object sweetness}
14
+ s.files = %w(Rakefile object_sugar.gemspec)
15
+ s.files += Dir.glob("lib/**/*")
20
16
  s.require_paths = ["lib"]
17
+
18
+ s.add_dependency "activesupport", "> 1.0"
21
19
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 10
9
- version: 0.0.10
8
+ - 12
9
+ version: 0.0.12
10
10
  platform: ruby
11
11
  authors:
12
12
  - Robert Zotter
@@ -14,13 +14,25 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-21 00:00:00 -07:00
17
+ date: 2011-03-22 00:00:00 -07:00
18
18
  default_executable:
19
- dependencies: []
20
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">"
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ version: "1.0"
31
+ type: :runtime
32
+ version_requirements: *id001
21
33
  description: Object sweetness
22
34
  email:
23
- - robertzotter@ioffer.com
35
+ - robertzotter@gmail.com
24
36
  executables: []
25
37
 
26
38
  extensions: []
@@ -28,14 +40,13 @@ extensions: []
28
40
  extra_rdoc_files: []
29
41
 
30
42
  files:
31
- - .gitignore
32
- - Gemfile
33
43
  - Rakefile
34
- - lib/object_sugar.rb
35
- - lib/object_sugar/version.rb
36
44
  - object_sugar.gemspec
45
+ - lib/object_sugar/object_sugar.rb
46
+ - lib/object_sugar/version.rb
47
+ - lib/object_sugar.rb
37
48
  has_rdoc: true
38
- homepage: ""
49
+ homepage: http://github.com/zapient/Object-Sugar
39
50
  licenses: []
40
51
 
41
52
  post_install_message:
@@ -59,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
70
  version: "0"
60
71
  requirements: []
61
72
 
62
- rubyforge_project: object_sugar
73
+ rubyforge_project:
63
74
  rubygems_version: 1.3.6
64
75
  signing_key:
65
76
  specification_version: 3
data/.gitignore DELETED
@@ -1,4 +0,0 @@
1
- *.gem
2
- .bundle
3
- Gemfile.lock
4
- pkg/*
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in object_sugar.gemspec
4
- gemspec