attr_plus 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # ClassAttr
1
+ # AttrPlus
2
+
3
+ ## ClassAttr
2
4
 
3
5
  Adds `#class_attr_accessor` (and reader/writer) and `#inheritable_class_attr_accessor` (and
4
6
  reader/writer of course) to Class.
@@ -48,18 +50,36 @@ make it more clear:
48
50
  Agent.legs #=> 2
49
51
 
50
52
 
53
+ ## ModuleAttr
54
+
55
+ Almost exactly the same as `class_*` but for modules. __Note__ there is no module inheritence
56
+ so `inheritable_module_*` will not work, I am thinking of adding `includable_module_*` later
57
+ but they aren't in yet so can't be used!
58
+
59
+ module MyHouse
60
+ module_attr_accessor :width, :height, :default => 200
61
+ module_attr_accessor :rooms => []
62
+ module_attr_accessor :number, :street, :city, :country
63
+ end
64
+
65
+ House.width = 500
66
+ House.rooms = [:living_room, :kitchen]
67
+ House.rooms.first #=> :living_room
68
+
69
+
51
70
  ## Install
52
71
 
53
- (sudo) gem install class_attr
72
+ (sudo) gem install attr_plus
54
73
 
55
74
 
56
75
  ## Use
57
76
 
58
- require 'class_attr'
59
-
60
- Or, as in some cases this may be overkill, copy the parts you need over (you will need to modify it,
61
- but that should be easy to work out.)
77
+ require 'attr_plus'
62
78
 
79
+ # or for specific methods
80
+ require 'attr_plus/class' # for only class_*
81
+ require 'attr_plus/module' # for only module_*
82
+
63
83
 
64
84
  ### Important!
65
85
 
@@ -1,5 +1,9 @@
1
1
  require 'attr_plus/ext'
2
2
 
3
+ # Should add cattr_accessor, which provides accessors for @@ class
4
+ # variables!
5
+
6
+
3
7
  class Class
4
8
 
5
9
  # Defines a method that allows you to read an instance variable set at the
@@ -121,6 +125,7 @@ class Class
121
125
  EOS
122
126
  self.instance_variable_set("@#{name}", (default.dup rescue default))
123
127
  end
128
+ inheritable_attrs.concat(names).uniq!
124
129
  end
125
130
 
126
131
  # The same as #class_attr_writer.
@@ -181,10 +186,17 @@ class Class
181
186
  registered_defaults[key] = value
182
187
  end
183
188
 
189
+ # Hash of default values, used for inheritable and non-inheritable attrs.
184
190
  def registered_defaults
185
191
  @registered_defaults ||= {}
186
192
  end
187
193
 
194
+ # Array of symbols to call when setting up a subclass, which will provide the
195
+ # new default values to use. This allows the current values to be inherited.
196
+ def inheritable_attrs
197
+ @inheritable_attrs ||= []
198
+ end
199
+
188
200
  def inherited_with_attrs(klass)
189
201
  inherited_without_attrs(klass) if respond_to?(:inherited_without_attrs)
190
202
  if registered_defaults
@@ -195,6 +207,13 @@ class Class
195
207
  new_attrs = {}
196
208
  end
197
209
 
210
+ new_attrs.each do |key, value|
211
+ result = self.send(key)
212
+ if result
213
+ new_attrs[key] = result
214
+ end # else leave as default
215
+ end
216
+
198
217
  new_attrs.each do |k, v|
199
218
  klass.instance_variable_set("@#{k}", v)
200
219
  end
@@ -0,0 +1,19 @@
1
+ # At the moment attr_* is very fast, but very inflexible. It is not possible
2
+ # to create :something? methods, which would be much easier than
3
+ #
4
+ # attr_accessor :alive
5
+ # alias_method :alive?, :alive
6
+ #
7
+ # Also it is not possible to set defaults, these things could be remedied,
8
+ # but should be done in the most efficient ways possible, ie. don't rely on
9
+ # eval.
10
+ #
11
+ # attr_accessor :alive?
12
+ #
13
+ # Could become...
14
+ #
15
+ # attr_accessor :alive
16
+ # alias_method :alive?, :alive
17
+ #
18
+ # Or something similar.
19
+ #
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joshua Hawxwell
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-12 00:00:00 +00:00
17
+ date: 2011-02-22 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies: []
20
20