maiha-dsl_accessor 0.3.0 → 0.3.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/Rakefile +1 -2
- data/core_ext/class/dsl_accessor.rb +4 -1
- data/core_ext/class/inheritable_attributes.rb +140 -0
- data/core_ext/duplicable.rb +43 -0
- data/test/default_test.rb +2 -2
- data/test/test_helper.rb +0 -9
- metadata +6 -12
data/Rakefile
CHANGED
@@ -33,7 +33,7 @@ AUTHOR = "maiha"
|
|
33
33
|
EMAIL = "maiha@wota.jp"
|
34
34
|
HOMEPAGE = "http://github.com/maiha/dsl_accessor"
|
35
35
|
SUMMARY = "This plugin gives hybrid accessor class methods to classes by DSL like definition"
|
36
|
-
GEM_VERSION = "0.3.
|
36
|
+
GEM_VERSION = "0.3.1"
|
37
37
|
|
38
38
|
spec = Gem::Specification.new do |s|
|
39
39
|
# s.rubyforge_project = 'merb'
|
@@ -49,7 +49,6 @@ spec = Gem::Specification.new do |s|
|
|
49
49
|
s.homepage = HOMEPAGE
|
50
50
|
s.require_path = 'lib'
|
51
51
|
s.files = %w(LICENSE README Rakefile) + Dir.glob("{core_ext,lib,spec,tasks,test}/**/*")
|
52
|
-
s.add_dependency('activesupport', '>= 2.0.0')
|
53
52
|
end
|
54
53
|
|
55
54
|
Rake::GemPackageTask.new(spec) do |pkg|
|
@@ -1,4 +1,7 @@
|
|
1
|
-
|
1
|
+
unless Class.new.respond_to?(:class_inheritable_accessor)
|
2
|
+
require File.dirname(__FILE__) + "/../duplicable" unless Object.new.respond_to?(:duplicable?)
|
3
|
+
require File.dirname(__FILE__) + "/inheritable_attributes"
|
4
|
+
end
|
2
5
|
|
3
6
|
module DslAccessor
|
4
7
|
def dsl_accessor(*args)
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# Retain for backward compatibility. Methods are now included in Class.
|
2
|
+
module ClassInheritableAttributes # :nodoc:
|
3
|
+
end
|
4
|
+
|
5
|
+
# Allows attributes to be shared within an inheritance hierarchy, but where each descendant gets a copy of
|
6
|
+
# their parents' attributes, instead of just a pointer to the same. This means that the child can add elements
|
7
|
+
# to, for example, an array without those additions being shared with either their parent, siblings, or
|
8
|
+
# children, which is unlike the regular class-level attributes that are shared across the entire hierarchy.
|
9
|
+
class Class # :nodoc:
|
10
|
+
def class_inheritable_reader(*syms)
|
11
|
+
syms.each do |sym|
|
12
|
+
next if sym.is_a?(Hash)
|
13
|
+
class_eval <<-EOS
|
14
|
+
def self.#{sym}
|
15
|
+
read_inheritable_attribute(:#{sym})
|
16
|
+
end
|
17
|
+
|
18
|
+
def #{sym}
|
19
|
+
self.class.#{sym}
|
20
|
+
end
|
21
|
+
EOS
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def class_inheritable_writer(*syms)
|
26
|
+
options = syms.extract_options!
|
27
|
+
syms.each do |sym|
|
28
|
+
class_eval <<-EOS
|
29
|
+
def self.#{sym}=(obj)
|
30
|
+
write_inheritable_attribute(:#{sym}, obj)
|
31
|
+
end
|
32
|
+
|
33
|
+
#{"
|
34
|
+
def #{sym}=(obj)
|
35
|
+
self.class.#{sym} = obj
|
36
|
+
end
|
37
|
+
" unless options[:instance_writer] == false }
|
38
|
+
EOS
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def class_inheritable_array_writer(*syms)
|
43
|
+
options = syms.extract_options!
|
44
|
+
syms.each do |sym|
|
45
|
+
class_eval <<-EOS
|
46
|
+
def self.#{sym}=(obj)
|
47
|
+
write_inheritable_array(:#{sym}, obj)
|
48
|
+
end
|
49
|
+
|
50
|
+
#{"
|
51
|
+
def #{sym}=(obj)
|
52
|
+
self.class.#{sym} = obj
|
53
|
+
end
|
54
|
+
" unless options[:instance_writer] == false }
|
55
|
+
EOS
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def class_inheritable_hash_writer(*syms)
|
60
|
+
options = syms.extract_options!
|
61
|
+
syms.each do |sym|
|
62
|
+
class_eval <<-EOS
|
63
|
+
def self.#{sym}=(obj)
|
64
|
+
write_inheritable_hash(:#{sym}, obj)
|
65
|
+
end
|
66
|
+
|
67
|
+
#{"
|
68
|
+
def #{sym}=(obj)
|
69
|
+
self.class.#{sym} = obj
|
70
|
+
end
|
71
|
+
" unless options[:instance_writer] == false }
|
72
|
+
EOS
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def class_inheritable_accessor(*syms)
|
77
|
+
class_inheritable_reader(*syms)
|
78
|
+
class_inheritable_writer(*syms)
|
79
|
+
end
|
80
|
+
|
81
|
+
def class_inheritable_array(*syms)
|
82
|
+
class_inheritable_reader(*syms)
|
83
|
+
class_inheritable_array_writer(*syms)
|
84
|
+
end
|
85
|
+
|
86
|
+
def class_inheritable_hash(*syms)
|
87
|
+
class_inheritable_reader(*syms)
|
88
|
+
class_inheritable_hash_writer(*syms)
|
89
|
+
end
|
90
|
+
|
91
|
+
def inheritable_attributes
|
92
|
+
@inheritable_attributes ||= EMPTY_INHERITABLE_ATTRIBUTES
|
93
|
+
end
|
94
|
+
|
95
|
+
def write_inheritable_attribute(key, value)
|
96
|
+
if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES)
|
97
|
+
@inheritable_attributes = {}
|
98
|
+
end
|
99
|
+
inheritable_attributes[key] = value
|
100
|
+
end
|
101
|
+
|
102
|
+
def write_inheritable_array(key, elements)
|
103
|
+
write_inheritable_attribute(key, []) if read_inheritable_attribute(key).nil?
|
104
|
+
write_inheritable_attribute(key, read_inheritable_attribute(key) + elements)
|
105
|
+
end
|
106
|
+
|
107
|
+
def write_inheritable_hash(key, hash)
|
108
|
+
write_inheritable_attribute(key, {}) if read_inheritable_attribute(key).nil?
|
109
|
+
write_inheritable_attribute(key, read_inheritable_attribute(key).merge(hash))
|
110
|
+
end
|
111
|
+
|
112
|
+
def read_inheritable_attribute(key)
|
113
|
+
inheritable_attributes[key]
|
114
|
+
end
|
115
|
+
|
116
|
+
def reset_inheritable_attributes
|
117
|
+
@inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
# Prevent this constant from being created multiple times
|
122
|
+
EMPTY_INHERITABLE_ATTRIBUTES = {}.freeze unless const_defined?(:EMPTY_INHERITABLE_ATTRIBUTES)
|
123
|
+
|
124
|
+
def inherited_with_inheritable_attributes(child)
|
125
|
+
inherited_without_inheritable_attributes(child) if respond_to?(:inherited_without_inheritable_attributes)
|
126
|
+
|
127
|
+
if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES)
|
128
|
+
new_inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES
|
129
|
+
else
|
130
|
+
new_inheritable_attributes = inheritable_attributes.inject({}) do |memo, (key, value)|
|
131
|
+
memo.update(key => value.duplicable? ? value.dup : value)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
child.instance_variable_set('@inheritable_attributes', new_inheritable_attributes)
|
136
|
+
end
|
137
|
+
|
138
|
+
alias inherited_without_inheritable_attributes inherited
|
139
|
+
alias inherited inherited_with_inheritable_attributes
|
140
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Object
|
2
|
+
# Can you safely .dup this object?
|
3
|
+
# False for nil, false, true, symbols, and numbers; true otherwise.
|
4
|
+
def duplicable?
|
5
|
+
true
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class NilClass #:nodoc:
|
10
|
+
def duplicable?
|
11
|
+
false
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class FalseClass #:nodoc:
|
16
|
+
def duplicable?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class TrueClass #:nodoc:
|
22
|
+
def duplicable?
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Symbol #:nodoc:
|
28
|
+
def duplicable?
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Numeric #:nodoc:
|
34
|
+
def duplicable?
|
35
|
+
false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Class #:nodoc:
|
40
|
+
def duplicable?
|
41
|
+
false
|
42
|
+
end
|
43
|
+
end
|
data/test/default_test.rb
CHANGED
@@ -5,7 +5,7 @@ class DslDefaultAccessorTest < Test::Unit::TestCase
|
|
5
5
|
|
6
6
|
class CoolActiveRecord
|
7
7
|
dsl_accessor :primary_key, :default=>"id"
|
8
|
-
dsl_accessor :table_name, :default=>proc{|klass| klass.name.
|
8
|
+
dsl_accessor :table_name, :default=>proc{|klass| klass.name.split(/::/).last.downcase+"s"}
|
9
9
|
end
|
10
10
|
|
11
11
|
class Item < CoolActiveRecord
|
@@ -48,7 +48,7 @@ end
|
|
48
48
|
class DslOverwritenDefaultAccessorTest < Test::Unit::TestCase
|
49
49
|
class CoolActiveRecord
|
50
50
|
dsl_accessor :primary_key, :default=>"id"
|
51
|
-
dsl_accessor :table_name, :default=>proc{|klass| klass.name
|
51
|
+
dsl_accessor :table_name, :default=>proc{|klass| klass.name+"s"}
|
52
52
|
end
|
53
53
|
|
54
54
|
class Item < CoolActiveRecord
|
data/test/test_helper.rb
CHANGED
@@ -1,14 +1,5 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
|
3
3
|
def __DIR__; File.dirname(__FILE__); end
|
4
|
-
|
5
|
-
begin
|
6
|
-
require 'rubygems'
|
7
|
-
require 'active_support'
|
8
|
-
rescue LoadError
|
9
|
-
$:.unshift(__DIR__ + '/../../../rails/activesupport/lib')
|
10
|
-
require 'active_support'
|
11
|
-
end
|
12
|
-
|
13
4
|
require File.dirname(__FILE__) + '/../init'
|
14
5
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maiha-dsl_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maiha
|
@@ -9,18 +9,10 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-02-11 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
name: activesupport
|
17
|
-
version_requirement:
|
18
|
-
version_requirements: !ruby/object:Gem::Requirement
|
19
|
-
requirements:
|
20
|
-
- - ">="
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: 2.0.0
|
23
|
-
version:
|
14
|
+
dependencies: []
|
15
|
+
|
24
16
|
description: This plugin gives hybrid accessor class methods to classes by DSL like definition
|
25
17
|
email: maiha@wota.jp
|
26
18
|
executables: []
|
@@ -34,8 +26,10 @@ files:
|
|
34
26
|
- LICENSE
|
35
27
|
- README
|
36
28
|
- Rakefile
|
29
|
+
- core_ext/duplicable.rb
|
37
30
|
- core_ext/class
|
38
31
|
- core_ext/class/dsl_accessor.rb
|
32
|
+
- core_ext/class/inheritable_attributes.rb
|
39
33
|
- lib/dsl_accessor.rb
|
40
34
|
- spec/auto_declared_spec.rb
|
41
35
|
- spec/spec_helper.rb
|