configable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in configable.gemspec
4
+ gemspec
5
+ gem 'bundler'
6
+ gem 'ya2yaml'
7
+ gem 'rspec'
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+
6
+ spec_prereq = "spec:setup"
7
+
8
+ desc "rake spec"
9
+ task :default => [:spec]
10
+
11
+ desc "Run all specs in spec directory"
12
+ RSpec::Core::RakeTask.new(:spec => spec_prereq) do |t|
13
+ t.pattern = "spec/*_spec.rb"
14
+ end
15
+
16
+ namespace :spec do
17
+ desc "Run all specs in spec directory with documentation format."
18
+ RSpec::Core::RakeTask.new(:show => spec_prereq) do |t|
19
+ t.rspec_opts = ["--color --format documentation --backtrace"]
20
+ t.pattern = "./spec/**/*_spec.rb"
21
+ end
22
+
23
+ task :setup do
24
+ require "./spec/spec_helper" if File.exist?("./spec/spec_helper")
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "configable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "configable"
7
+ s.version = Configable::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["kmasamoto"]
10
+ s.email = ["masamoto@104.net"]
11
+ s.homepage = ""
12
+ s.summary = %q{add easy config file access to your class}
13
+ s.description = %q{add easy config file access to your class}
14
+
15
+ s.rubyforge_project = "configable"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,3 @@
1
+ module Configable
2
+ VERSION = "0.0.1"
3
+ end
data/lib/configable.rb ADDED
@@ -0,0 +1,61 @@
1
+ #!ruby -Ku
2
+ # -*- encoding: UTF-8 -*-
3
+ require 'ya2yaml'
4
+
5
+ class Module
6
+ def define_class_method(name)
7
+ (class << self; self end).module_eval { define_method(name) { yield } }
8
+ end
9
+ end
10
+
11
+ module Configable
12
+ def config(&block)
13
+ config = ConfigFile.new
14
+ config.instance_eval(&block)
15
+
16
+ define_class_method(config.method) { config }
17
+ define_method(config.method) { config }
18
+ end
19
+
20
+ class ConfigFile
21
+ # �ݒ�f�[�^�̃��[�h
22
+ def load
23
+ val=nil
24
+ # �t�@�C���̓ǂݍ���
25
+ if File.exist?(@file)
26
+ open(@file, "rb") do |f|
27
+ l = YAML::load(f)
28
+ val = @default.merge(l) if l.is_a? Hash
29
+ end
30
+ end
31
+ # �������ށi�����l��ύX�����ꍇ�ȂǁB
32
+ open(@file,"wb") {|f| f.puts val.ya2yaml(:syck_compatible => true) }
33
+ # ������������f�t�H���g�l��߂�
34
+ val
35
+ end
36
+
37
+ def load!
38
+ @values = load
39
+ end
40
+ def save!
41
+ # �������ށi�����l��ύX�����ꍇ�ȂǁB
42
+ open(@file,"wb") {|f| f.puts @values.ya2yaml(:syck_compatible => true) }
43
+ end
44
+
45
+ def [](*a)
46
+ @values ||= load
47
+ @values[*a]
48
+ end
49
+ def method(sym=nil)
50
+ return @method if not sym
51
+ @method = sym
52
+ end
53
+ def file(sym=nil)
54
+ return @file if not sym
55
+ @file = sym
56
+ end
57
+ def default(hash)
58
+ @default = hash
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,68 @@
1
+ #!ruby -Ku
2
+ # -*- encoding: UTF-8 -*-
3
+ $KCODE="UTF8"
4
+ require 'configable'
5
+ require 'yaml'
6
+
7
+ # �g�p��ł�
8
+ class ConfigUser
9
+ extend Configable
10
+ # ���[�U�N���X�����琄��
11
+ # �f�t�H���g�l�́A�K���Ȓl����Ă���
12
+ # �֐����́A�f�t�H���g�� config
13
+ config do
14
+ method :confs
15
+ file "cu.yaml"
16
+ default "test" => "data", "any" => "value"
17
+ end
18
+
19
+ def any
20
+ confs["any"]
21
+ end
22
+ end
23
+
24
+ class ConfigUser2
25
+ extend Configable
26
+ # ���[�U�N���X�����琄��
27
+ # �f�t�H���g�l�́A�K���Ȓl����Ă���
28
+ # �֐����́A�f�t�H���g�� config
29
+ config do
30
+ method :confs
31
+ file "cu.yaml"
32
+ default "test2" => "data2", "any2" => "value2"
33
+ end
34
+
35
+ def any
36
+ confs["any2"]
37
+ end
38
+ end
39
+
40
+ describe ConfigUser do
41
+ context "when new" do
42
+ it { subject.confs["test"].should == "data" }
43
+ it { subject.confs.file.should == "cu.yaml" }
44
+ it { subject.any.should == "value" }
45
+ end
46
+
47
+ context "added class method" do
48
+ subject { ConfigUser }
49
+ it { subject.confs["test"].should == "data" }
50
+ it { subject.confs.file.should == "cu.yaml" }
51
+ it { subject.confs["any"].should == "value" }
52
+ end
53
+ end
54
+
55
+ describe ConfigUser2 do
56
+ context "when new" do
57
+ it { subject.confs["test2"].should == "data2" }
58
+ it { subject.confs.file.should == "cu.yaml" }
59
+ it { subject.any.should == "value2" }
60
+ end
61
+
62
+ context "added class method" do
63
+ subject { ConfigUser2 }
64
+ it { subject.confs["test2"].should == "data2" }
65
+ it { subject.confs.file.should == "cu.yaml" }
66
+ it { subject.confs["any2"].should == "value2" }
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - kmasamoto
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-27 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: add easy config file access to your class
23
+ email:
24
+ - masamoto@104.net
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - configable.gemspec
36
+ - lib/configable.rb
37
+ - lib/configable/version.rb
38
+ - spec/configable_spec.rb
39
+ has_rdoc: true
40
+ homepage: ""
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: configable
69
+ rubygems_version: 1.6.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: add easy config file access to your class
73
+ test_files: []
74
+