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

Class Method Summary collapse

Class Attribute Details

.applicationObject (readonly)

Returns the value of attribute application.



7
8
9
# File 'lib/ecology.rb', line 7

def application
  @application
end

.dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/ecology.rb', line 8

def data
  @data
end

.environmentObject (readonly)

Returns the value of attribute environment.



9
10
11
# File 'lib/ecology.rb', line 9

def environment
  @environment
end

.mutexObject

Returns the value of attribute mutex.



10
11
12
# File 'lib/ecology.rb', line 10

def mutex
  @mutex
end

Class Method Details

.clear_triggersObject



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, options = {})
  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 options[:as]

  unless value.is_a?(Hash)
    if [String, :string].include?(options[:as])
      return value.to_s
    elsif [Symbol, :symbol].include?(options[:as])
      return value.to_s.to_sym
    elsif [Fixnum, :int, :integer, :fixnum].include?(options[:as])
      return value.to_i
    elsif [Hash, :hash].include?(options[:as])
      raise "Cannot convert scalar value to Hash!"
    elsif [:path].include?(options[:as])
      return string_to_path(value.to_s)
    elsif [:json].include?(options[:as])
      raise "JSON return type not yet supported!"
    else
      raise "Unknown type #{options[:as].inspect} passed to Ecology.property(:as) for property #{param}!"
    end
  end

  return value if options[:as] == Hash
  raise "Couldn't convert JSON fields to #{options[: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.expand_path(ecology_path) != File.expand_path(@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

.resetObject

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