Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

created gem added some classes

  • Loading branch information...
commit 2b1b09513d6fa951416a228469dd04b5007f8dbc 0 parents
@matthewrobertson authored
17 .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
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in ress.gemspec
+gemspec
22 LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2013 Matthew Robertson
+
+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.
29 README.md
@@ -0,0 +1,29 @@
+# Ress
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'ress'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install ress
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Add some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
1  Rakefile
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
16 lib/ress.rb
@@ -0,0 +1,16 @@
+require "ress/version"
+require "ress/device_category"
+
+module Ress
+
+ @@categories = []
+
+ def self.register_category(options)
+ category = DeviceCategory.new(options.delete(:name), options.delete(:media_type), options)
+ @@categories << category
+ end
+
+ def self.categories
+ @@categories
+ end
+end
28 lib/ress/controller_additions.rb
@@ -0,0 +1,28 @@
+module Ress
+
+ # This module is automatically included into all controllers.
+ module ControllerAdditions
+ module ClassMethods
+ # class methods go here
+ end
+
+ def self.included(base)
+ base.extend ClassMethods
+ # base.helper_method :can?, :cannot?, :current_ability
+ base.before_filter :prepend_category_view_path
+ end
+
+ def prepend_category_view_path
+ Ress.categories.each do |cat|
+ prepend_view_path(cat.view_path) if cat.matches?(request.subdomain)
+ end
+ end
+
+ end
+end
+
+if defined? ActionController::Base
+ ActionController::Base.class_eval do
+ include CanCan::ControllerAdditions
+ end
+end
26 lib/ress/device_category.rb
@@ -0,0 +1,26 @@
+module Ress
+
+ class DeviceCategory
+
+ attr_reader :name, :subdomain, :view_path
+
+ def initialize(name, media_query, options = {})
+ @name = name
+ @media_query = media_query
+ @subdomain = options.fetch(:subdomain, name)
+ @view_path = options.fetch(:view_path, default_view_path)
+ end
+
+ def matches?(subdomain)
+ self.subdomain == subdomain.split('.').first
+ end
+
+ private
+
+ def default_view_path
+ name
+ end
+
+ end
+
+end
3  lib/ress/version.rb
@@ -0,0 +1,3 @@
+module Ress
+ VERSION = "0.0.1"
+end
21 ress.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'ress/version'
+
+Gem::Specification.new do |gem|
+ gem.name = "ress"
+ gem.version = Ress::VERSION
+ gem.authors = ["Matthew Robertson"]
+ gem.email = ["matthewrobertson03@gmail.com"]
+ gem.description = %q{TODO: Write a gem description}
+ gem.summary = %q{TODO: Write a gem summary}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($/)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+
+ gem.add_development_dependency("rspec")
+end
54 spec/ress/controller_additions_spec.rb
@@ -0,0 +1,54 @@
+require_relative '../../lib/ress/controller_additions'
+
+class ActionControllerStub
+
+ attr_accessor :request
+
+ def self.before_filter(action)
+ @@action = action
+ end
+
+ def self.action
+ @@action
+ end
+
+end
+
+ActionControllerStub.class_eval do
+ include Ress::ControllerAdditions
+end
+
+describe Ress::ControllerAdditions do
+
+ it 'adds a before_filter to all actions when it is included' do
+ ActionControllerStub.action.should == :prepend_category_view_path
+ end
+
+ describe '#prepend_category_view_path' do
+
+ let(:controller) { ActionControllerStub.new }
+
+ before do
+ request = stub('request', :subdomain => 'foo')
+ controller.request = request
+ end
+
+ it 'prepends view paths of matching categories' do
+ category = stub('category', :matches? => true, :view_path => 'foo/bar')
+ Ress.stub(:categories => [category])
+
+ controller.should_receive(:prepend_view_path).with('foo/bar')
+ controller.prepend_category_view_path
+ end
+
+ it 'does not prepend view paths of categories that dont match' do
+ category = stub('category', :matches? => false, :view_path => 'foo/bar')
+ Ress.stub(:categories => [category])
+
+ controller.should_not_receive(:prepend_view_path)
+ controller.prepend_category_view_path
+ end
+
+ end
+
+end
56 spec/ress/device_category_spec.rb
@@ -0,0 +1,56 @@
+require_relative '../../lib/ress/device_category'
+
+describe Ress::DeviceCategory do
+
+ describe '#subdomain' do
+
+ it 'defaults to the name of the category' do
+ category = Ress::DeviceCategory.new('mobile', 'some media query')
+ category.subdomain.should == 'mobile'
+ end
+
+ it 'can be overridden by an optional parameter' do
+ category = Ress::DeviceCategory.new('mobile', 'some media query', :subdomain => 'foo')
+ category.subdomain.should == 'foo'
+ end
+
+ end
+
+ describe '#view_path' do
+
+ it 'defaults to something'
+
+ it 'can be overridden by an optional parameter' do
+ category = Ress::DeviceCategory.new('mobile', 'some media query', :view_path => 'foo')
+ category.view_path.should == 'foo'
+ end
+
+ end
+
+ describe '#matches?' do
+
+ let(:category) { Ress::DeviceCategory.new('mobile', 'some media query', :subdomain => 'foo') }
+
+ it 'returns true if the subdomain matches' do
+ category.matches?('foo').should be_true
+ end
+
+ it 'returns false if the subdomain does not match' do
+ category.matches?('bar').should be_false
+ end
+
+ context 'when there are multiple subdomains' do
+
+ it 'returns true if the first subdomain matches' do
+ category.matches?('foo.bar').should be_true
+ end
+
+ it 'returns false if the first subdomain does not match' do
+ category.matches?('bar.baz').should be_false
+ end
+
+ end
+
+ end
+
+end
31 spec/ress_spec.rb
@@ -0,0 +1,31 @@
+require_relative '../lib/ress'
+
+describe Ress do
+
+ describe '.configure' do
+
+ it 'yeilds Ress'
+
+ end
+
+ describe '.register_category' do
+
+ it 'adds a category to list' do
+ expect {
+ Ress.register_category({:name => 'stuff', :media_query => 'foo'})
+ }.to change { Ress.categories.count }.by(1)
+ end
+
+ it 'sets the name and media_query of the category' do
+ Ress.register_category({:name => 'stuff', :media_query => 'foo'})
+ Ress.categories.last.name.should == 'stuff'
+ end
+
+ it 'passes options tot he category' do
+ Ress.register_category({:name => 'stuff', :media_query => 'foo', :subdomain => 'm'})
+ Ress.categories.last.subdomain.should == 'm'
+ end
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.