Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
hollow committed Apr 4, 2012
0 parents commit 07632d7
Show file tree
Hide file tree
Showing 19 changed files with 279 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .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
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm use --create ruby-1.9.3-p125@madvertise-ext
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: ruby
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
1 change: 1 addition & 0 deletions .yardopts
@@ -0,0 +1 @@
--no-private
15 changes: 15 additions & 0 deletions 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
22 changes: 22 additions & 0 deletions 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.
31 changes: 31 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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]
3 changes: 3 additions & 0 deletions lib/madvertise-ext.rb
@@ -0,0 +1,3 @@
require 'madvertise/ext/version'
require 'madvertise/ext/config'
require 'madvertise/ext/environment'
69 changes: 69 additions & 0 deletions 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
33 changes: 33 additions & 0 deletions 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
7 changes: 7 additions & 0 deletions lib/madvertise/ext/mask.reek
@@ -0,0 +1,7 @@
---
NestedIterators:
exclude:
- Configuration#build_config
FeatureEnvy:
exclude:
- Section#deep_merge
6 changes: 6 additions & 0 deletions lib/madvertise/ext/version.rb
@@ -0,0 +1,6 @@
module Madvertise
module Ext
# @private
VERSION = "0.1.0"
end
end
17 changes: 17 additions & 0 deletions 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
1 change: 1 addition & 0 deletions spec/spec.opts
@@ -0,0 +1 @@
--colour
26 changes: 26 additions & 0 deletions 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
5 changes: 5 additions & 0 deletions tasks/reek.rake
@@ -0,0 +1,5 @@
require 'reek/rake/task'

Reek::Rake::Task.new do |t|
t.fail_on_error = false
end
7 changes: 7 additions & 0 deletions 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
5 changes: 5 additions & 0 deletions tasks/yard.rake
@@ -0,0 +1,5 @@
require 'yard'

YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', 'README.rdoc']
end

0 comments on commit 07632d7

Please sign in to comment.