caesars 0.5.1 → 0.5.2

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.
Files changed (5) hide show
  1. data/CHANGES.txt +12 -0
  2. data/README.rdoc +19 -4
  3. data/caesars.gemspec +2 -2
  4. data/lib/caesars.rb +98 -4
  5. metadata +3 -3
data/CHANGES.txt CHANGED
@@ -1,6 +1,17 @@
1
1
  CAESAR -- CHANGES
2
2
 
3
3
 
4
+ #### 0.5.2 (2009-03-31) ###############################
5
+
6
+ * NEW: Caesars.debug?, Caesars.enable_debug, Caesars.disable_debug
7
+ * CHANGED: find_deferred now supports nested Arrays. See rdocs.
8
+ * BUG: Found bug related to string/symbol ambiguity when using find.
9
+ It's not fixed yet so for now be mindful of which attributes are
10
+ strings and which are symbols. String attributes are stored as
11
+ strings but find and find_deferred look for symbols. The hash syntax
12
+ and method accessors are not affected.
13
+
14
+
4
15
  #### 0.5.1 (2009-03-11) ###############################
5
16
 
6
17
  * FIX: Method-syntax was broken for attributes of top level method
@@ -8,6 +19,7 @@ CAESAR -- CHANGES
8
19
  * UPDATED: docs and bin/example to reflect Caesars::Hash changes.
9
20
  * FIX: instance_variables in Ruby 1.9.1 returns Symbols
10
21
 
22
+
11
23
  #### 0.5.0 (2009-03-11) ###############################
12
24
 
13
25
  * FIX: find_deferred now gracefully handles nil errors
data/README.rdoc CHANGED
@@ -11,9 +11,9 @@ One of:
11
11
 
12
12
  Or for GitHub fans:
13
13
 
14
+ * gem install delano-caesars --source http://gems.github.com
14
15
  * git clone git://github.com/delano/caesar.git
15
- * gem sources -a http://gems.github.com (you only have to do this once, ever...), then:
16
- * gem install delano-caesar
16
+
17
17
 
18
18
  == EXAMPLE 1 -- Flavour
19
19
 
@@ -133,10 +133,23 @@ Or for GitHub fans:
133
133
  # [... make changes to party.conf ...]
134
134
 
135
135
  @config.refresh
136
-
136
+
137
+ *party.conf*
138
+
139
+ food do
140
+ oysters "10kg"
141
+ order do
142
+ self.oysters
143
+ end
144
+ end
145
+
146
+ drink do
147
+ wine "12L"
148
+ end
149
+
137
150
  == More Info
138
151
 
