ouija 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2012 Rob Lewis ( [kohder](http://github.com/kohder) )
2
+ =================================================================================================
3
+
4
+ The "Ouija" RubyGem is released under the **MIT LICENSE**
5
+ ----------------------------------------------------------
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ require 'yaml'
3
+
4
+ module Ouija
5
+ LIBRARY_PATH = File.join(File.dirname(__FILE__), 'ouija')
6
+ MEDIUM_PATH = File.join(LIBRARY_PATH, 'medium')
7
+
8
+ class Error < StandardError; end
9
+
10
+ module Medium
11
+ class Error < Ouija::Error; end
12
+
13
+ autoload :Yaml, File.join(MEDIUM_PATH, 'yaml')
14
+ end
15
+
16
+ %w{
17
+ planchette
18
+ ouija
19
+ }.each {|lib| require File.join(LIBRARY_PATH, lib) }
20
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ module Ouija
4
+ module Medium
5
+ class Yaml
6
+ DEFAULT_CONFIG_DIRNAME = 'ouija'
7
+
8
+ def channel(options={})
9
+ config_dirname = options[:config_dirname] || DEFAULT_CONFIG_DIRNAME
10
+ config_path = options[:config_path]
11
+ config_files = find_config_files(config_dirname, config_path)
12
+ config_map = {}
13
+ config_files.each do |config_file|
14
+ section_name = File.basename(config_file, '.*')
15
+ begin
16
+ config_data = YAML.load_file(config_file)
17
+ rescue Exception => ex
18
+ raise Ouija::Medium::Error.new "Failed to load YAML file \"#{config_file}\".\nError message: #{ex.message}"
19
+ end
20
+ config_map[section_name] = config_data
21
+ end
22
+ config_map
23
+ end
24
+
25
+ protected
26
+
27
+ def find_config_files(config_dirname, config_path=nil)
28
+ if !config_path.nil? && !config_path.empty?
29
+ config_dir = search_for_config_dir(config_dirname, config_path)
30
+ raise Error.new("Could not find configuration directory \"#{config_dirname}\" in path \"#{config_path}\".") if config_dir.nil?
31
+ else
32
+ config_dir = search_for_config_dir(config_dirname, '.')
33
+ raise Error.new("Could not find configuration directory \"#{config_dirname}\".") if config_dir.nil?
34
+ end
35
+ Dir.glob(File.join(config_dir, '*.yml'), File::FNM_CASEFOLD)
36
+ end
37
+
38
+ def search_for_config_dir(config_dirname, path)
39
+ config_dir = nil
40
+ if File.directory?(path)
41
+ if File.basename(path) == config_dirname
42
+ config_dir = path
43
+ else
44
+ possible_dir = File.join(path, config_dirname)
45
+ if File.exists?(possible_dir)
46
+ config_dir = possible_dir
47
+ else
48
+ possible_dir = File.join(path, 'config', config_dirname)
49
+ if File.exists?(possible_dir)
50
+ config_dir = possible_dir
51
+ end
52
+ end
53
+ end
54
+ end
55
+ config_dir.nil? ? nil : File.expand_path(config_dir)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ module Ouija
4
+ class << self
5
+ def setup(options={})
6
+ @options = options
7
+ # ToDo: For other mediums, include options logic here (defaulting to YAML)
8
+ @channel = stringify_keys_recursive(Medium::Yaml.new.channel(options))
9
+ true
10
+ end
11
+
12
+ def topics
13
+ @channel.keys
14
+ end
15
+
16
+ def session(topic, context={})
17
+ topic = topic.to_s
18
+ unless topics.include?(topic)
19
+ raise Error.new "Unknown topic \"#{topic}\""
20
+ end
21
+ Planchette.new(scope_topic(topic, context))
22
+ end
23
+ alias_method :séance, :session
24
+
25
+ protected
26
+
27
+ def stringify_keys_recursive(hash)
28
+ stringified_hash = {}
29
+ hash.each_pair do |k,v|
30
+ key = k.kind_of?(Symbol) ? k.to_s : k
31
+ value = v.is_a?(Hash) ? stringify_keys_recursive(v) : v
32
+ stringified_hash[key] = value
33
+ end
34
+ stringified_hash
35
+ end
36
+
37
+ def scope_topic(topic, context={})
38
+ hash = @channel[topic] || {}
39
+ environment = context[:environment] || context[:env] || get_environment
40
+ hostname = context[:hostname] || context[:host] || get_hostname
41
+
42
+ #puts "Loading config for environment \"#{environment}\" and hostname \"#{hostname}\"."
43
+
44
+ base_hash = hash.has_key?('default') ? hash['default'].dup : {}
45
+ if !environment.nil? && !environment.empty? && !hash['environments'].nil? && !hash['environments'][environment.to_s].nil?
46
+ env_hash = hash['environments'][environment.to_s]
47
+ recursive_merge!(base_hash, env_hash)
48
+ end
49
+ if !hostname.nil? && !hostname.empty? && !hash['hosts'].nil? && !hash['hosts'][hostname.to_s].nil?
50
+ host_hash = hash['hosts'][hostname.to_s]
51
+ recursive_merge!(base_hash, host_hash)
52
+ end
53
+ base_hash
54
+ end
55
+
56
+ def recursive_merge!(hash, other_hash)
57
+ other_hash.each_pair do |k, v|
58
+ cur_val = hash[k]
59
+ if cur_val.is_a?(Hash) && v.is_a?(Hash)
60
+ hash[k] = recursive_merge!(cur_val, v)
61
+ elsif !v.nil?
62
+ hash[k] = v
63
+ end
64
+ end
65
+ hash
66
+ end
67
+
68
+ def get_environment
69
+ @options[:environment] || @options[:env] || ENV['RAILS_ENV'] || ENV['OUIJA_ENV'] || 'development'
70
+ end
71
+
72
+ def get_hostname
73
+ @options[:hostname] || @options[:host] || begin
74
+ require 'socket'
75
+ Socket.gethostname rescue nil
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module Ouija
4
+ class Planchette
5
+
6
+ def initialize(hash)
7
+ @hash = hash
8
+ end
9
+
10
+ def [](key)
11
+ value = @hash[key]
12
+ value.respond?(:dup) ? value.dup : value
13
+ end
14
+
15
+ def to_hash
16
+ @hash.dup
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ module Ouija
4
+ class Version
5
+ MAJOR, MINOR, PATCH = 0, 1, 0
6
+
7
+ def self.major
8
+ MAJOR
9
+ end
10
+
11
+ def self.minor
12
+ MINOR
13
+ end
14
+
15
+ def self.patch
16
+ PATCH
17
+ end
18
+
19
+ def self.current
20
+ "#{major}.#{minor}.#{patch}"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('lib/ouija/version')
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'ouija'
6
+ gem.version = Ouija::Version.current
7
+ gem.platform = Gem::Platform::RUBY
8
+ gem.author = 'Rob Lewis (kohder)'
9
+ gem.email = 'rob@kohder.com'
10
+ gem.homepage = 'http://github.com/kohder/ouija'
11
+ gem.description = 'The Ouija configuration gem'
12
+ gem.summary = 'Hold a séance and listen to what your configuration is trying to tell you. Fun for the whole family.'
13
+ gem.license = 'MIT'
14
+
15
+ gem.required_ruby_version = '>= 1.9.2'
16
+
17
+ #gem.files = %x[git ls-files].split("\n")
18
+ gem.files = %w{ouija.gemspec LICENSE.md lib/ouija.rb lib/ouija/ouija.rb lib/ouija/planchette.rb lib/ouija/version.rb lib/ouija/medium/yaml.rb}
19
+ #gem.test_files = %x[git ls-files -- {spec}/*].split("\n")
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.require_paths = %w(lib)
22
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ouija
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Lewis (kohder)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-02 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: The Ouija configuration gem
15
+ email: rob@kohder.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ouija.gemspec
21
+ - LICENSE.md
22
+ - lib/ouija.rb
23
+ - lib/ouija/ouija.rb
24
+ - lib/ouija/planchette.rb
25
+ - lib/ouija/version.rb
26
+ - lib/ouija/medium/yaml.rb
27
+ homepage: http://github.com/kohder/ouija
28
+ licenses:
29
+ - MIT
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: 1.9.2
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.10
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Hold a séance and listen to what your configuration is trying to tell you. Fun
52
+ for the whole family.
53
+ test_files: []