Module: Ecology

Defined in:
lib/ecology.rb,
lib/ecology/version.rb

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.11"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.applicationObject (readonly)

Returns the value of attribute application.



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

def application
  @application
end

.dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

.environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

.mutexObject

Returns the value of attribute mutex.



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

def mutex
  @mutex
end

Class Method Details

.clear_triggersObject



30
31
32
# File 'lib/ecology.rb', line 30

def clear_triggers
  @triggers = {}
end

.default_ecology_name(executable = $0) ⇒ Object



244
245
246
247
248
# File 'lib/ecology.rb', line 244

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



61
62
63
# File 'lib/ecology.rb', line 61

def on_initialize(token = nil, &block)
  on_event(:initialize, token, &block)
end

.on_reset(token = nil, &block) ⇒ Object



65
66
67
# File 'lib/ecology.rb', line 65

def on_reset(token = nil, &block)
  on_event(:reset, token, &block)
end

.path(path_name) ⇒ Object



225
226
227
228
229
230
# File 'lib/ecology.rb', line 225

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ecology.rb', line 182

def property(param, options = {})
  components = param.split(":").compact.select {|s| s != ""}

  value = components.inject(@data) do |data, component|
    if data
      data[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.data(:as) for property #{property}!"
    end
  end

  return value if options[:as] == Hash
  raise "Couldn't convert JSON fields to #{options[:as].inspect} for property #{property}!"
end

.read(ecology_pathname = nil) ⇒ Object



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
# File 'lib/ecology.rb', line 34

def read(ecology_pathname = nil)
  return if @ecology_initialized

  should_publish_event = false

  mutex.synchronize do
    return if @ecology_initialized

    file_path = ENV['ECOLOGY_SPEC'] || ecology_pathname || default_ecology_name
    if File.exist?(file_path)
      @data = {}
      contents = merge_with_overrides(file_path)
    end

    @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.
  publish_event(:initialize) if should_publish_event
end

.remove_trigger(token) ⇒ Object



69
70
71
72
73
74
# File 'lib/ecology.rb', line 69

def remove_trigger(token)
  @triggers ||= {}
  @triggers.each do |event, trigger_list|
    @triggers[event].delete(token)
  end
end

.resetObject

Normally this is only for testing.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ecology.rb', line 18

def reset
  # Preserve triggers across resets by default
  @triggers ||= {}

  @application = nil
  @environment = nil
  @data = nil
  @ecology_initialized = 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?)



253
254
255
256
257
258
259
260
261
262
# File 'lib/ecology.rb', line 253

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