classy 1.1.0 → 1.1.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/VERSION +1 -1
- data/lib/classy/templatable.rb +37 -0
- metadata +1 -1
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.1.
|
|
1
|
+
1.1.1
|
data/lib/classy/templatable.rb
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
+
# Templatable allows a class to set default variables for its instances.
|
|
2
|
+
#
|
|
3
|
+
# == Example
|
|
4
|
+
#
|
|
5
|
+
# class Widget
|
|
6
|
+
# extend Templatable
|
|
7
|
+
#
|
|
8
|
+
# templatable_attr :awesomeness, :temperature
|
|
9
|
+
# awesomeness :total
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# Widget.awesomeness # => :total
|
|
13
|
+
# Widget.temperature # => nil
|
|
14
|
+
#
|
|
15
|
+
# doodad = Widget.new
|
|
16
|
+
# doodad.awesomeness # => :total
|
|
17
|
+
# doodad.temperature # => nil
|
|
18
|
+
#
|
|
19
|
+
# # New defaults affect existing instances.
|
|
20
|
+
# Widget.temperature = :cool
|
|
21
|
+
# Widget.temperature # => :cool
|
|
22
|
+
# doodad.temperature # => :cool
|
|
23
|
+
#
|
|
24
|
+
# # Instances can override the defaults.
|
|
25
|
+
# doodad.awesomeness = nil
|
|
26
|
+
# doodad.temperature = :cool
|
|
27
|
+
#
|
|
28
|
+
# == Note
|
|
29
|
+
#
|
|
30
|
+
# The default values are stored as class variables of the same name as the
|
|
31
|
+
# associated instance variables. This may lead to some surprising results, eg,
|
|
32
|
+
# if you set a default value on a subclass.
|
|
33
|
+
#
|
|
1
34
|
module Templatable
|
|
2
35
|
|
|
3
36
|
# A hash to track templatable variables that have been locally set.
|
|
@@ -6,6 +39,10 @@ module Templatable
|
|
|
6
39
|
#
|
|
7
40
|
@@ever_been_set = Hash.new { |hash, key| hash[key] = {} }
|
|
8
41
|
|
|
42
|
+
# Defines one or more templatable attrs, which will add instance methods
|
|
43
|
+
# similar to Ruby's standard attr_accessor method, and will also add class
|
|
44
|
+
# methods to set or get default values, as described above.
|
|
45
|
+
#
|
|
9
46
|
def templatable_attr (*symbols)
|
|
10
47
|
symbols.each do |symbol|
|
|
11
48
|
# define the instance setter method
|