Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 4893495ddf3a76ab009eea433cbb6e1674c0bcb9 0 parents
@markbates 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
7 Gemfile
@@ -0,0 +1,7 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in mongoid_delorean.gemspec
+gemspec
+
+gem "rspec"
+gem 'database_cleaner'
22 LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Mark Bates
+
+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 @@
+# Mongoid::Tags::Arent::Hard
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'mongoid-tags-arent-hard'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install mongoid-tags-arent-hard
+
+## 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
9 Rakefile
@@ -0,0 +1,9 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+desc "Run tests"
+task :default => [:ruby]
+
+task :ruby do
+ system "bundle exec rspec"
+end
18 lib/mongoid-tags-arent-hard.rb
@@ -0,0 +1,18 @@
+require 'delegate'
+require 'mongoid'
+require "mongoid/tags_arent_hard/version"
+require "mongoid/tags_arent_hard/configuration"
+require "mongoid/tags_arent_hard/tags_arent_hard"
+require "mongoid/tags_arent_hard/tags"
+
+module Mongoid
+ module TagsArentHard
+
+ def self.config(&block)
+ @config ||= Configuration.new
+ yield @config if block_given?
+ return @config
+ end
+
+ end
+end
12 lib/mongoid/tags_arent_hard/configuration.rb
@@ -0,0 +1,12 @@
+module Mongoid
+ module TagsArentHard
+ class Configuration
+ attr_accessor :separator
+
+ def initialize
+ self.separator = ","
+ end
+
+ end
+ end
+end
28 lib/mongoid/tags_arent_hard/tags.rb
@@ -0,0 +1,28 @@
+module Mongoid
+ module TagsArentHard
+ class Tags
+
+ attr_accessor :tag_list
+ attr_accessor :options
+ delegate :<<, :join, :map, :each, :inspect, :==, :===, :eql?, :__bson_dump__, to: :tag_list
+
+ def initialize(*tag_list, options)
+ self.options = {separator: Mongoid::TagsArentHard.config.separator}.merge(options)
+ self.tag_list = self.normalize(*tag_list)
+ end
+
+ def normalize(*tag_list)
+ tag_list.flatten.map {|s| s.split(self.options[:separator]).map {|x| x.strip}}.flatten
+ end
+
+ def to_s
+ self.join(self.options[:separator])
+ end
+
+ def to_str
+ self.to_s
+ end
+
+ end
+ end
+end
46 lib/mongoid/tags_arent_hard/tags_arent_hard.rb
@@ -0,0 +1,46 @@
+module Mongoid
+ module TagsArentHard
+
+ def self.included(klass)
+ klass.extend(ClassMethods)
+ end
+
+ module ClassMethods
+
+ def taggable_with(name, options = {})
+ options = {separator: Mongoid::TagsArentHard.config.separator}.merge(options)
+ self.field(name, type: Array, default: [])
+ self.class_eval do
+ define_method(name) do
+ val = super()
+ unless val.is_a?(Mongoid::TagsArentHard::Tags)
+ val = Mongoid::TagsArentHard::Tags.new(val, options)
+ self.send("#{name}=", val)
+ end
+ return val
+ end
+ define_method("#{name}=") do |val|
+ unless val.is_a?(Mongoid::TagsArentHard::Tags)
+ val = Mongoid::TagsArentHard::Tags.new(val, options)
+ end
+ super(val)
+ end
+
+ end
+ self.class.send(:define_method, "with_#{name}") do |*val|
+ self.send("with_any_#{name}", *val)
+ end
+
+ self.class.send(:define_method, "with_any_#{name}") do |*val|
+ any_in(name => Mongoid::TagsArentHard::Tags.new(*val, {}).tag_list)
+ end
+
+ self.class.send(:define_method, "with_all_#{name}") do |*val|
+ all_in(name => Mongoid::TagsArentHard::Tags.new(*val, {}).tag_list)
+ end
+ end
+
+ end
+
+ end
+end
9 lib/mongoid/tags_arent_hard/version.rb
@@ -0,0 +1,9 @@
+module Mongoid
+ module Tags
+ module Arent
+ module Hard
+ VERSION = "0.0.1"
+ end
+ end
+ end
+end
21 mongoid-tags-arent-hard.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'mongoid/tags_arent_hard/version'
+
+Gem::Specification.new do |gem|
+ gem.name = "mongoid-tags-arent-hard"
+ gem.version = Mongoid::Tags::Arent::Hard::VERSION
+ gem.authors = ["Mark Bates"]
+ gem.email = ["mark@markbates.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_dependency("mongoid", ">=3.0.0")
+end
6 spec/config.yml
@@ -0,0 +1,6 @@
+test:
+ sessions:
+ default:
+ database: mongoid_tags_arent_hard_test
+ hosts:
+ - localhost:27017
23 spec/spec_helper.rb
@@ -0,0 +1,23 @@
+require 'bundler/setup'
+
+require 'mongoid-tags-arent-hard' # and any other gems you need
+
+require 'database_cleaner'
+
+Mongoid.load!(File.join(File.dirname(__FILE__), "config.yml"), :test)
+# require File.join(File.dirname(__FILE__), "support", "models")
+
+DatabaseCleaner[:mongoid].strategy = :truncation
+
+RSpec.configure do |config|
+
+ config.before(:each) do
+ DatabaseCleaner.clean
+ Mongoid::TagsArentHard.config.separator = ","
+ end
+
+ config.after(:each) do
+ Mongoid::TagsArentHard.config.separator = ","
+ end
+
+end
190 spec/tags_arent_hard/tags_arent_hard_spec.rb
@@ -0,0 +1,190 @@
+require 'spec_helper'
+
+class Foo
+ include Mongoid::Document
+ include Mongoid::TagsArentHard
+
+ taggable_with :tags
+ taggable_with :colors, separator: ";"
+end
+
+describe Mongoid::TagsArentHard do
+
+ let(:foo) { Foo.new }
+
+ {tags: ",", colors: ";"}.each do |_name, _separator|
+
+ describe ".taggable_with" do
+
+ it "defines a getter for '#{_name}'" do
+ foo.send(_name).should be_kind_of(Mongoid::TagsArentHard::Tags)
+ foo.send(_name).should eql([])
+ end
+
+ it "defines a setter for '#{_name}'" do
+ foo.send("#{_name}=", "foo #{_separator} bar")
+ foo.send(_name).should eql(["foo","bar"])
+ end
+
+ end
+
+ describe '#save' do
+
+ it "saves the #{_name} correctly" do
+ foo.send("#{_name}=", "foo#{_separator}bar")
+ foo.save!
+ foo.reload
+ foo.send(_name).should eql(["foo","bar"])
+ end
+
+ end
+
+ context 'class scopes' do
+
+ before(:each) do
+ @foo1 = Foo.create!(_name => "a#{_separator}b#{_separator}c")
+ @foo2 = Foo.create!(_name => "b#{_separator}c#{_separator}f")
+ @foo3 = Foo.create!(_name => "d#{_separator}e#{_separator}f")
+ end
+
+ describe "with_#{_name}" do
+
+ it "returns all models with a specific #{_name} (splatted)" do
+ results = Foo.send("with_#{_name}", "a")
+ results.should have(1).foo
+ results.should include(@foo1)
+
+ results = Foo.send("with_#{_name}", "b")
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo2)
+ end
+
+ it "returns all models with a specific #{_name} (arrayed)" do
+ results = Foo.send("with_#{_name}", ["a"])
+ results.should have(1).foo
+ results.should include(@foo1)
+
+ results = Foo.send("with_#{_name}", ["b"])
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo2)
+ end
+
+ end
+
+ describe "with_any_#{_name}" do
+
+ it "returns all models with any #{_name} (splatted)" do
+ results = Foo.send("with_any_#{_name}", "a")
+ results.should have(1).foo
+ results.should include(@foo1)
+
+ results = Foo.send("with_any_#{_name}", "b")
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo2)
+
+ results = Foo.send("with_any_#{_name}", "a", "e")
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo3)
+ end
+
+ it "returns all models with any #{_name} (arrayed)" do
+ results = Foo.send("with_any_#{_name}", ["a"])
+ results.should have(1).foo
+ results.should include(@foo1)
+
+ results = Foo.send("with_any_#{_name}", ["b"])
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo2)
+
+ results = Foo.send("with_any_#{_name}", ["a", "e"])
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo3)
+ end
+
+ it "returns all models with any #{_name} (string)" do
+ results = Foo.send("with_any_#{_name}", "a")
+ results.should have(1).foo
+ results.should include(@foo1)
+
+ results = Foo.send("with_any_#{_name}", "b")
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo2)
+
+ results = Foo.send("with_any_#{_name}", "a,e")
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo3)
+ end
+
+ end
+
+ describe "with_all_#{_name}" do
+
+ it "returns all models with all #{_name} (splatted)" do
+ results = Foo.send("with_all_#{_name}", "a")
+ results.should have(1).foo
+ results.should include(@foo1)
+
+ results = Foo.send("with_all_#{_name}", "b")
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo2)
+
+ results = Foo.send("with_all_#{_name}", "a", "e")
+ results.should have(0).foos
+
+ results = Foo.send("with_all_#{_name}", "b", "f")
+ results.should have(1).foo
+ results.should include(@foo2)
+ end
+
+ it "returns all models with all #{_name} (arrayed)" do
+ results = Foo.send("with_all_#{_name}", ["a"])
+ results.should have(1).foo
+ results.should include(@foo1)
+
+ results = Foo.send("with_all_#{_name}", ["b"])
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo2)
+
+ results = Foo.send("with_all_#{_name}", ["a", "e"])
+ results.should have(0).foos
+
+ results = Foo.send("with_all_#{_name}", ["b", "f"])
+ results.should have(1).foo
+ results.should include(@foo2)
+ end
+
+ it "returns all models with all #{_name} (string)" do
+ results = Foo.send("with_all_#{_name}", "a")
+ results.should have(1).foo
+ results.should include(@foo1)
+
+ results = Foo.send("with_all_#{_name}", "b")
+ results.should have(2).foos
+ results.should include(@foo1)
+ results.should include(@foo2)
+
+ results = Foo.send("with_all_#{_name}", "a,e")
+ results.should have(0).foos
+
+ results = Foo.send("with_all_#{_name}", "b,f")
+ results.should have(1).foo
+ results.should include(@foo2)
+ end
+
+ end
+
+ end
+
+ end
+
+end
44 spec/tags_arent_hard/tags_spec.rb
@@ -0,0 +1,44 @@
+require "spec_helper"
+
+describe Mongoid::TagsArentHard::Tags do
+
+ let(:tags) { Mongoid::TagsArentHard::Tags.new(%w{foo bar baz}, {}) }
+
+ after(:each) do
+ Mongoid::TagsArentHard.config.separator = ","
+ end
+
+ describe 'initialize' do
+
+ it "takes an array of tags" do
+ tags = Mongoid::TagsArentHard::Tags.new(["foo", "bar"], {})
+ tags.tag_list.should eql(["foo", "bar"])
+ end
+
+ it "takes a splatted list" do
+ tags = Mongoid::TagsArentHard::Tags.new("foo", "bar", {})
+ tags.tag_list.should eql(["foo", "bar"])
+ end
+
+ it "takes a string" do
+ tags = Mongoid::TagsArentHard::Tags.new("foo,bar", {})
+ tags.tag_list.should eql(["foo", "bar"])
+ end
+
+ it "takes a string with a different separator" do
+ tags = Mongoid::TagsArentHard::Tags.new("foo bar", separator: " ")
+ tags.tag_list.should eql(["foo", "bar"])
+ end
+
+ end
+
+ describe 'to_s, to_str' do
+
+ it "returns a comma separated list of tags" do
+ tags.to_s.should eql("foo,bar,baz")
+ tags.to_str.should eql("foo,bar,baz")
+ end
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.