tcravit_ruby_lib 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ module TcravitRubyLib #:nodoc:
2
+
3
+ # Simple and Flexible Configuration Data Storage.
4
+ #
5
+ # AppConfig provides a simple facility for storing configuration data in
6
+ # an application. It does this using metaprogramming techniques to define
7
+ # accessor methods dynamically when the configuration file is loaded.
8
+ #
9
+ # This is far from the best way to do this, and was written as an exercise
10
+ # in metaprogramming as much as anything else.
11
+ #
12
+ # === Example
13
+ #
14
+ # TcravitRubyLib::AppConfig.configure do
15
+ # some_value 1023
16
+ # another_value "Foo"
17
+ # end
18
+ #
19
+ # TcravitRubyLib::AppConfig.some_value # => 1023
20
+ # TcravitRubyLib::AppConfig.some_value = 2048
21
+ # TcravitRubyLib::AppConfig.some_value # => 2048
22
+ #
23
+ # Author:: Tammy Cravit (mailto:tammy@tammycravit.com)
24
+ # Copyright:: Copyright (c) 2011, Tammy Cravit. All rights reserved.
25
+ # License:: This program is free software: you can redistribute it and/or modify
26
+ # it under the terms of the GNU General Public License as published by
27
+ # the Free Software Foundation, either version 3 of the License, or
28
+ # (at your option) any later version.
29
+
30
+ module AppConfig
31
+ extend self
32
+
33
+ def configure(&block)
34
+ @definitions ||= Hash.new
35
+
36
+ # The method_missing auto-trigger should only run when we're inside
37
+ # a configure block
38
+ @in_config = true
39
+ instance_eval &block
40
+ @in_config = false
41
+ end
42
+
43
+ def remove!(key)
44
+ the_sym = key.to_sym
45
+ @definitions.delete(the_sym)
46
+ send :undef_method, the_sym
47
+ send :undef_method, "#{the_sym.to_s}=".to_sym
48
+ end
49
+
50
+ def method_missing(methname, *args)
51
+ if (methname.to_s =~ /^([A-Za-z0-9][A-Za-z0-9\-\_]+)$/)
52
+ if @in_config
53
+ getter_method_name = methname.to_s.to_sym
54
+ setter_method_name = "#{methname}=".to_s.to_sym
55
+ send :define_method, getter_method_name do
56
+ @definitions[getter_method_name]
57
+ end
58
+ send :define_method, setter_method_name do |new_value|
59
+ @definitions[getter_method_name] = new_value
60
+ end
61
+ send setter_method_name, (args.count > 0 ? args.first : nil)
62
+ else
63
+ super
64
+ end
65
+ else
66
+ nil
67
+ end
68
+ end
69
+ end
70
+ end
@@ -3,7 +3,7 @@ require 'pathname'
3
3
  module TcravitRubyLib #:nodoc:
4
4
 
5
5
  # Utilities for locating a configuration directory within a tree.
6
- #
6
+ #
7
7
  # Unix utilities such as +git+ will look for the .git directory in a
8
8
  # project by starting with the current directory and traversing upward
9
9
  # until a .git folder is found. This allows local configuration in a
@@ -11,6 +11,13 @@ module TcravitRubyLib #:nodoc:
11
11
  #
12
12
  # This module, inspired by a post on Practicing Ruby, implements that
13
13
  # kind of traversal for Ruby applications.
14
+ #
15
+ # Author:: Tammy Cravit (mailto:tammy@tammycravit.com)
16
+ # Copyright:: Copyright (c) 2011, Tammy Cravit. All rights reserved.
17
+ # License:: This program is free software: you can redistribute it and/or modify
18
+ # it under the terms of the GNU General Public License as published by
19
+ # the Free Software Foundation, either version 3 of the License, or
20
+ # (at your option) any later version.
14
21
  module ConfigSearcher
15
22
 
16
23
  # Locate a configuration folder by starting in the specified directory
@@ -1,3 +1,3 @@
1
1
  module TcravitRubyLib
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require "tcravit_ruby_lib/version"
2
2
  require 'tcravit_ruby_lib/config_searcher'
3
+ require 'tcravit_ruby_lib/app_config'
3
4
 
4
5
  module TcravitRubyLib # :nodoc:
5
6
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe "TcravitRubyLib::AppConfig" do
5
+
6
+ STRING_VAL = "string"
7
+ NUMERIC_VAL = 18293
8
+ SYMBOLIC_VAL = :squid
9
+
10
+ before(:all) do
11
+ TcravitRubyLib::AppConfig.configure do
12
+ string_value STRING_VAL
13
+ numeric_value NUMERIC_VAL
14
+ symbol_value SYMBOLIC_VAL
15
+ end
16
+ end
17
+
18
+ it "should return the values for defined properties" do
19
+ TcravitRubyLib::AppConfig.string_value.should == STRING_VAL
20
+ TcravitRubyLib::AppConfig.numeric_value.should == NUMERIC_VAL
21
+ TcravitRubyLib::AppConfig.symbol_value.should == SYMBOLIC_VAL
22
+ end
23
+
24
+ it "should raise an error for undefined properties" do
25
+ lambda { TcravitRubyLib::AppConfig.fishpaste_value }.should raise_error
26
+ end
27
+
28
+ it "should allow me to change a configuration property" do
29
+ TcravitRubyLib::AppConfig.string_value = "new value"
30
+ TcravitRubyLib::AppConfig.string_value.should == "new value"
31
+ end
32
+
33
+ it "should allow me to remove a configuration property" do
34
+ TcravitRubyLib::AppConfig.remove!(:string_value)
35
+ lambda { TcravitRubyLib::AppConfig.string_value }.should raise_error
36
+ end
37
+
38
+ it "should allow additional properties to be added" do
39
+ TcravitRubyLib::AppConfig.configure do
40
+ another_value 192
41
+ end
42
+ TcravitRubyLib::AppConfig.configure do
43
+ and_another_value 384
44
+ end
45
+ TcravitRubyLib::AppConfig.another_value.should == 192
46
+ TcravitRubyLib::AppConfig.and_another_value.should == 384
47
+ end
48
+
49
+ end
@@ -1,56 +1,54 @@
1
1
  require 'spec_helper'
