confiture 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml CHANGED
@@ -1,10 +1,5 @@
1
1
  script: "bundle exec rake"
2
2
  rvm:
3
- - 1.8.7
4
3
  - 1.9.2
5
4
  - 1.9.3
6
- - rbx
7
- - rbx-2.0
8
- - ree
9
5
  - ruby-head
10
- - jruby
data/Gemfile.lock CHANGED
@@ -6,7 +6,13 @@ PATH
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
+ coderay (1.0.6)
9
10
  diff-lcs (1.1.3)
11
+ method_source (0.7.1)
12
+ pry (0.9.9.6)
13
+ coderay (~> 1.0.5)
14
+ method_source (~> 0.7.1)
15
+ slop (>= 2.4.4, < 3)
10
16
  rake (0.9.2.2)
11
17
  rspec (2.7.0)
12
18
  rspec-core (~> 2.7.0)
@@ -16,6 +22,7 @@ GEM
16
22
  rspec-expectations (2.7.0)
17
23
  diff-lcs (~> 1.1.2)
18
24
  rspec-mocks (2.7.0)
25
+ slop (2.4.4)
19
26
 
20
27
  PLATFORMS
21
28
  java
@@ -23,5 +30,6 @@ PLATFORMS
23
30
 
24
31
  DEPENDENCIES
25
32
  confiture!
26
- rake (~> 0.9.2.2)
27
- rspec (~> 2.7.0)
33
+ pry (~> 0.9.9)
34
+ rake (~> 0.9.2)
35
+ rspec (~> 2.7)
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ ## Infos
2
+
3
+ ![Status](http://stillmaintained.com/phoet/confiture.png "Status")
4
+ ![Build](http://travis-ci.org/phoet/confiture.png "Build")
5
+
6
+ ## Installation
7
+
8
+ gem install confiture
9
+
10
+ or in your Gemfile:
11
+
12
+ gem "confiture"
13
+
14
+ ## Usage
15
+
16
+ In order to use confiture, just include it into a configuration klass:
17
+
18
+ module Your
19
+ class Configuration
20
+ include Confiture::Configuration
21
+
22
+ confiture_allowed_keys(:secret, :key)
23
+ confiture_defaults(secret: 'SECRET_STUFF', key: 'EVEN_MOAR_SECRET')
24
+ end
25
+ end
26
+
27
+ Rails style initializer (config/initializers/asin.rb):
28
+
29
+ Your::Configuration.configure do |config|
30
+ config.secret = 'your-secret'
31
+ config.key = 'your-key'
32
+ end
33
+
34
+ YAML style configuration:
35
+
36
+ Your::Configuration.configure :yaml => 'config/some.yml'
37
+
38
+ Inline style configuration:
39
+
40
+ Your::Configuration.configure :secret => 'your-secret', :key => 'your-key'
41
+ # or
42
+ client.configure :secret => 'your-secret', :key => 'your-key'
43
+
44
+ ## License
45
+
46
+ "THE BEER-WARE LICENSE" (Revision 42):
47
+ ps@nofail.de[mailto:ps@nofail.de] wrote this file. As long as you retain this notice you
48
+ can do whatever you want with this stuff. If we meet some day, and you think
49
+ this stuff is worth it, you can buy me a beer in return Peter Schröder
data/confiture.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_development_dependency('rake', '~> 0.9.2.2')
22
- s.add_development_dependency('rspec', '~> 2.7.0')
21
+ s.add_development_dependency('rake', '~> 0.9.2')
22
+ s.add_development_dependency('rspec', '~> 2.7')
23
+ s.add_development_dependency('pry', '~> 0.9.9')
23
24
  end
@@ -1,49 +1,45 @@
1
1
  require "yaml"
2
- require 'logger'
3
2
 
4
3
  module Confiture
5
- class Configuration
6
- class << self
4
+ module Configuration
5
+ def self.included(base)
6
+ base.extend ClassExtension
7
+ end
7
8
 
8
- attr_accessor :secret, :key
9
+ module ClassExtension
10
+ def confiture_defaults(defaults)
11
+ @defaults = defaults
12
+ end
9
13
 
10
- # Rails initializer configuration.
11
- #
12
- # Expects at least +secret+ and +key+ for the API call:
14
+ def confiture_allowed_keys(*allowed_keys)
15
+ @allowed_keys = allowed_keys
16
+ end
17
+
18
+ # Rails initializer configuration:
13
19
  #
14
- # ASIN::Configuration.configure do |config|
20
+ # Confiture::Configuration.configure do |config|
15
21
  # config.secret = 'your-secret'
16
22
  # config.key = 'your-key'
17
23
  # end
18
24
  #
19
- # With the latest version of the Product Advertising API you need to include your associate_tag[https://affiliate-program.amazon.com/gp/advertising/api/detail/api-changes.html].
20
- #
21
25
  # You may pass options as a hash as well:
22
26
  #
23
- # ASIN::Configuration.configure :secret => 'your-secret', :key => 'your-key'
27
+ # Confiture::Configuration.configure :secret => 'your-secret', :key => 'your-key'
24
28
  #
25
29
  # Or configure everything using YAML:
26
30
  #
27
- # ASIN::Configuration.configure :yaml => 'config/asin.yml'
31
+ # Confiture::Configuration.configure :yaml => 'config/asin.yml'
28
32
  #
29
- # ASIN::Configuration.configure :yaml => 'config/asin.yml' do |config, yml|
30
- # config.key = yml[Rails.env]['aws_access_key']
33
+ # Confiture::Configuration.configure :yaml => 'config/asin.yml' do |config, yml|
34
+ # config.key = yml[Rails.env]['key']
31
35
  # end
32
36
  #
33
37
  # ==== Options:
34
38
  #
35
- # [secret] the API secret key (required)
36
- # [key] the API access key (required)
37
- # [associate_tag] your Amazon associate tag. Default is blank (required in latest API version)
38
- # [host] the host, which defaults to 'webservices.amazon.com'
39
- # [logger] a different logger than logging to STDERR (nil for no logging)
40
- # [version] a custom version of the API calls. Default is 2010-11-01
41
- # [item_type] a different class for SimpleItem, use :mash / :rash for Hashie::Mash / Hashie::Rash or :raw for a plain hash
42
- # [cart_type] a different class for SimpleCart, use :mash / :rash for Hashie::Mash / Hashie::Rash or :raw for a plain hash
43
- # [node_type] a different class for SimpleNode, use :mash / :rash for Hashie::Mash / Hashie::Rash or :raw for a plain hash
39
+ # [yaml|yml] path to a yaml file with configuration
44
40
  #
45
- def configure(options={})
46
- init_config
41
+ def configure(options={},reset=false)
42
+ init_config(reset)
47
43
  if yml_path = options[:yaml] || options[:yml]
48
44
  yml = File.open(yml_path) { |file| YAML.load(file) }
49
45
  if block_given?
@@ -63,26 +59,52 @@ module Confiture
63
59
  self
64
60
  end
65
61
 
62
+ def with_config(options={})
63
+ current_data = data
64
+ configure(options, true)
65
+ ensure
66
+ self.data = current_data
67
+ end
68
+
66
69
  # Resets configuration to defaults
67
70
  #
68
- def reset
71
+ def reset!
69
72
  init_config(true)
70
73
  end
71
74
 
72
- # Check if a key is set
73
- #
74
- def blank?(key)
75
- val = self.send :key
76
- val.nil? || val.empty?
75
+ private
76
+
77
+ def data
78
+ Thread.current[:confiture]
77
79
  end
78
80
 
79
- private
81
+ def data=(data)
82
+ Thread.current[:confiture] = data
83
+ end
80
84
 
81
85
  def init_config(force=false)
82
86
  return if @init && !force
83
- @init = true
84
- @secret = ''
85
- @key = ''
87
+ @init = true
88
+ self.data = Data.new(@defaults)
89
+ end
90
+
91
+ def method_missing(meth, *args)
92
+ meth = "#{meth}"
93
+ if meth =~ /.+=/ && args.size == 1
94
+ key = meth[0..-2].to_sym
95
+ check_key!(key)
96
+ data.options[key] = args.last
97
+ elsif args.size == 0
98
+ key = meth.to_sym
99
+ check_key!(key)
100
+ data.options[key]
101
+ else
102
+ super
103
+ end
104
+ end
105
+
106
+ def check_key!(key)
107
+ raise ArgumentError.new("#{key} is not allowed, use one of #{@allowed_keys}") if @allowed_keys && !@allowed_keys.include?(key)
86
108
  end
87
109
  end
88
110
  end
@@ -0,0 +1,10 @@
1
+ module Confiture
2
+ class Data
3
+ attr_accessor :options
4
+
5
+ def initialize(defaults)
6
+ @options = defaults.dup if defaults
7
+ @options ||= {}
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Confiture
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/confiture.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Confiture
2
2
  require 'confiture/version'
3
+ require 'confiture/data'
3
4
  require 'confiture/configuration'
4
5
  end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ module Confiture
4
+ class Config
5
+ include Confiture::Configuration
6
+ end
7
+
8
+ class Allowed
9
+ include Confiture::Configuration
10
+ confiture_allowed_keys(:key)
11
+ end
12
+
13
+ class Default
14
+ include Confiture::Configuration
15
+ confiture_defaults(:key => 'value')
16
+ end
17
+
18
+ describe Confiture do
19
+ context "defaults" do
20
+ it "should have default settings" do
21
+ Default.configure.key.should eql('value')
22
+ end
23
+
24
+ it "should restore defaults" do
25
+ Default.configure.key.should eql('value')
26
+ Default.configure(:key => 'bla').key.should eql('bla')
27
+ Default.reset!
28
+ Default.configure.key.should eql('value')
29
+ end
30
+ end
31
+ context "allowed_keys" do
32
+ it "should have access to allowed fields" do
33
+ Allowed.configure do |config|
34
+ config.key = 'bla'
35
+ end.key.should eql('bla')
36
+ end
37
+
38
+ it "should raise an error for not allowed keys" do
39
+ expect do
40
+ Allowed.configure do |config|
41
+ config.not_allowed_key = 'bla'
42
+ end
43
+ end.should raise_error(ArgumentError, "not_allowed_key is not allowed, use one of [:key]")
44
+ end
45
+
46
+ it "should access any field if not restricted" do
47
+ Config.configure do |config|
48
+ config.not_allowed_key = 'bla'
49
+ end.not_allowed_key.should eql('bla')
50
+ end
51
+ end
52
+
53
+ context "configuration" do
54
+ it "should work with a configuration block" do
55
+ Config.configure do |config|
56
+ config.key = 'bla'
57
+ end.key.should eql('bla')
58
+ end
59
+
60
+ it "should read configuration from yml" do
61
+ config = Config.configure :yaml => 'spec/config.yml'
62
+ config.secret.should eql('secret_yml')
63
+ config.key.should eql('key_yml')
64
+ end
65
+
66
+ it "should read configuration from yml with block" do
67
+ conf = Config.configure :yaml => 'spec/config.yml' do |config, yml|
68
+ config.secret = nil
69
+ config.key = yml['secret']
70
+ end
71
+ conf.secret.should be_nil
72
+ conf.key.should eql('secret_yml')
73
+ end
74
+ end
75
+
76
+ context "with_config" do
77
+ it "should have a config on a per block basis" do
78
+ config = Config.configure(:key => "value")
79
+ config.with_config(:key => "bla") do
80
+ Config.key.should eql("bla")
81
+ end
82
+ Config.key.should eql("value")
83
+ end
84
+ end
85
+ end
86
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift File.join(File.dirname(__FILE__),'..','..','lib')
2
2
 
3
3
  require 'rspec'
4
4
  require 'confiture'
5
- require 'pp'
5
+ require 'pry'
6
6
 
7
7
  RSpec.configure do |config|
8
8
  config.mock_with :rspec
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confiture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,30 +9,41 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-11 00:00:00.000000000 Z
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70363265328940 !ruby/object:Gem::Requirement
16
+ requirement: &70276619699480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.9.2.2
21
+ version: 0.9.2
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70363265328940
24
+ version_requirements: *70276619699480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70363265328440 !ruby/object:Gem::Requirement
27
+ requirement: &70276619698980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 2.7.0
32
+ version: '2.7'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70363265328440
35
+ version_requirements: *70276619698980
36
+ - !ruby/object:Gem::Dependency
37
+ name: pry
38
+ requirement: &70276619698520 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.9
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70276619698520
36
47
  description: Confiture helps with configuring your gem.
37
48
  email:
38
49
  - phoetmail@googlemail.com
@@ -47,14 +58,15 @@ files:
47
58
  - CHANGELOG.rdoc
48
59
  - Gemfile
49
60
  - Gemfile.lock
50
- - README.rdoc
61
+ - README.md
51
62
  - confiture.gemspec
52
63
  - lib/confiture.rb
53
64
  - lib/confiture/configuration.rb
65
+ - lib/confiture/data.rb
54
66
  - lib/confiture/version.rb
55
67
  - rakefile.rb
56
68
  - spec/config.yml
57
- - spec/config_spec.rb
69
+ - spec/configuration_spec.rb
58
70
  - spec/spec_helper.rb
59
71
  homepage: http://github.com/phoet/confiture
60
72
  licenses: []
@@ -82,5 +94,5 @@ specification_version: 3
82
94
  summary: Confiture helps with configuring your gem.
83
95
  test_files:
84
96
  - spec/config.yml
85
- - spec/config_spec.rb
97
+ - spec/configuration_spec.rb
86
98
  - spec/spec_helper.rb
data/README.rdoc DELETED
@@ -1,51 +0,0 @@
1
- == Infos
2
-
3
- Status: http://stillmaintained.com/phoet/asin.png
4
- Build: http://travis-ci.org/phoet/asin.png
5
-
6
-
7
- == Installation
8
-
9
- gem install confiture
10
-
11
- or in your Gemfile:
12
-
13
- gem "confiture"
14
-
15
- == Usage
16
-
17
- Rails style initializer (config/initializers/asin.rb):
18
-
19
- ASIN::Configuration.configure do |config|
20
- config.secret = 'your-secret'
21
- config.key = 'your-key'
22
- end
23
-
24
- YAML style configuration:
25
-
26
- ASIN::Configuration.configure :yaml => 'config/asin.yml'
27
-
28
- Inline style configuration:
29
-
30
- ASIN::Configuration.configure :secret => 'your-secret', :key => 'your-key'
31
- # or
32
- client.configure :secret => 'your-secret', :key => 'your-key'
33
-
34
- Have a look at ASIN::Configuration class for all the details.
35
-
36
- ===API Changes
37
-
38
- With the latest version of the Product Advertising API you need to include your associate_tag[https://affiliate-program.amazon.com/gp/advertising/api/detail/api-changes.html].
39
-
40
- ASIN::Configuration.configure do |config|
41
- config.secret = 'your-secret'
42
- config.key = 'your-key'
43
- config.associate_tag = 'your-tag'
44
- end
45
-
46
- == License
47
-
48
- "THE BEER-WARE LICENSE" (Revision 42):
49
- ps@nofail.de[mailto:ps@nofail.de] wrote this file. As long as you retain this notice you
50
- can do whatever you want with this stuff. If we meet some day, and you think
51
- this stuff is worth it, you can buy me a beer in return Peter Schröder
data/spec/config_spec.rb DELETED
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Confiture
4
- describe Confiture do
5
- context "configuration" do
6
- it "should fail with wrong configuration key" do
7
- lambda { @helper.configure :wrong => 'key' }.should raise_error(NoMethodError)
8
- end
9
-
10
- it "should work with a configuration block" do
11
- conf = Confiture::Configuration.configure do |config|
12
- config.key = 'bla'
13
- end
14
- conf.key.should eql('bla')
15
- end
16
-
17
- it "should read configuration from yml" do
18
- config = Confiture::Configuration.configure :yaml => 'spec/config.yml'
19
- config.secret.should eql('secret_yml')
20
- config.key.should eql('key_yml')
21
- end
22
-
23
- it "should read configuration from yml with block" do
24
- conf = Confiture::Configuration.configure :yaml => 'spec/config.yml' do |config, yml|
25
- config.secret = nil
26
- config.key = yml['secret']
27
- end
28
- conf.secret.should be_nil
29
- conf.key.should eql('secret_yml')
30
- end
31
- end
32
- end
33
- end