Module: Ecology
- Defined in:
- lib/ecology.rb,
lib/ecology/version.rb,
lib/ecology/test_methods.rb
Defined Under Namespace
Modules: Test
Constant Summary collapse
- ECOLOGY_EXTENSION =
".ecology"
- PATH_SUBSTITUTIONS =
{ "$env" => proc { Ecology.environment }, "$cwd" => proc { Dir.getwd }, "$app" => proc { File.dirname($0) }, "$pid" => proc { Process.pid.to_s }, }
- VERSION =
"0.0.18"
Class Attribute Summary collapse
-
.application ⇒ Object
readonly
Returns the value of attribute application.
-
.data ⇒ Object
readonly
Returns the value of attribute data.
-
.environment ⇒ Object
readonly
Returns the value of attribute environment.
-
.mutex ⇒ Object
Returns the value of attribute mutex.
Class Method Summary collapse
- .clear_triggers ⇒ Object
- .default_ecology_name(executable = $0) ⇒ Object
-
.on_initialize(token = nil, &block) ⇒ Object
These functions are called to set and remove triggers that are called when the specified event has occurred.
- .on_reset(token = nil, &block) ⇒ Object
- .path(path_name) ⇒ Object
-
.property(param, options = {}) ⇒ Object
Returns the value of a specified property, with nestings denoted with colons.
-
.read(ecology_pathname = nil) ⇒ Object
This function is called in applications or libraries to initialize the ecology.
- .remove_trigger(token) ⇒ Object
-
.reset ⇒ Object
Normally this is only for testing.
-
.thread_id(thread) ⇒ Object
This is a convenience function because the Ruby thread API has no accessor for the thread ID, but includes it in “to_s” (buh?).
Class Attribute Details
.application ⇒ Object (readonly)
Returns the value of attribute application.
7 8 9 |
# File 'lib/ecology.rb', line 7 def application @application end |
.data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/ecology.rb', line 8 def data @data end |
.environment ⇒ Object (readonly)
Returns the value of attribute environment.
9 10 11 |
# File 'lib/ecology.rb', line 9 def environment @environment end |
.mutex ⇒ Object
Returns the value of attribute mutex.
10 11 12 |
# File 'lib/ecology.rb', line 10 def mutex @mutex end |
Class Method Details
.clear_triggers ⇒ Object
87 88 89 |
# File 'lib/ecology.rb', line 87 def clear_triggers @triggers = {} end |
.default_ecology_name(executable = $0) ⇒ Object
280 281 282 283 284 |
# File 'lib/ecology.rb', line 280 def default_ecology_name(executable = $0) suffix = File.extname(executable) executable[0..(executable.length - 1 - suffix.size)] + ECOLOGY_EXTENSION end |
.on_initialize(token = nil, &block) ⇒ Object
These functions are called to set and remove triggers that are called when the specified event has occurred.
72 73 74 |
# File 'lib/ecology.rb', line 72 def on_initialize(token = nil, &block) on_event(:initialize, token, &block) end |
.on_reset(token = nil, &block) ⇒ Object
76 77 78 |
# File 'lib/ecology.rb', line 76 def on_reset(token = nil, &block) on_event(:reset, token, &block) end |
.path(path_name) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/ecology.rb', line 261 def path(path_name) path_data = @data ? @data["paths"] : nil return nil unless path_data && path_data[path_name] string_to_path path_data[path_name] end |
.property(param, options = {}) ⇒ Object
Returns the value of a specified property, with nestings denoted with colons. This can be requested with :as => <type> as an option.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/ecology.rb', line 218 def property(param, = {}) components = param.split(":").compact.select {|s| s != ""} value = components.inject(@data) do |data_hash, component| if data_hash data_hash[component] else nil end end return nil unless value return value unless [:as] unless value.is_a?(Hash) if [String, :string].include?([:as]) return value.to_s elsif [Symbol, :symbol].include?([:as]) return value.to_s.to_sym elsif [Fixnum, :int, :integer, :fixnum].include?([:as]) return value.to_i elsif [Hash, :hash].include?([:as]) raise "Cannot convert scalar value to Hash!" elsif [:path].include?([:as]) return string_to_path(value.to_s) elsif [:json].include?([:as]) raise "JSON return type not yet supported!" else raise "Unknown type #{[:as].inspect} passed to Ecology.property(:as) for property #{param}!" end end return value if [:as] == Hash raise "Couldn't convert JSON fields to #{[:as].inspect} for property #{param}!" end |
.read(ecology_pathname = nil) ⇒ Object
This function is called in applications or libraries to initialize the ecology
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ecology.rb', line 33 def read(ecology_pathname = nil) filelist = [ENV["ECOLOGY_SPEC"], ecology_pathname, default_ecology_name] ecology_path = filelist.detect { |file_path| file_path && (File.exist?(file_path) || File.exist?(file_path + ".erb")) } if @ecology_initialized if ecology_path != nil && File.(ecology_path) != File.(@ecology_path) raise "You've tried to load both #{ecology_path.inspect || "nothing"} and " + "#{@ecology_path.inspect || "nothing"} as ecology files since last reset!" end return end should_publish_event = false mutex.synchronize do return if @ecology_initialized @ecology_path = ecology_path @data ||= {} merge_with_overrides!(@ecology_path) if @ecology_path @application ||= File.basename($0) @environment ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development" should_publish_event = true @ecology_initialized = true end # Do this outside the mutex to reduce the likelihood # of deadlocks. This will run all of the on_initialize triggers. publish_event(:initialize) if should_publish_event nil end |
.remove_trigger(token) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/ecology.rb', line 80 def remove_trigger(token) @triggers ||= {} @triggers.each do |event, trigger_list| @triggers[event].delete(token) end end |
.reset ⇒ Object
Normally this is only for testing.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ecology.rb', line 19 def reset # Preserve triggers across resets by default @triggers ||= {} @application = nil @environment = nil @data = nil @ecology_initialized = nil @ecology_path = nil publish_event :reset end |
.thread_id(thread) ⇒ Object
This is a convenience function because the Ruby thread API has no accessor for the thread ID, but includes it in “to_s” (buh?)
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/ecology.rb', line 289 def thread_id(thread) return "main" if thread == Thread.main str = thread.to_s match = nil match = str.match /(0x\d+)/ return nil unless match match[1] end |