2
2
  require 'fileutils'
3
3
 
4
- describe "TcravitRubyLib" do
5
- describe "ConfigSearcher" do
6
-
7
- BASE_DIR = '/tmp/config_searcher_test'
8
- DEEP_DIR = "#{BASE_DIR}/foo/bar/baz"
9
- CONFIG_DIR = "#{BASE_DIR}/.config"
10
-
11
- before(:all) do
12
- FileUtils.mkdir_p DEEP_DIR
13
- FileUtils.mkdir_p CONFIG_DIR
14
- end
15
-
16
- after(:all) do
17
- FileUtils.remove_dir BASE_DIR, true
18
- end
19
-
20
- it "should successfully find a directory which exists" do
21
- dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".config")
22
- dir_path.to_s.should_not be_nil
23
- dir_path.to_s.should == CONFIG_DIR
24
- end
4
+ describe "TcravitRubyLib::ConfigSearcher" do
5
+
6
+ BASE_DIR = '/tmp/config_searcher_test'
7
+ DEEP_DIR = "#{BASE_DIR}/foo/bar/baz"
8
+ CONFIG_DIR = "#{BASE_DIR}/.config"
9
+
10
+ before(:all) do
11
+ FileUtils.mkdir_p DEEP_DIR
12
+ FileUtils.mkdir_p CONFIG_DIR
13
+ end
14
+
15
+ after(:all) do
16
+ FileUtils.remove_dir BASE_DIR, true
17
+ end
18
+
19
+ it "should successfully find a directory which exists" do
20
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".config")
21
+ dir_path.to_s.should_not be_nil
22
+ dir_path.to_s.should == CONFIG_DIR
23
+ end
24
+
25
+ it "should not find a directory when one doesn't exist" do
26
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".snausages")
27
+ dir_path.to_s.should == ""
28
+ end
29
+
30
+ it "should return the container dir when the only_container_dir option is provided" do
31
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".config", only_container_dir: true)
32
+ dir_path.to_s.should == BASE_DIR
33
+ end
25
34
 
26
- it "should not find a directory when one doesn't exist" do
27
- dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".snausages")
28
- dir_path.to_s.should == ""
29
- end
35
+ it "should raise an exception when the start_in directory doesn't exist" do
36
+ an_exception = nil
30
37
 
31
- it "should return the container dir when the only_container_dir option is provided" do
32
- dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".config", only_container_dir: true)
33
- dir_path.to_s.should == BASE_DIR
34
- end
35
-
36
- it "should raise an exception when the start_in directory doesn't exist" do
37
- an_exception = nil
38
-
39
- begin
40
- dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: "#{DEEP_DIR}xxxxxxx", look_for: ".snausages")
41
- rescue => e
42
- an_exception = e
43
- end
44
-
45
- an_exception.should_not be_nil
46
- an_exception.message.should == "No such file or directory - #{DEEP_DIR}xxxxxxx"
38
+ begin
39
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: "#{DEEP_DIR}xxxxxxx", look_for: ".snausages")
40
+ rescue => e
41
+ an_exception = e
47
42
  end
48
43
 
49
- it "should behave the same for the alternative option names" do
50
- dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_dir: DEEP_DIR, config_dir: ".config")
51
- dir_path.to_s.should_not be_nil
52
- dir_path.to_s.should == CONFIG_DIR
53
- end
44
+ an_exception.should_not be_nil
45
+ an_exception.message.should == "No such file or directory - #{DEEP_DIR}xxxxxxx"
46
+ end
47
+
48
+ it "should behave the same for the alternative option names" do
49
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_dir: DEEP_DIR, config_dir: ".config")
50
+ dir_path.to_s.should_not be_nil
51
+ dir_path.to_s.should == CONFIG_DIR
54
52
  end
55
53
 
56
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tcravit_ruby_lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-02 00:00:00.000000000Z
12
+ date: 2011-12-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70224760100280 !ruby/object:Gem::Requirement
16
+ requirement: &70216638519080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70224760100280
24
+ version_requirements: *70216638519080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70224760099360 !ruby/object:Gem::Requirement
27
+ requirement: &70216638518460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70224760099360
35
+ version_requirements: *70216638518460
36
36
  description:
37
37
  email:
38
38
  - tcravit@taylored-software.com
@@ -47,9 +47,11 @@ files:
47
47
  - README.md
48
48
  - Rakefile
49
49
  - lib/tcravit_ruby_lib.rb
50
+ - lib/tcravit_ruby_lib/app_config.rb
50
51
  - lib/tcravit_ruby_lib/config_searcher.rb
51
52
  - lib/tcravit_ruby_lib/version.rb
52
53
  - spec/spec_helper.rb
54
+ - spec/tcravit_ruby_lib/app_config_spec.rb
53
55
  - spec/tcravit_ruby_lib/config_searcher_spec.rb
54
56
  - tcravit_ruby_lib.gemspec
55
57
  homepage: ''
@@ -78,5 +80,6 @@ specification_version: 3
78
80
  summary: Random reusable ruby stuff
79
81
  test_files:
80
82
  - spec/spec_helper.rb
83
+ - spec/tcravit_ruby_lib/app_config_spec.rb
81
84
  - spec/tcravit_ruby_lib/config_searcher_spec.rb
82
85
  has_rdoc: