object_sugar 0.0.6 → 0.0.7

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,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 object_sugar.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ module ObjectSugar
2
+ VERSION = "0.0.7"
3
+ end
@@ -0,0 +1,176 @@
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
+ klass = const_set( name.to_s.camelize.to_sym, Class.new )
37
+ klass.extend ObjectSugar::InstanceMethods
38
+
39
+ args.flatten.each do |enum|
40
+ if enum.is_a?(Hash)
41
+ enum.each { |key, value| klass.const_set(key.to_s.underscore.upcase, value) }
42
+ else
43
+ key, value = Array.new(2, enum.to_s.underscore.upcase)
44
+ klass.const_set(key, value)
45
+ end
46
+ end
47
+
48
+ klass
49
+ end
50
+
51
+ ##
52
+ # Creates a class +name+ on self and creates a simple enumeration whereas
53
+ # each subsequent argument in +args+ has a value +factor+^index
54
+
55
+ def create_enum(name, factor, *args)
56
+ klass = const_set( name.to_s.camelize.to_sym, Class.new )
57
+ klass.extend ObjectSugar::InstanceMethods
58
+
59
+ args.flatten.each_with_index { |const, index|
60
+ value = (factor == 1) ? (factor * index) : (factor.power!( index ))
61
+
62
+ klass.const_set( const.to_s.underscore.upcase, value.to_i )
63
+ }
64
+
65
+ klass
66
+ end
67
+
68
+ ##
69
+ # Create a constant with a name of the pluralized version of +name+
70
+ # that returns all of the class constants of +name+
71
+ #
72
+ # class Foo
73
+ # object_constants :my_constants, :one, :two, :three
74
+ # end
75
+ #
76
+ # Will add the following constant on class Foo
77
+ #
78
+ # Foo::MY_CONSTANTS
79
+ #
80
+ # Which would return
81
+ # => ONE, TWO, THREE
82
+
83
+ def create_pluralized_constant(name)
84
+ pluralized = ActiveSupport::Inflector.pluralize( name.to_s ).upcase.to_sym
85
+ constants = const_get( name.to_s.camelize.to_sym ).constants
86
+
87
+ const_set( pluralized, constants ) unless const_defined?( pluralized )
88
+ end
89
+ end
90
+ end
91
+
92
+ ##
93
+ # Extend with #object_constants
94
+
95
+ def self.extend_class_and_module_with_constants!
96
+ [Class, Module].each do |object|
97
+ object.class_eval do
98
+ def object_constants(name, *args)
99
+ create_constants(name, *args)
100
+ create_pluralized_constant(name)
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ ##
107
+ # Extend with #value_enums
108
+
109
+ def self.extend_class_and_module_with_value_enums!
110
+ [Class, Module].each do |object|
111
+ object.class_eval do
112
+ def value_enums(name, *args)
113
+ create_enum( name, 1, *args )
114
+ create_pluralized_constant( name )
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ ##
121
+ # Extend with #bitwise_enums
122
+
123
+ def self.extend_class_and_module_with_bitwise_enums!
124
+ [Class, Module].each do |object|
125
+ object.class_eval do
126
+ def bitwise_enums(name, *args)
127
+ create_enum( name, 2, *args )
128
+ create_pluralized_constant( name )
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ module InstanceMethods
135
+ include Enumerable
136
+
137
+ ##
138
+ # Iterate over each const name and value
139
+
140
+ def each(&block)
141
+ constants.each { |c| yield(c, const_get(c)) }
142
+ end
143
+
144
+ ##
145
+ # Names
146
+
147
+ def names
148
+ map(&:first)
149
+ end
150
+
151
+ ##
152
+ # Values
153
+
154
+ def values
155
+ map(&:last)
156
+ end
157
+
158
+ ##
159
+ # Find Name
160
+
161
+ def find_name(&block)
162
+ _found = find(&block)
163
+ _found ? _found.first : nil
164
+ end
165
+
166
+ ##
167
+ # Find Value
168
+
169
+ def find_value(&block)
170
+ _found = find(&block)
171
+ _found ? _found.last : nil
172
+ end
173
+ end
174
+ end
175
+
176
+ ObjectSugar.setup!
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "object_sugar/version"
4
+
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) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- version: 0.0.6
8
+ - 7
9
+ version: 0.0.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Robert Zotter
@@ -27,8 +27,13 @@ extensions: []
27
27
 
28
28
  extra_rdoc_files: []
29
29
 
30
- files: []
31
-
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - lib/object_sugar.rb
35
+ - lib/object_sugar/version.rb
36
+ - object_sugar.gemspec
32
37
  has_rdoc: true
33
38
  homepage: ""
34
39
  licenses: []