coconut 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ Gemfile.lock
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@coconut --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in coconut.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Javier Acero
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,87 @@
1
+ #coconut [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/jacegu/coconut)
2
+
3
+ A coconfiguration tool for your Ruby projects.
4
+
5
+ ![coconut for fun and profit](https://dl.dropbox.com/u/1130242/coconut.jpg)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'coconut'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install coconut
20
+
21
+
22
+ ## What is coconut?
23
+ Coconut provides **a simple DSL that allows you to configure your application's
24
+ assets on different environments**
25
+
26
+ ```ruby
27
+ require 'coconut'
28
+
29
+ Coconut.configure MyApp do
30
+ twitter do
31
+ environment :development do
32
+ consumer_key 'development_key'
33
+ consumer_secret 'development_secret'
34
+ end
35
+
36
+ environment :production do
37
+ consumer_key 'production_key'
38
+ consumer_secret 'production_secret'
39
+ end
40
+ end
41
+ end
42
+ ```
43
+
44
+ You only need to require file from your app. Coconut will define a
45
+ `config` method on your application's namespace that you can use to query it.
46
+ **You don't have to specify the environment when querying for configuration
47
+ values**. Coconut will only run the configuration for the environment it's
48
+ running on.
49
+
50
+ ```ruby
51
+ ENV['RACK_ENV'] = :development
52
+ MyApp.config.twitter.consumer_key # => development_key
53
+
54
+ ENV['RACK_ENV'] = :production
55
+ MyApp.config.twitter.consumer_key # => production_key
56
+ ```
57
+
58
+ ## Why coconut?
59
+ TODO
60
+
61
+
62
+ ## Coconut flavours
63
+ TODO
64
+ ### Single file
65
+ TODO
66
+
67
+ ### Folder
68
+ TODO
69
+
70
+ ### List of files
71
+ TODO
72
+
73
+
74
+ ## How is the environment detected?
75
+ Right now coconut uses the environment variable `RACK_ENV` to detect the
76
+ environment the app is running on. If `RACK_ENV` is not set or is empty it
77
+ defaults to `:development`.
78
+
79
+ ### Changing the environment
80
+ TODO
81
+
82
+ ## Contributing
83
+ 1. Fork it
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'coconut/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "coconut"
8
+ gem.version = Coconut::VERSION
9
+ gem.authors = ["Javier Acero"]
10
+ gem.email = ["javier@path11.com"]
11
+ gem.description = %q{A coconfiguration tool for your Ruby applications }
12
+ gem.summary = %q{Coconut is a simple DSL that allows you to easily write and query your application's configuration with pure Ruby.}
13
+ gem.homepage = "http://github.com/jacegu/coconut"
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_runtime_dependency 'hashie'
21
+
22
+ gem.add_development_dependency 'cucumber'
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rspec'
25
+ end
@@ -0,0 +1,16 @@
1
+ Feature: Single file configuration
2
+
3
+ Scenario: One environment configuration
4
+ Given my application is configured like this:
5
+ """
6
+ Coconut.configure(MyApp) do
7
+ ftp do
8
+ environment(:development) do
9
+ user 'root'
10
+ end
11
+ end
12
+ end
13
+ """
14
+ When I run my application on the "development" environment
15
+ And I query the configuration for "ftp.user"
16
+ Then the configured value should be "root"
@@ -0,0 +1,16 @@
1
+ Given /^my application is configured like this:$/ do |configuration|
2
+ @app_config = configuration
3
+ end
4
+
5
+ When /^I run my application on the "(.*?)" environment$/ do |environment|
6
+ ENV['RACK_ENV']= environment
7
+ eval @app_config
8
+ end
9
+
10
+ When /^I query the configuration for "(.*?)"$/ do |query|
11
+ @query = "MyApp.config.#{query}"
12
+ end
13
+
14
+ Then /^the configured value should be "(.*?)"$/ do |expected_result|
15
+ eval(@query).should eq expected_result
16
+ end
@@ -0,0 +1,5 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+
3
+ require 'coconut'
4
+
5
+ module MyApp; end
@@ -0,0 +1,27 @@
1
+ require_relative 'coconut/version'
2
+ require_relative 'coconut/config'
3
+
4
+ module Coconut
5
+ def self.configure(namespace, &assets)
6
+ raise "#{namespace} already has a config method" if namespace.respond_to? :config
7
+ define_config_method namespace, Config.new(current_environment, &assets)
8
+ end
9
+
10
+ def self.current_environment
11
+ ENV['RACK_ENV'] || :development
12
+ end
13
+
14
+ private
15
+
16
+ def self.define_config_method(namespace, configuration)
17
+ singleton_class_of(namespace).instance_eval do
18
+ define_method :config do
19
+ @_coconut_configuration ||= configuration
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.singleton_class_of(class_or_module)
25
+ class << class_or_module; self; end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require 'delegate'
2
+ require 'hashie'
3
+ require_relative 'environment'
4
+
5
+ module Coconut
6
+ class Asset < DelegateClass(Hashie::Mash)
7
+ def initialize(current_environment, &config)
8
+ super Hashie::Mash.new
9
+ run config, current_environment if block_given?
10
+ end
11
+
12
+ def environment(environment_name, &config)
13
+ merge! Environment.new(&config) if current? environment_name
14
+ end
15
+
16
+ private
17
+
18
+ def run(config, environment)
19
+ @current_environment = environment
20
+ instance_eval &config
21
+ end
22
+
23
+ def current?(environment)
24
+ environment.to_sym == @current_environment.to_sym
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ require 'delegate'
2
+ require 'hashie'
3
+ require_relative 'asset'
4
+ require_relative 'runner'
5
+
6
+ module Coconut
7
+ class Config < DelegateClass(Hashie::Mash)
8
+ def initialize(current_environment, &assets_config)
9
+ super Hashie::Mash.new
10
+ run assets_config, current_environment
11
+ end
12
+
13
+ private
14
+
15
+ def run(config, environment)
16
+ Runner.new run: config, method_missing: configure_asset_on(environment)
17
+ end
18
+
19
+ def configure_asset_on(environment)
20
+ ->(name, *args, &config){ self[name] = Asset.new(environment, &config) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ require 'delegate'
2
+ require 'hashie'
3
+ require_relative 'runner'
4
+
5
+ module Coconut
6
+ class Environment < DelegateClass(Hashie::Mash)
7
+ def initialize(&config)
8
+ super Hashie::Mash.new
9
+ run config
10
+ end
11
+
12
+ private
13
+
14
+ def run(config)
15
+ Runner.new run: config, method_missing: method(:configure_property)
16
+ end
17
+
18
+ def configure_property(name, values)
19
+ self[name] = values.first
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ module Coconut
2
+ class BlankSlate < BasicObject
3
+ private
4
+
5
+ def self.inherited(subclass)
6
+ eraseable_methods.each{ |method_name| undef_method method_name }
7
+ end
8
+
9
+ def self.eraseable_methods
10
+ instance_methods - PERMANENT_METHODS
11
+ end
12
+
13
+ PERMANENT_METHODS = [:instance_eval, :__send__, :object_id]
14
+ end
15
+
16
+ class Runner < BlankSlate
17
+ def initialize(options)
18
+ @callback = options[:method_missing]
19
+ instance_eval &options[:run]
20
+ end
21
+
22
+ private
23
+
24
+ def method_missing(name, *args, &block)
25
+ @callback.(name, args, &block)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Coconut
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'coconut/asset'
2
+
3
+ describe Coconut::Asset do
4
+ it "knows the asset's config on the current environment" do
5
+ subject = described_class.new(:current) do
6
+ environment(:other) { property 'value on other' }
7
+ environment(:current) { property 'value on current' }
8
+ end
9
+ subject.should have_key 'property'
10
+ subject.fetch('property').should eq 'value on current'
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ require 'coconut'
2
+
3
+ class MyClass; end
4
+ module MyModule; end
5
+ module MyConfig; def self.config; end; end;
6
+
7
+ describe Coconut do
8
+ it 'defines a config method in the provided namespace' do
9
+ Coconut.configure(MyClass){}
10
+ MyClass.should respond_to(:config)
11
+ Coconut.configure(MyModule){}
12
+ MyModule.should respond_to(:config)
13
+ end
14
+
15
+ it 'raises an error if the namespace already has a config method' do
16
+ expect { Coconut.configure(MyConfig, :asset){} }.to raise_error
17
+ end
18
+ end
19
+
20
+
@@ -0,0 +1,34 @@
1
+ require 'coconut/config'
2
+
3
+ describe Coconut::Config do
4
+ let(:asset_config) { Coconut::Asset }
5
+
6
+ it 'configures a series of assets' do
7
+ subject = described_class.new(:current) do
8
+ ftp {}
9
+ ssh {}
10
+ end
11
+ subject.should have_key 'ftp'
12
+ subject.should have_key 'ssh'
13
+ end
14
+
15
+ it 'runs the configuration of each asset' do
16
+ ftp_config = lambda {}
17
+ ssh_config = lambda {}
18
+ asset_config.should_receive(:new).with(:current, &ftp_config)
19
+ asset_config.should_receive(:new).with(:current, &ssh_config)
20
+ described_class.new(:current) do
21
+ ftp &ftp_config
22
+ ssh &ssh_config
23
+ end
24
+ end
25
+
26
+ it 'assets can have (almost) any name' do
27
+ subject = described_class.new(:current) do
28
+ __id__ {}
29
+ equal? {}
30
+ inspect {}
31
+ end
32
+ ['__id__', 'equal?', 'inspect'].each { |key| subject.should have_key(key) }
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ require 'hashie'
2
+ require 'coconut/environment'
3
+
4
+ describe Coconut::Environment do
5
+ it 'collects method calls as configuration values' do
6
+ config = described_class.new { property 'value' }
7
+ config.should have_key :property
8
+ config.property.should eq 'value'
9
+ end
10
+
11
+ it 'can have (almost) any property name' do
12
+ config = described_class.new do
13
+ __id__ 11
14
+ equal? 33
15
+ inspect 'CoconutJuice'
16
+ end
17
+ config.fetch('__id__').should eq 11
18
+ config.fetch('equal?').should eq 33
19
+ config.fetch('inspect').should eq 'CoconutJuice'
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coconut
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Javier Acero
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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: cucumber
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
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: ! 'A coconfiguration tool for your Ruby applications '
79
+ email:
80
+ - javier@path11.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rvmrc
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - coconut.gemspec
92
+ - features/single_file_configuration.feature
93
+ - features/step_definitions/configuration_steps.rb
94
+ - features/support/env.rb
95
+ - lib/coconut.rb
96
+ - lib/coconut/asset.rb
97
+ - lib/coconut/config.rb
98
+ - lib/coconut/environment.rb
99
+ - lib/coconut/runner.rb
100
+ - lib/coconut/version.rb
101
+ - spec/coconut/asset_spec.rb
102
+ - spec/coconut/coconut_spec.rb
103
+ - spec/coconut/config_spec.rb
104
+ - spec/coconut/environment_spec.rb
105
+ homepage: http://github.com/jacegu/coconut
106
+ licenses: []
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ segments:
118
+ - 0
119
+ hash: 3927338585615885072
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ segments:
127
+ - 0
128
+ hash: 3927338585615885072
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.24
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Coconut is a simple DSL that allows you to easily write and query your application's
135
+ configuration with pure Ruby.
136
+ test_files:
137
+ - features/single_file_configuration.feature
138
+ - features/step_definitions/configuration_steps.rb
139
+ - features/support/env.rb
140
+ - spec/coconut/asset_spec.rb
141
+ - spec/coconut/coconut_spec.rb
142
+ - spec/coconut/config_spec.rb
143
+ - spec/coconut/environment_spec.rb