diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..9409848 --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm use --create ruby-1.9.3-p125@madvertise-ext diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b3a5c8b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: ruby +rvm: + - 1.8.7 + - 1.9.2 + - 1.9.3 diff --git a/.yardopts b/.yardopts new file mode 100644 index 0000000..22ce944 --- /dev/null +++ b/.yardopts @@ -0,0 +1 @@ +--no-private diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..6f9cfeb --- /dev/null +++ b/Gemfile @@ -0,0 +1,15 @@ +source :rubygems + +gemspec + +gem 'rake' +gem 'bundler' + +group :test do + gem 'redcarpet' + gem 'reek' + gem 'rspec' + gem 'ruby2ruby', '=1.3.0' # 1.3.1 is broken :( + gem 'simplecov' + gem 'yard' +end diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..229cf5a --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2012 madvertise Mobile Advertising GmbH + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ab161e7 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Madvertise::Ext + +The madvertise-ext gem provides a bunch of ruby extensions. + +[![Build Status](https://secure.travis-ci.org/madvertise/ext.png)](http://travis-ci.org/madvertise/ext) + +## Installation + +Add this line to your application's Gemfile: + + gem 'madvertise-ext' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install madvertise-ext + +## Usage + +Please refer to the [API documentation](http://rubydoc.info/gems/madvertise-ext/frames). + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Added some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..16f9fcb --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +#!/usr/bin/env rake + +require "bundler/setup" +require "bundler/gem_tasks" + +Dir['tasks/**/*.rake'].each { |t| load t } + +task :default => [:spec] diff --git a/lib/madvertise-ext.rb b/lib/madvertise-ext.rb new file mode 100644 index 0000000..21842da --- /dev/null +++ b/lib/madvertise-ext.rb @@ -0,0 +1,3 @@ +require 'madvertise/ext/version' +require 'madvertise/ext/config' +require 'madvertise/ext/environment' diff --git a/lib/madvertise/ext/config.rb b/lib/madvertise/ext/config.rb new file mode 100644 index 0000000..2bf349d --- /dev/null +++ b/lib/madvertise/ext/config.rb @@ -0,0 +1,69 @@ +require 'yaml' + +class Section < Hash + def deep_merge(other_hash) + self.merge(other_hash) do |key, oldval, newval| + oldval = oldval.to_hash if oldval.respond_to?(:to_hash) + newval = newval.to_hash if newval.respond_to?(:to_hash) + oldval.is_a?(Hash) && newval.is_a?(Hash) ? oldval.deep_merge(newval) : newval + end + end + + def deep_merge!(other_hash) + replace(deep_merge(other_hash)) + end + + def method_missing(name, *args) + if name.to_s[-1] == ?= + self[name.to_s[0..-2].to_sym] = args.first + else + value = self[name] + value.is_a?(Proc) ? value.call : value + end + end +end + +class Configuration < Section + def initialize(base_config_path, mode = :development, mixin_path = nil, mixins_to_use = []) + config = build_config(YAML.load(File.read(base_config_path))) + + @mode = mode + + self.update(config.generic) + self.deep_merge!(config[mode]) if config.has_key?(mode) + + @mixin_path = mixin_path + @mixins_to_use = mixins_to_use + + load_mixins if @mixin_path and @mixins_to_use.any? + end + + def mixin_files + @mixins_to_use.map do |mixin_name| + File.join(@mixin_path, "#{mixin_name}.yml") + end + end + + def load_mixins + mixin_files.each do |mixin_file| + mixin = build_config(YAML.load(File.read(mixin_file))) + if mixin.has_key?(@mode) + self.deep_merge!(mixin[@mode]) + else + self.deep_merge!(mixin) + end + end + end + + def build_config(hash) + Section.new.tap do |result| + hash.each do |key, value| + result[key.to_sym] = if value.is_a?(Hash) + build_config(value) + else + value + end + end + end + end +end diff --git a/lib/madvertise/ext/environment.rb b/lib/madvertise/ext/environment.rb new file mode 100644 index 0000000..b9cba7c --- /dev/null +++ b/lib/madvertise/ext/environment.rb @@ -0,0 +1,33 @@ +class Environment + attr_accessor :key + + def initialize(key) + @key = key + end + + def mode + ENV[@key] || 'development' + end + + def to_sym + mode.to_sym + end + + def prod? + to_sym == :production + end + + def dev? + to_sym == :development + end + + def test? + to_sym == :test + end + + def set(value) + ENV[@key] = value.to_s + end +end + +Env = Environment.new diff --git a/lib/madvertise/ext/mask.reek b/lib/madvertise/ext/mask.reek new file mode 100644 index 0000000..0e764ef --- /dev/null +++ b/lib/madvertise/ext/mask.reek @@ -0,0 +1,7 @@ +--- +NestedIterators: + exclude: + - Configuration#build_config +FeatureEnvy: + exclude: + - Section#deep_merge diff --git a/lib/madvertise/ext/version.rb b/lib/madvertise/ext/version.rb new file mode 100644 index 0000000..82a723e --- /dev/null +++ b/lib/madvertise/ext/version.rb @@ -0,0 +1,6 @@ +module Madvertise + module Ext + # @private + VERSION = "0.1.0" + end +end diff --git a/madvertise-ext.gemspec b/madvertise-ext.gemspec new file mode 100644 index 0000000..9cbf5bf --- /dev/null +++ b/madvertise-ext.gemspec @@ -0,0 +1,17 @@ +# -*- encoding: utf-8 -*- +require File.expand_path('../lib/madvertise/ext/version', __FILE__) + +Gem::Specification.new do |gem| + gem.name = "madvertise-ext" + gem.version = Madvertise::Ext::VERSION + gem.authors = ["Benedikt Böhm"] + gem.email = ["benedikt.boehm@madvertise.com"] + gem.description = %q{Ruby extensions} + gem.summary = %q{Ruby extensions} + gem.homepage = "https://github.com/madvertise/ext" + + gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + gem.files = `git ls-files`.split("\n") + gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + gem.require_paths = ["lib"] +end diff --git a/spec/spec.opts b/spec/spec.opts new file mode 100644 index 0000000..53607ea --- /dev/null +++ b/spec/spec.opts @@ -0,0 +1 @@ +--colour diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..ecc39e6 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,26 @@ +require 'rubygems' +require 'rspec' +require 'fileutils' + +require 'simplecov' +SimpleCov.start + +ROOT = "#{File.dirname(__FILE__)}/../tmp" + +$:.unshift(File.dirname(__FILE__) + '/../lib') +require 'madvertise-ext' + +RSpec.configure do |config| + # == Mock Framework + # + # RSpec uses it's own mocking framework by default. If you prefer to + # use mocha, flexmock or RR, uncomment the appropriate line: + # + # config.mock_with :mocha + # config.mock_with :flexmock + # config.mock_with :rr + + # setup a fake root + config.before(:all) { File.directory?(ROOT) ? FileUtils.rm_rf("#{ROOT}/*") : FileUtils.mkdir_p(ROOT) } + config.after(:all) { FileUtils.rm_rf("#{ROOT}/*") } +end diff --git a/tasks/reek.rake b/tasks/reek.rake new file mode 100644 index 0000000..68f7507 --- /dev/null +++ b/tasks/reek.rake @@ -0,0 +1,5 @@ +require 'reek/rake/task' + +Reek::Rake::Task.new do |t| + t.fail_on_error = false +end diff --git a/tasks/rspec.rake b/tasks/rspec.rake new file mode 100644 index 0000000..1375ffb --- /dev/null +++ b/tasks/rspec.rake @@ -0,0 +1,7 @@ +require 'rspec' +require 'rspec/core/rake_task' + +desc "Run the specs" +RSpec::Core::RakeTask.new do |t| + t.rspec_opts = ['--options', "spec/spec.opts"] +end diff --git a/tasks/yard.rake b/tasks/yard.rake new file mode 100644 index 0000000..3eee672 --- /dev/null +++ b/tasks/yard.rake @@ -0,0 +1,5 @@ +require 'yard' + +YARD::Rake::YardocTask.new do |t| + t.files = ['lib/**/*.rb', 'README.rdoc'] +end