cat-0.2.1/0000755000175100017500000000000012655731157011320 5ustar mmollmmollcat-0.2.1/metadata.yml0000644000175100017500000000532012655731157013623 0ustar mmollmmoll--- !ruby/object:Gem::Specification name: cat version: !ruby/object:Gem::Version version: 0.2.1 prerelease: platform: ruby authors: - Chris Doyle autorequire: bindir: bin cert_chain: [] date: 2012-04-27 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: rspec requirement: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: rdoc requirement: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: jeweler requirement: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' description: Create objects in a sandbox email: archslide@gmail.com executables: [] extensions: [] extra_rdoc_files: - LICENSE.txt - README.rdoc files: - .document - .rvmrc - .travis.yml - Gemfile - Gemfile.lock - LICENSE.txt - README.rdoc - Rakefile - VERSION - cat.gemspec - lib/cat.rb - lib/sandbox.rb - spec/cat_spec.rb homepage: http://github.com/arches/cat licenses: - MIT post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' segments: - 0 hash: 1174699877385095298 required_rubygems_version: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 1.8.19 signing_key: specification_version: 3 summary: Create objects in a sandbox test_files: [] cat-0.2.1/spec/0000755000175100017500000000000012655731157012252 5ustar mmollmmollcat-0.2.1/spec/cat_spec.rb0000644000175100017500000000613412655731157014364 0ustar mmollmmollrequire 'sandbox' describe Sandbox do before(:each) do Sandbox.cleanup! Sandbox.constants.should == [] end describe "#cleanup!" do it "removes all the classes defined by Sandbox" do Sandbox.add_class("Foo") constants = Sandbox.created_classes Sandbox.cleanup! Sandbox.created_classes.collect(&:to_s).should == ["Foo"] constants.each do |const| Sandbox.constants.should_not include(const) end end end describe "#add_class" do it "creates a class namedspaced under Sandbox" do Sandbox.add_class("Foo") Sandbox.constants.collect(&:to_s).should include("Foo") end describe "when you pass a double-colon delimited string" do it "creates nested subclasses" do Sandbox.add_class("Bar::Wood") Sandbox.constants.collect(&:to_s).should include("Bar") Sandbox::Bar.constants.collect(&:to_s).should include("Wood") end end end describe "#add_attributes" do it "adds getter and setter methods to the class" do Sandbox.add_class("Soap::Ivory::Powdered") Sandbox.add_attributes("Soap::Ivory", "id", :title) ivory = Sandbox::Soap::Ivory.new ivory.id = 10 ivory.title = "Toweling off" ivory.id.should == 10 ivory.title.should == "Toweling off" end it "adds an initialize method to set attributes with a hash" do Sandbox.add_class("Soap::Ivory::Powdered") Sandbox.add_attributes("Soap::Ivory", "id", :title) ivory = Sandbox::Soap::Ivory.new(:id => 10, :title => "Toweling off") ivory.id.should == 10 ivory.title.should == "Toweling off" end end describe "#add_method" do context "passing a proc" do it "adds an instance method" do Sandbox.add_class("Soap::Ivory::Liquid") Sandbox.add_method("Soap::Ivory", :imbroglio, lambda { 'he said she said' }) ivory = Sandbox::Soap::Ivory.new ivory.imbroglio.should == 'he said she said' end end context "passing a block" do it "adds an instance method" do Sandbox.add_class("Soap::Ivory::Liquid") Sandbox.add_method("Soap::Ivory", :imbroglio) { 'he said she said' } ivory = Sandbox::Soap::Ivory.new ivory.imbroglio.should == 'he said she said' end end end describe "#add_class_method" do context "passing a proc" do it "adds a class method" do Sandbox.add_class("Foo") Sandbox.add_class_method("Foo", "bar", lambda { "foo foo" }) Sandbox::Foo.bar.should == "foo foo" end end context "passing a block" do it "adds a class method" do Sandbox.add_class("Foo") Sandbox.add_class_method("Foo", "bar") { "foo " * 2 } Sandbox::Foo.bar.should == "foo foo " end end end describe "setting a subclass on an existing subclass" do it "doesn't raise a warning" do orig_stderr = $stderr $stderr = StringIO.new Sandbox.add_class("Foo::Bar") Sandbox.add_class("Foo::Babar") $stderr.rewind $stderr.string.chomp.should == "" $stderr = orig_stderr end end end cat-0.2.1/lib/0000755000175100017500000000000012655731157012066 5ustar mmollmmollcat-0.2.1/lib/sandbox.rb0000644000175100017500000000333112655731157014051 0ustar mmollmmollmodule Sandbox # keep track of the top-level classes we create, so we can destroy only those methods # if we get mixed in somewhere @@created_classes = [] def self.add_class(klass) const_set_from_string(klass) end def self.add_attributes(klass, *attrs) self.const_get_from_string(klass).class_eval do attr_accessor *attrs # set up the 'initialize' method to assign the attributes define_method(:initialize) do |*value_hash| value_hash = value_hash.first value_hash ||= {} value_hash.each do |k, v| instance_variable_set("@#{k.to_s}", v) end end end end def self.add_method(klass, method_name, proc_obj=nil, &blk) self.const_get_from_string(klass).send(:define_method, method_name, proc_obj || blk) end def self.add_class_method(klass, method_name, proc_obj=nil, &blk) singleton = ( class << self.const_get_from_string(klass); self end) singleton.send(:define_method, method_name, proc_obj || blk) end def self.cleanup! constants.each { |klass| remove_const klass } end def self.scoop! self.cleanup! end def self.created_classes @@created_classes end def self.const_get_from_string(klass) klass.split("::").inject(self) { |const, klass| const.const_get(klass) } end def self.const_set_from_string(delimited_klasses) # try to const_get before set const_set to avoid "already initialized" warnings delimited_klasses.split("::").inject(self) do |const, klass| const.constants.collect(&:to_s).include?(klass.to_s) ? const.const_get(klass) : const.const_set(klass, Class.new) end @@created_classes << delimited_klasses.split("::").first @@created_classes.uniq! end end cat-0.2.1/lib/cat.rb0000644000175100017500000000002212655731157013154 0ustar mmollmmollrequire 'sandbox' cat-0.2.1/cat.gemspec0000644000175100017500000000327012655731157013436 0ustar mmollmmoll# Generated by jeweler # DO NOT EDIT THIS FILE DIRECTLY # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = "cat" s.version = "0.2.1" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Chris Doyle"] s.date = "2012-04-27" s.description = "Create objects in a sandbox" s.email = "archslide@gmail.com" s.extra_rdoc_files = [ "LICENSE.txt", "README.rdoc" ] s.files = [ ".document", ".rvmrc", ".travis.yml", "Gemfile", "Gemfile.lock", "LICENSE.txt", "README.rdoc", "Rakefile", "VERSION", "cat.gemspec", "lib/cat.rb", "lib/sandbox.rb", "spec/cat_spec.rb" ] s.homepage = "http://github.com/arches/cat" s.licenses = ["MIT"] s.require_paths = ["lib"] s.rubygems_version = "1.8.19" s.summary = "Create objects in a sandbox" if s.respond_to? :specification_version then s.specification_version = 3 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) end else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) end end cat-0.2.1/VERSION0000644000175100017500000000000512655731157012363 0ustar mmollmmoll0.2.1cat-0.2.1/Rakefile0000644000175100017500000000211712655731157012766 0ustar mmollmmoll# encoding: utf-8 require 'rubygems' require 'bundler' begin Bundler.setup(:default, :development) rescue Bundler::BundlerError => e $stderr.puts e.message $stderr.puts "Run `bundle install` to install missing gems" exit e.status_code end require 'rake' require 'jeweler' Jeweler::Tasks.new do |gem| # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options gem.name = "cat" gem.homepage = "http://github.com/arches/cat" gem.license = "MIT" gem.summary = %Q{Create objects in a sandbox} gem.description = %Q{Create objects in a sandbox} gem.email = "archslide@gmail.com" gem.authors = ["Chris Doyle"] # dependencies defined in Gemfile end Jeweler::RubygemsDotOrgTasks.new require 'rspec/core/rake_task' desc "Run specs" RSpec::Core::RakeTask.new do |t| end task :default => :spec require 'rdoc/task' Rake::RDocTask.new do |rdoc| version = File.exist?('VERSION') ? File.read('VERSION') : "" rdoc.rdoc_dir = 'rdoc' rdoc.title = "cat #{version}" rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('lib/**/*.rb') end cat-0.2.1/README.rdoc0000644000175100017500000000461012655731157013127 0ustar mmollmmoll= cat Create objects in a sandbox This project grew out of a project where I needed to create complicated object definitions and didn't want to write it out in classic ruby syntax (clunky and fragile). So, this gem lets you easily generate classes inside a sandbox and clean it out the namespace between tests. {Build Status}[http://travis-ci.org/arches/cat] == Installation gem install cat == Usage require 'cat' === Creating classes Add a single class > Sandbox.add_class("Foo") > Sandbox::Foo.new => # Add a subclass > Sandbox.add_class("Foo::Bar") > Sandbox::Foo::Bar.new => # Add some attributes to a class > Sandbox.add_attributes("Foo::Bar", "id", :title) > fb = Sandbox::Foo::Bar.new(id: 10, title: "I like chicken") # it adds an initialize method for you too > fb.id => 10 > fb.title => "I like chicken" Add a method to a class > Sandbox.add_method("Foo::Bar", :food, lambda{"I like liver"}) > fb = Sandbox::Foo::Bar.new > fb.food => "I like liver" Or do it with a block > Sandbox.add_method("Foo::Bar", :food) {"Meow mix meow mix"} > fb = Sandbox::Foo::Bar.new > fb.food => "Meow mix meow mix" Class methods work the same way > Sandbox.add_class_method("Foo::Bar", :babar, lambda{"king of elephants"}) > Sandbox::Foo::Bar.babar => "king of elephants" When you've cluttered your namespace beyond repair, just delete it all > Sandbox.cleanup! # or of course, use the alias Sandbox.scoop! :) == Contributing to cat * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. * Fork the project. * Start a feature/bugfix branch. * Commit and push until you are happy with your contribution. * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. == Copyright Copyright (c) 2012 Chris Doyle. See LICENSE.txt for further details. cat-0.2.1/LICENSE.txt0000644000175100017500000000203712655731157013145 0ustar mmollmmollCopyright (c) 2012 Chris Doyle 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. cat-0.2.1/Gemfile.lock0000644000175100017500000000101112655731157013533 0ustar mmollmmollGEM remote: http://rubygems.org/ specs: diff-lcs (1.1.3) git (1.2.5) jeweler (1.8.3) bundler (~> 1.0) git (>= 1.2.5) rake rdoc json (1.6.6) rake (0.9.2.2) rdoc (3.12) json (~> 1.4) rspec (2.9.0) rspec-core (~> 2.9.0) rspec-expectations (~> 2.9.0) rspec-mocks (~> 2.9.0) rspec-core (2.9.0) rspec-expectations (2.9.1) diff-lcs (~> 1.1.3) rspec-mocks (2.9.0) PLATFORMS ruby DEPENDENCIES bundler jeweler rdoc rspec cat-0.2.1/Gemfile0000644000175100017500000000016312655731157012613 0ustar mmollmmollsource "http://rubygems.org" group :development do gem "rspec" gem "rdoc" gem "bundler" gem "jeweler" end cat-0.2.1/.travis.yml0000644000175100017500000000017212655731157013431 0ustar mmollmmolllanguage: ruby rvm: - 1.8.7 - 1.9.2 - 1.9.3 - jruby-18mode - jruby-19mode - rbx-18mode - rbx-19mode - ree cat-0.2.1/.rvmrc0000644000175100017500000000372612655731157012462 0ustar mmollmmoll#!/usr/bin/env bash # This is an RVM Project .rvmrc file, used to automatically load the ruby # development environment upon cd'ing into the directory # First we specify our desired [@], the @gemset name is optional, # Only full ruby name is supported here, for short names use: # echo "rvm use 1.9.3" > .rvmrc environment_id="ruby-1.9.3-p125@cat" # Uncomment the following lines if you want to verify rvm version per project # rvmrc_rvm_version="1.10.3" # 1.10.1 seams as a safe start # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || { # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading." # return 1 # } # First we attempt to load the desired environment directly from the environment # file. This is very fast and efficient compared to running through the entire # CLI and selector. If you want feedback on which environment was used then # insert the word 'use' after --create as this triggers verbose mode. if [[ -d "${rvm_path:-$HOME/.rvm}/environments" && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] then \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id" [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] && \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true else # If the environment file has not yet been created, use the RVM CLI to select. rvm --create "$environment_id" || { echo "Failed to create RVM environment '${environment_id}'." return 1 } fi # If you use bundler, this might be useful to you: # if [[ -s Gemfile ]] && { # ! builtin command -v bundle >/dev/null || # builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null # } # then # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n" # gem install bundler # fi # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null # then # bundle install | grep -vE '^Using|Your bundle is complete' # fi cat-0.2.1/.document0000644000175100017500000000006712655731157013142 0ustar mmollmmolllib/**/*.rb bin/* - features/**/*.feature LICENSE.txt