139
- * GitHub[http://github.com/delano/caesar]
152
+ * GitHub[http://github.com/delano/caesars]
140
153
  * Inspiration[http://www.youtube.com/watch?v=ycElb0tB9_U]
141
154
  * Recipe[http://twitter.com/solutious/status/1264327499]
142
155
 
@@ -147,6 +160,8 @@ Or for GitHub fans:
147
160
  == Thanks
148
161
 
149
162
  * Clams, Tomatoes, Vodka, and the rest of the crew.
163
+ * Caleb Buxton (http://cpb.ca) for early feedback.
164
+
150
165
 
151
166
  == License
152
167
 
data/caesars.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  @spec = Gem::Specification.new do |s|
2
2
  s.name = %q{caesars}
3
- s.version = "0.5.1"
4
- s.date = %q{2009-03-11}
3
+ s.version = "0.5.2"
4
+ s.date = %q{2009-03-31}
5
5
  s.specification_version = 1 if s.respond_to? :specification_version=
6
6
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
7
7
 
data/lib/caesars.rb CHANGED
@@ -7,7 +7,13 @@
7
7
  # See bin/example
8
8
  #
9
9
  class Caesars
10
- VERSION = "0.5.1"
10
+ VERSION = "0.5.2"
11
+ @@debug = false
12
+
13
+ def Caesars.enable_debug; @@debug = true; end
14
+ def Caesars.disable_debug; @@debug = false; end
15
+ def Caesars.debug?; @@debug; end
16
+
11
17
  # A subclass of ::Hash that provides method names for hash parameters.
12
18
  # It's like a lightweight OpenStruct.
13
19
  # ch = Caesars::Hash[:tabasco => :lots!]
@@ -55,7 +61,9 @@ class Caesars
55
61
  def to_hash
56
62
  @caesars_properties.to_hash
57
63
  end
58
-
64
+
65
+ # DEPRECATED -- use find_deferred
66
+ #
59
67
  # Look for an attribute, bubbling up to the parent if it's not found
60
68
  # +criteria+ is an array of attribute names, orders according to their
61
69
  # relationship. The last element is considered to the desired attribute.
@@ -69,12 +77,13 @@ class Caesars
69
77
  #
70
78
  # Returns the attribute if found or nil.
71
79
  #
72
- def find_deferred(*criteria)
80
+ def find_deferred_old(*criteria)
73
81
  # This is a nasty implementation. Sorry me! I'll enjoy a few
74
82
  # caesars and be right with you.
75
83
  att = criteria.pop
76
84
  val = nil
77
85
  while !criteria.empty?
86
+ p [criteria, att].flatten if Caesars.debug?
78
87
  val = find(criteria, att)
79
88
  break if val
80
89
  criteria.pop
@@ -84,6 +93,88 @@ class Caesars
84
93
  val
85
94
  end
86
95
 
96
+ # Look for an attribute, bubbling up through the parents until it's found.
97
+ # +criteria+ is an array of hierarchical attributes, ordered according to
98
+ # their relationship. The last element is the desired attribute to find.
99
+ # Looking for 'ami':
100
+ #
101
+ # find_deferred(:environment, :role, :ami)
102
+ #
103
+ # First checks at @caesars_properties[:environment][:role][:ami]
104
+ # Then, @caesars_properties[:environment][:ami]
105
+ # Finally, @caesars_properties[:ami]
106
+ #
107
+ # If the last element is an Array, it's assumed that only that combination
108
+ # should be returned.
109
+ #
110
+ # find_deferred(:environment, :role:, [:disks, '/file/path'])
111
+ #
112
+ # [:environment][:role][:disks]['/file/path']
113
+ # [:environment][:disks]['/file/path']
114
+ # [:disks]['/file/path']
115
+ #
116
+ # Other nested Arrays are treated special too. We look at the criteria from
117
+ # right to left and remove the first nested element we find.
118
+ #
119
+ # find_deferred([:region, :zone], :environment, :role, :ami)
120
+ #
121
+ # [:region][:zone][:environment][:role][:ami]
122
+ # [:region][:environment][:role][:ami]
123
+ # [:environment][:role][:ami]
124
+ # [:environment][:ami]
125
+ # [:ami]
126
+ #
127
+ # Returns the attribute if found or nil.
128
+ #
129
+ def find_deferred(*criteria)
130
+ # The last element is assumed to be the attribute we're looking for.
131
+ # The purpose of this function is to bubble up the hierarchy of a
132
+ # hash to find it.
133
+ att = criteria.pop
134
+
135
+ # Account for everything being sent as an Array
136
+ # i.e. find([1, 2, :attribute])
137
+ # We don't use flatten b/c we don't want to disturb nested Arrays
138
+ if criteria.empty?
139
+ criteria = att
140
+ att = criteria.pop
141
+ end
142
+
143
+ found = nil
144
+ sacrifice = nil
145
+
146
+ while !criteria.empty?
147
+ p [criteria, att].flatten if Caesars.debug?
148
+ found = find(criteria, att)
149
+ break if found
150
+
151
+ # Nested Arrays are treated special. We look at the criteria from
152
+ # right to left and remove the first nested element we find.
153
+ #
154
+ # i.e. [['a', 'b'], 1, 2, [:first, :second], :attribute]
155
+ #
156
+ # In this example, :second will be removed first.
157
+ criteria.reverse.each_with_index do |el,index|
158
+ next unless el.is_a?(Array) # Ignore regular criteria
159
+ next if el.empty? # Ignore empty nested hashes
160
+ sacrifice = el.pop
161
+ break
162
+ end
163
+
164
+ # Remove empty nested Arrays
165
+ criteria.delete_if { |el| el.is_a?(Array) && el.empty? }
166
+
167
+ # We need to make a sacrifice
168
+ sacrifice = criteria.pop if sacrifice.nil?
169
+
170
+ break if (@limit ||= 0) > 5 # A failsafe
171
+ @limit += 1
172
+ sacrifice = nil
173
+ end
174
+
175
+ found || find(att) # One last try in the root namespace
176
+ end
177
+
87
178
  # Looks for the specific attribute specified.
88
179
  # +criteria+ is an array of attribute names, orders according to their
89
180
  # relationship. The last element is considered to the desired attribute.
@@ -92,8 +183,11 @@ class Caesars
92
183
  # Unlike find_deferred, it will return only the value specified, otherwise nil.
93
184
  def find(*criteria)
94
185
  criteria.flatten! if criteria.first.is_a?(Array)
186
+ p criteria if Caesars.debug?
187
+ # BUG: Attributes can be stored as strings and here we only look for symbols
95
188
  str = criteria.collect { |v| "[:'#{v}']" if v }.join
96
- val = eval "@caesars_properties#{str} if defined?(@caesars_properties#{str})"
189
+ eval_str = "@caesars_properties#{str} if defined?(@caesars_properties#{str})"
190
+ val = eval eval_str
97
191
  val
98
192
  end
99
193
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caesars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delano Mandelbaum
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-11 00:00:00 -04:00
12
+ date: 2009-03-31 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  requirements: []
61
61
 
62
62
  rubyforge_project: caesars
63
- rubygems_version: 1.2.0
63
+ rubygems_version: 1.3.1
64
64
  signing_key:
65
65
  specification_version: 1
66
66
  summary: "Caesars: A simple class for rapid DSL prototyping in Ruby."