castor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in castor.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Guillaume Malette
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Castor
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'castor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install castor
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'castor/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "castor"
8
+ gem.version = Castor::VERSION
9
+ gem.authors = ["Guillaume Malette"]
10
+ gem.email = ["gmalette@gmail.com"]
11
+ gem.description = %q{Castor is a configuration management gem. It can help write configuration apis for other gems}
12
+ gem.summary = %q{Castor is a configuration management gem.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "pry"
21
+ gem.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,8 @@
1
+ require "castor/version"
2
+ require "castor/configuration"
3
+
4
+ module Castor
5
+ def self.configure(&block)
6
+ Castor::Configuration.new(block)
7
+ end
8
+ end
@@ -0,0 +1,126 @@
1
+ require 'pry'
2
+
3
+ module Castor
4
+ class Configuration
5
+ def initialize(block)
6
+ @values = {}
7
+ instance_eval(&block)
8
+ end
9
+
10
+ def method_missing(name, *args, &block)
11
+ options = args.last.is_a?(::Hash) ? args.pop : {}
12
+
13
+ config_value = nil
14
+
15
+ if options[:nested]
16
+ config_value = Castor::Configuration.new(block)
17
+
18
+ selfclass.define_method(name) do
19
+ config_value
20
+ end
21
+ else
22
+ block = Proc.new {
23
+ default (args.first || options.delete(:lazy))
24
+ } unless block
25
+
26
+ config_value = Castor::Configuration::Value.new(name, block)
27
+
28
+ selfclass.define_method(name) do
29
+ config_value.value
30
+ end
31
+
32
+ selfclass.define_method("#{name}=") do |args|
33
+ config_value.value = args
34
+ end
35
+
36
+ selfclass.define_method("#{name}!") do |arg|
37
+ config_value
38
+ end
39
+ end
40
+
41
+ @values[name] = config_value
42
+ end
43
+
44
+ def call(attributes)
45
+ attributes.each do |key, value|
46
+ self.send key, value
47
+ end
48
+ end
49
+
50
+ def selfclass
51
+ class << self; self; end;
52
+ end
53
+
54
+
55
+ class Value
56
+ def initialize(name, block)
57
+ @name = name
58
+ instance_eval(&block)
59
+ self.value = @default
60
+ end
61
+
62
+ def value=(new_value)
63
+ if validate!(new_value)
64
+ @value = new_value
65
+ end
66
+ end
67
+
68
+ def value
69
+ lazy? ? lazy_value : @value
70
+ end
71
+
72
+ private
73
+
74
+ def lazy_value
75
+ v = @value.call
76
+ validate!(v, true)
77
+ v
78
+ end
79
+
80
+ def desc(description)
81
+ @description = description
82
+ end
83
+
84
+ def type(*types)
85
+ @types = types.flatten
86
+ end
87
+
88
+ def value_in(*values)
89
+ @possible_values = if values.length == 1 && values.first.is_a?(Enumerable)
90
+ values.first
91
+ else
92
+ values.flatten
93
+ end
94
+ end
95
+
96
+ def default(default_value = nil, &block)
97
+ @default = default_value || block
98
+ end
99
+
100
+ def lazy?(lazy_value = nil)
101
+ lazy_value = lazy_value || @value || @default
102
+ lazy_value.is_a?(Proc) && !(@types && @types.include?(Proc))
103
+ end
104
+
105
+ def validate!(new_value, jit = false)
106
+ return true if lazy?(new_value) && !jit
107
+
108
+ if (@possible_values && !@possible_values.include?(new_value))
109
+ raise_validation_error(new_value, "Value must be included in #{@possible_values.to_s}")
110
+ end
111
+
112
+ if (@types && @types.none?{|klass| new_value.is_a?(klass)})
113
+ raise_validation_error(new_value, "Value must be in types #{@types.to_s}")
114
+ end
115
+
116
+ true
117
+ end
118
+
119
+ def raise_validation_error(new_value, message)
120
+ raise InvalidValueError.new("Invalid value #{new_value} for #{@name}. #{message}")
121
+ end
122
+ end
123
+
124
+ class InvalidValueError < RuntimeError; end
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ module Castor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,94 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe Castor do
4
+ subject {
5
+ Castor.configure do |config|
6
+
7
+ # Complete syntax
8
+ config.toto do
9
+ type Integer
10
+ value_in 1..50
11
+ default 42
12
+ end
13
+
14
+ # Short syntax
15
+ config.titi "hello"
16
+
17
+ # Mass-assign syntax
18
+ config.(:mass => :assign, :is => :working, :for => 100)
19
+
20
+ # Nested
21
+ config.more :nested => true do |nested_config|
22
+ nested_config.titi :toto
23
+ end
24
+
25
+ # Nested through new Castor
26
+ config.other_nested Castor.configure{|nested_config|
27
+ nested_config.is_nested true
28
+ }
29
+
30
+ # Lazy Eval
31
+ config.time_now :lazy => lambda { Time.now }
32
+
33
+ # Lazy eval with block
34
+ config.lazy_increment do
35
+ type Fixnum
36
+ default 3
37
+ end
38
+
39
+ config.proc do
40
+ type Proc
41
+ default { 3 }
42
+ end
43
+ end
44
+ }
45
+
46
+ context "default values" do
47
+ its(:toto) { should == 42 }
48
+ its(:titi) { should == "hello" }
49
+ its(:mass) { should == :assign }
50
+ its(:is) { should == :working }
51
+ its(:for) { should == 100 }
52
+ end
53
+
54
+ context "nested values" do
55
+ it "sets the correct default values" do
56
+ subject.more.titi.should == :toto
57
+ subject.other_nested.is_nested.should be_true
58
+ end
59
+ end
60
+
61
+ context "lazy values" do
62
+ it "doesn't override procs" do
63
+ subject.proc.should be_a Proc
64
+ end
65
+ end
66
+
67
+ context "changing defaults" do
68
+ context "normal case" do
69
+ before {
70
+ subject.toto = 11
71
+ }
72
+
73
+ its(:toto) { should == 11 }
74
+ end
75
+
76
+ context "lazy eval" do
77
+ before {
78
+ i = 0
79
+ subject.lazy_increment = lambda { i += 1 }
80
+ }
81
+
82
+ it "evaluates the proc" do
83
+ subject.lazy_increment.should == 1
84
+ subject.lazy_increment.should == 2
85
+ end
86
+ end
87
+
88
+ context "to a value out of range" do
89
+ it "throws an exception" do
90
+ expect { subject.toto = 100 }.to raise_error Castor::Configuration::InvalidValueError
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,20 @@
1
+ require 'bundler/setup'
2
+ require 'castor'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # Require this file using `require "spec_helper"` to ensure that it is only
7
+ # loaded once.
8
+ #
9
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: castor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guillaume Malette
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pry
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Castor is a configuration management gem. It can help write configuration
47
+ apis for other gems
48
+ email:
49
+ - gmalette@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - castor.gemspec
61
+ - lib/castor.rb
62
+ - lib/castor/configuration.rb
63
+ - lib/castor/version.rb
64
+ - spec/castor_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Castor is a configuration management gem.
90
+ test_files:
91
+ - spec/castor_spec.rb
92
+ - spec/spec_helper.rb
93
+ has_rdoc: