kungfuig 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f6ba2a2d65c5d77ce06bd93df7f1c98e97211a7
4
+ data.tar.gz: b0088e41cc0db5dd9797dc05432151432a2a1e52
5
+ SHA512:
6
+ metadata.gz: 1162a74661dabaf6f796ff1ff78ee98ccf5b9bf76144c60f7ed6b81d2dc4fe4cdacce2b0190a22d6353a99381f1de33b4501560e9937bb0938dbf20ac2cae61e
7
+ data.tar.gz: 53d68ae2b530834e31fbd191387c4bc625f47c193bfdb29e59c3abc69aa4a83c2b64c1fa59acc04c5518ae22f9fa225821c3327008d67d9694ede03853275c46
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kungfuig.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Kungfuig
2
+
3
+ **Kungfuig** (_pronounced: [ˌkʌŋˈfig]_) provides a drastically easy way to plug configuration into everything.
4
+
5
+ [![Build Status](https://travis-ci.org/am-kantox/kungfuig.svg\?branch\=master)](https://travis-ci.org/am-kantox/kungfuig)
6
+
7
+ Configuration is thread-safe (starting with `0.2.0`).
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ class MyApp
13
+ include Kungfuig
14
+ end
15
+
16
+ # Load configuration file
17
+ MyApp.config('config/myapp.yml')
18
+
19
+ # Append options explicitly
20
+ MyApp.config do |options|
21
+ options.value = 42
22
+ end
23
+
24
+ # load options from JSON file and execute block on it
25
+ MyApp.config('config/myapp.json') do |options|
26
+ options.other_value = options[:value]
27
+ end
28
+
29
+ # DSL (note `kungfuig` method name)
30
+ MyApp.kungfuig do
31
+ set :value, 42
32
+ end
33
+ ```
34
+
35
+ ### Plugin to be called on method execution
36
+
37
+ ```ruby
38
+ class MyApp
39
+ include Kungfuig
40
+ def report
41
+ # ...
42
+ 42
43
+ end
44
+ end
45
+
46
+ MyApp.kungfuig do
47
+ plugin :report do |result|
48
+ puts "MyApp#report returned #{result}"
49
+ end
50
+ end
51
+
52
+ MyApp.new.report
53
+ #⇒ "MyApp#report returned 42"
54
+ ```
55
+
56
+ ## Installation
57
+
58
+ Add this line to your application's Gemfile:
59
+
60
+ ```ruby
61
+ gem 'kungfuig'
62
+ ```
63
+
64
+ And then execute:
65
+
66
+ $ bundle
67
+
68
+ Or install it yourself as:
69
+
70
+ $ gem install kungfuig
71
+
72
+ ## Include/extend each class/instance you want to have configuration options
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it ( https://github.com/[my-github-username]/kungfuig/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+
4
+ require 'bundler'
5
+ require 'bundler/setup'
6
+ require 'bundler/gem_tasks'
7
+
8
+ require 'rspec/core/rake_task'
9
+
10
+ begin
11
+ Bundler.setup(:default, :development, :test)
12
+ rescue Bundler::BundlerError => e
13
+ $stderr.puts e.message
14
+ $stderr.puts 'Run `bundle install` to install missing gems'
15
+ exit e.status_code
16
+ end
17
+
18
+ desc 'Tests'
19
+ RSpec::Core::RakeTask.new(:spec) do |spec|
20
+ spec.rspec_opts = '-Ispec'
21
+ # spec.rcov = true
22
+ end
23
+
24
+ require 'cucumber/rake/task'
25
+ desc 'Cucumber'
26
+ Cucumber::Rake::Task.new(:features)
27
+
28
+ task default: [:features, :spec]
data/kungfuig.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kungfuig/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'kungfuig'
8
+ spec.version = Kungfuig::VERSION
9
+ spec.authors = ['Kantox LTD']
10
+ spec.email = ['aleksei.matiushkin@kantox.com']
11
+ spec.licenses = ['MIT']
12
+
13
+ spec.summary = 'Simple but powerful config for any gem.'
14
+ spec.description = 'Config with goodnesses.'
15
+ spec.homepage = 'http://kantox.com'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(/\A(test|spec|features)\//) }
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(/\Abin\//) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ # spec.metadata['allowed_push_host'] = 'http://mygemserver.com' if spec.respond_to?(:metadata)
23
+
24
+ spec.add_dependency 'hashie', '~> 3'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.7'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'pry', '~> 0.10'
29
+
30
+ spec.add_development_dependency 'rspec', '~> 2.12'
31
+ spec.add_development_dependency 'cucumber', '~> 1.3'
32
+ spec.add_development_dependency 'yard', '~> 0'
33
+ end
@@ -0,0 +1,3 @@
1
+ module Kungfuig
2
+ VERSION = "0.3.0"
3
+ end
data/lib/kungfuig.rb ADDED
@@ -0,0 +1,134 @@
1
+ require 'kungfuig/version'
2
+ require 'yaml'
3
+ require 'hashie'
4
+
5
+ module Kungfuig
6
+ MX = Mutex.new
7
+
8
+ module InstanceMethods
9
+ # Configures everything by hash or yaml from string or file. Whether code block
10
+ # is passed, it is processed with @options instance.
11
+ # @param hos [String|Hash|Hashie::Mash] the input data to merge into options
12
+ def config hos = nil
13
+ MX.synchronize {
14
+ merge_hash_or_string! hos
15
+ yield options if block_given?
16
+ options
17
+ }
18
+ end
19
+
20
+ # Options getter
21
+ # @return [Hashie::Mash] options
22
+ def options
23
+ @options ||= Hashie::Mash.new
24
+ end
25
+ private :options
26
+
27
+ # Accepts:
28
+ # option :foo, :bar: baz
29
+ # option [:foo, 'bar', 'baz']
30
+ # option 'foo.bar.baz'
31
+ # option 'foo::bar::baz'
32
+ def option *keys
33
+ key = keys.join('.').gsub(/::/, '.').split('.')
34
+
35
+ MX.synchronize {
36
+ # options.foo!.bar!.baz!
37
+ build = [key, key[1..-1]].map do |candidate|
38
+ candidate.inject(options.dup) do |memo, k|
39
+ memo.public_send("#{k}") unless memo.nil?
40
+ end
41
+ end.reduce(nil) do |memo, candidate|
42
+ memo || candidate
43
+ end
44
+ }
45
+ end
46
+
47
+ # Accepts:
48
+ # option! [:foo, 'bar', 'baz'], value
49
+ # option! 'foo.bar.baz', value
50
+ # option! 'foo::bar::baz', value
51
+ def option! keys, value
52
+ key = (keys.is_a?(Array) ? keys.join('.') : keys).gsub(/::/, '.').split('.')
53
+ last = key.pop
54
+
55
+ MX.synchronize {
56
+ # options.foo!.bar!.baz! = value
57
+ build = key.inject(options) do |memo, k|
58
+ memo.public_send("#{k}!")
59
+ end
60
+ build[last] = value
61
+ }
62
+ end
63
+
64
+ def option? *keys
65
+ !option(*keys).nil?
66
+ end
67
+
68
+ # @param hos [Hash|String] the new values taken from hash,
69
+ # mash or string (when string, should be either valid YAML file name or
70
+ # string with valid YAML)
71
+ def merge_hash_or_string! hos
72
+ options.deep_merge! case hos
73
+ when NilClass then {} # aka skip
74
+ when Hash then hos
75
+ when String
76
+ begin
77
+ File.exists?(hos) ? Hashie::Mash.load(hos) : Hashie::Mash.new(YAML.load(hos))
78
+ rescue ArgumentError => ae
79
+ fail ArgumentError.new "#{__callee__} expects valid YAML configuration file. [#{hos}] contains invalid syntax."
80
+ rescue Psych::SyntaxError => pse
81
+ fail ArgumentError.new "#{__callee__} expects valid YAML configuration string. Got:\n#{hos}"
82
+ end
83
+ else
84
+ fail ArgumentError.new "#{__callee__} accepts either String or Hash as parameter."
85
+ end
86
+ end
87
+ private :merge_hash_or_string!
88
+ end
89
+
90
+ def self.included base
91
+ base.include InstanceMethods
92
+ base.extend ClassMethods
93
+ if (base.instance_methods & [:[], :[]=]).empty?
94
+ base.send :alias_method, :[], :option
95
+ base.send :alias_method, :[]=, :option!
96
+ end
97
+ end
98
+
99
+ def self.extended base
100
+ base.include ClassMethods
101
+ end
102
+
103
+ module ClassMethods
104
+ include InstanceMethods
105
+
106
+ # A wrapper for the configuration block
107
+ # @param block the block to be executed in the context of this module
108
+ def kungfuig &block
109
+ instance_eval(&block)
110
+ end
111
+
112
+ def plugin meth
113
+ fail ArgumentError.new "Plugin must have a codeblock" unless block_given?
114
+ fail NoMethodError.new "Plugin must be attached to existing method" unless instance_methods.include? meth.to_sym
115
+
116
+ ((@plugins ||= {})[meth.to_sym] ||= []) << Proc.new
117
+ plugins = @plugins
118
+ class_eval do
119
+ unless instance_methods(true).include?(:"∃#{meth}")
120
+ alias_method :"∃#{meth}", meth.to_sym
121
+ define_method meth.to_sym do |*args|
122
+ send(:"∃#{meth}", *args).tap do |result|
123
+ plugins[meth.to_sym].each do |p|
124
+ p.call result
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ end
131
+ end
132
+ alias_method :set, :option!
133
+ end
134
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kungfuig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Kantox LTD
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hashie
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: cucumber
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Config with goodnesses.
112
+ email:
113
+ - aleksei.matiushkin@kantox.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - README.md
123
+ - Rakefile
124
+ - kungfuig.gemspec
125
+ - lib/kungfuig.rb
126
+ - lib/kungfuig/version.rb
127
+ homepage: http://kantox.com
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.4.8
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Simple but powerful config for any gem.
151
+ test_files: []
152
+ has_rdoc: