active_enumeration 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 223d109ab6d201e3f8584ebc9fe3ba20402a4e4d
4
+ data.tar.gz: fa0283d584c8b94ef1c6cd490ad6c50d4920e5d8
5
+ SHA512:
6
+ metadata.gz: 1f4116d54cc911e9f352079440806058a4404ad8639cf93de1a10f9fb54d0083e2ed96e80d5396a17e79815d3184bedb5db9a0c0dcf37f505d64054e04b49d21
7
+ data.tar.gz: 9c297672df23f9394cea61922e633e06088201438ffcf59ee0383afdeaefe3d47164eeec97c6982ae018e46e9e82f329f8910c941fed45e25dec5948bb666a36
@@ -0,0 +1,134 @@
1
+ require 'active_support/inflector'
2
+
3
+ module ActiveEnumeration
4
+ class Base
5
+
6
+ def self.find(id)
7
+ id = id.to_i
8
+ @instances ||= {}
9
+ unless @instances[id] || !@active_enumeration_values[id]
10
+ @instances[id] = self.new *@active_enumeration_values[id]
11
+ end
12
+ @instances[id]
13
+ end
14
+
15
+ def self.all
16
+ @active_enumeration_values.keys.map { |id| self.find(id) }
17
+ end
18
+
19
+ def self.count
20
+ @active_enumeration_values.count
21
+ end
22
+
23
+ def self.to_a
24
+ all.map { |e| [e.to_s, e.instance_variable_get("@active_enumeration_id")] }
25
+ end
26
+
27
+ def symbol
28
+ self.instance_variable_get("@symbol")
29
+ end
30
+
31
+ protected
32
+
33
+ def self.attr_reader(*attributes)
34
+ super
35
+ @active_enumeration_attributes ||= []
36
+ @active_enumeration_attributes.concat(attributes.map(&:to_sym))
37
+ end
38
+
39
+ def initialize(*attributes)
40
+ key_index = self.class.active_enumeration_index_location
41
+ self.instance_variable_set("@active_enumeration_id", attributes[key_index].to_i)
42
+
43
+ self.class.active_enumeration_attributes.each_with_index do |attr_name, index|
44
+ self.instance_variable_set("@#{attr_name}", attributes[index])
45
+ end
46
+ end
47
+
48
+ def self.active_enumeration_attributes
49
+ @active_enumeration_attributes
50
+ end
51
+
52
+ def self.active_enumeration_symbols
53
+ @active_enumeration_symbols
54
+ end
55
+
56
+ def self.active_enumeration_index_location
57
+ active_enumeration_attributes.index(:id) || 0
58
+ end
59
+
60
+ def self.values(values)
61
+ @active_enumeration_values ||= Hash.new
62
+ @active_enumeration_symbols ||= Hash.new
63
+
64
+ if values.class == Hash
65
+ values.each_with_index { |(k, v), i| handle_hash_values(k, v, i) }
66
+ elsif values.class == Array
67
+ values.each_with_index { |v, i| handle_array_values(v, i) }
68
+ end
69
+ end
70
+
71
+ def self.groups(hash)
72
+ hash.each do |group, ids|
73
+ self.send(:define_singleton_method, group) do
74
+ ids.map { |id| self.find(id) }
75
+ end
76
+ end
77
+ end
78
+
79
+ def self.belongs_to(name, options = {})
80
+ class_name = (options[:class_name] || name.to_s.camelize).to_s
81
+ foreign_key = (options[:foreign_key] || "#{name}_id").to_s
82
+
83
+ self.send(:define_method, name) {
84
+ klass = class_name.constantize
85
+ klass.find(self.send(foreign_key))
86
+ }
87
+
88
+ self.send(:define_singleton_method, "active_enumeration_belongs_to_#{class_name.underscore}".upcase) {
89
+ var_name = "@active_enumeration_belongs_to_#{class_name.underscore}"
90
+ return instance_variable_get(var_name) if instance_variable_get(var_name)
91
+ hash = Hash.new {Array.new}
92
+ self.all.each do |obj|
93
+ hash[obj.send(foreign_key)] = hash[obj.send(foreign_key)] << obj
94
+ end
95
+ instance_variable_set(var_name, hash)
96
+ hash
97
+ }
98
+ end
99
+
100
+ def self.has_many(name, options = {})
101
+ class_name = (options[:class_name] || name.to_s.singularize.camelize).to_s
102
+ self.send(:define_method, name) {
103
+ klass = class_name.constantize
104
+ klass.send("active_enumeration_belongs_to_#{self.class.name.underscore}".upcase)[self.id]
105
+ }
106
+ end
107
+
108
+ private
109
+
110
+ def self.handle_hash_values(k, v, i)
111
+ v = [v] unless v.class == Array
112
+ values_key = v[active_enumeration_index_location].to_i
113
+ @active_enumeration_values[values_key] = v
114
+
115
+ unless active_enumeration_attributes.index(:symbol)
116
+ @active_enumeration_symbols[values_key] = k.to_sym
117
+ self.send(:define_method, :symbol) { self.class.active_enumeration_symbols[self.instance_variable_get("@active_enumeration_id")] }
118
+ end
119
+
120
+ self.send(:define_singleton_method, k) { self.find(values_key) }
121
+
122
+ self.send(:define_method, "#{k}?") { self.instance_variable_get("@active_enumeration_id") == values_key }
123
+
124
+ const_set(k.to_s.upcase, values_key)
125
+ end
126
+
127
+ def self.handle_array_values(v, i)
128
+ v = [v] unless v.class == Array
129
+ values_key = v[active_enumeration_index_location].to_i
130
+ @active_enumeration_values[values_key] = v
131
+ end
132
+
133
+ end
134
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_support/inflector'
2
+ require 'active_enumeration/base'
3
+
4
+ module ActiveEnumeration
5
+
6
+ def self.extended(receiver)
7
+ receiver.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def has_active_enumeration_for(name, options = {})
13
+ class_name = (options[:class_name] || name.to_s.camelize).to_s
14
+ foreign_key = (options[:foreign_key] || "#{name}_id").to_s
15
+
16
+ self.send(:define_method, name) do
17
+ klass = class_name.constantize
18
+ klass.find(self.send(foreign_key))
19
+ end
20
+
21
+ self.send(:define_method, "#{name}=") do |enum_obj|
22
+ self.send("#{foreign_key}=", enum_obj.instance_variable_get("@active_enumeration_id"))
23
+ end
24
+ end
25
+
26
+ def has_active_enumerations_for(name, options = {})
27
+ class_name = (options[:class_name] || name.to_s.camelize).to_s
28
+ foreign_key = (options[:foreign_key] || "#{name.to_s.singularize}_ids").to_s
29
+
30
+ self.send(:define_method, name) do
31
+ klass = class_name.constantize
32
+ self.send(foreign_key).map { |id| klass.find(id) }
33
+ end
34
+
35
+ self.send(:define_method, "#{name}=") do |enum_objs|
36
+ self.send("#{foreign_key}=", enum_objs.map { |obj| obj.instance_variable_get("@active_enumeration_id") })
37
+ end
38
+ end
39
+ end
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_enumeration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Peter Lin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.1
27
+ description: A simpler way to create more complex Ruby enumerations, with ActiveRecord
28
+ like syntax and associations.
29
+ email: peter@peterl.in
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/active_enumeration.rb
35
+ - lib/active_enumeration/base.rb
36
+ homepage: https://github.com/ptrln/active_enumeration
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.2.2
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: ActiveEnumeration
60
+ test_files: []