Sutto-perennial 0.2.4.0 → 0.2.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,20 @@
1
+ require 'yaml'
1
2
  module Perennial
2
3
  # A ninja hash. Like OpenStruct, but better
3
4
  class Nash
4
5
 
6
+ def self.load_file(path)
7
+ n = self.new
8
+ if File.file?(path) && File.readable?(path)
9
+ contents = YAML.load_file(path)
10
+ end
11
+ if contents.is_a?(Hash)
12
+ contents.to_hash
13
+ else
14
+ new(:data => contents).normalized
15
+ end
16
+ end
17
+
5
18
  attr_reader :table
6
19
 
7
20
  def initialize(initial = {})
@@ -104,7 +117,7 @@ module Perennial
104
117
 
105
118
  def inspect
106
119
  str = ""
107
- if first = Thread.current[:inspect_stack].nil?
120
+ if Thread.current[:inspect_stack].nil?
108
121
  Thread.current[:inspect_stack] = [self]
109
122
  str = _inspect
110
123
  Thread.current[:inspect_stack] = nil
@@ -129,9 +142,52 @@ module Perennial
129
142
  str << ">"
130
143
  return str
131
144
  end
145
+
146
+ def hash
147
+ @table.hash
148
+ end
149
+
150
+ def normalized(n = nil)
151
+ item = nil
152
+ if Thread.current[:normalized].nil?
153
+ n = self.class.new
154
+ Thread.current[:normalized] = {self => n}
155
+ item = normalize_nash(n)
156
+ Thread.current[:normalized] = nil
157
+ else
158
+ if Thread.current[:normalized].has_key?(self)
159
+ return Thread.current[:normalized][self]
160
+ else
161
+ n = self.class.new
162
+ Thread.current[:normalized][self] = n
163
+ item = normalize_nash(n)
164
+ end
165
+ end
166
+ item
167
+ end
132
168
 
133
169
  protected
134
170
 
171
+ def normalize_nash(n = self.class.new)
172
+ each_pair do |k, v|
173
+ n[k] = normalize_item(v)
174
+ end
175
+ return n
176
+ end
177
+
178
+ def normalize_item(i)
179
+ case i
180
+ when Hash
181
+ self.class.new(i).normalized
182
+ when Array
183
+ i.map { |v| normalize_item(v) }
184
+ when self.class
185
+ i.normalized
186
+ else
187
+ i
188
+ end
189
+ end
190
+
135
191
  def method_missing(name, *args, &blk)
136
192
  name = name.to_s
137
193
  case name.to_s[-1]
@@ -71,6 +71,8 @@ module Perennial
71
71
  @@configuration.merge!(lookup_settings_from(loaded_yaml))
72
72
  end
73
73
  @@configuration.merge! options
74
+ # Finally, normalize settings
75
+ @@configuration = @@configuration.normalized
74
76
  @@setup = true
75
77
  end
76
78
 
data/lib/perennial.rb CHANGED
@@ -9,7 +9,7 @@ require 'perennial/exceptions'
9
9
 
10
10
  module Perennial
11
11
 
12
- VERSION = "0.2.4.0"
12
+ VERSION = "0.2.4.1"
13
13
 
14
14
  has_library :dispatchable, :hookable, :loader, :logger, :nash,
15
15
  :loggable, :manifest, :settings, :argument_parser,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Sutto-perennial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4.0
4
+ version: 0.2.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darcy Laycock