├── .gitignore ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib ├── reserved_word.rb └── reserved_word │ ├── version.rb │ ├── word.rb │ └── word │ └── site_slug.rb ├── reserved_word.gemspec └── spec └── reserved_word_spec.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in reserved_word.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 rono23 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ReservedWord 2 | 3 | Make your reserved words on Rails. 4 | 5 | For example, when you make a nickname is used example.com/nickname, you may validate some nicknames (e.g. about, faq etc). ReservedWord reserves a kind of words. 6 | 7 | ## Installation 8 | 9 | Add this line to your application's Gemfile: 10 | 11 | gem 'reserved_word' 12 | 13 | And then execute: 14 | 15 | $ bundle 16 | 17 | Or install it yourself as: 18 | 19 | $ gem install reserved_word 20 | 21 | ## Usage 22 | 23 | ``` 24 | # app/model/user.rb 25 | before_validation { nickname.downcase! } # disable case sensitive if you want 26 | validates :nickname, exclusion: { in: ReservedWord.list } 27 | 28 | # Setup your reserved words on config/initializers/reserved_word.rb 29 | ReservedWord.your_words = %w(foo bar) 30 | 31 | # then use anywhere 32 | ReservedWord.your_words.include?("foo") #=> true 33 | 34 | # model 35 | validates :nickname, exclusion: { in: ReservedWord.your_words } 36 | ``` 37 | 38 | ## Contributing 39 | 40 | 1. Fork it 41 | 2. Create your feature branch (`git checkout -b my-new-feature`) 42 | 3. Commit your changes (`git commit -am 'Add some feature'`) 43 | 4. Push to the branch (`git push origin my-new-feature`) 44 | 5. Create new Pull Request 45 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /lib/reserved_word.rb: -------------------------------------------------------------------------------- 1 | require "active_support/all" 2 | require "reserved_word/version" 3 | require "reserved_word/word" 4 | 5 | module ReservedWord 6 | @words = ActiveSupport::OrderedOptions.new 7 | @words.default_words = ReservedWord::Word::SITE_SLUG 8 | 9 | def self.include?(word) 10 | @words.default_words.include?(word) 11 | end 12 | 13 | def self.list 14 | @words.default_words 15 | end 16 | 17 | def self.clear 18 | @words.default_words = Array.new 19 | end 20 | 21 | def self.clear_all 22 | @words = ActiveSupport::OrderedOptions.new 23 | self.clear 24 | end 25 | 26 | def self.word_list 27 | ReservedWord::Word.constants.map(&:downcase) 28 | end 29 | 30 | def self.all 31 | @words.to_hash 32 | end 33 | 34 | def self.load!(type, key=nil) 35 | if key == nil 36 | @words.default_words = load_words(type) 37 | else 38 | @words[key] = load_words(type) 39 | end 40 | end 41 | 42 | def self.load_words(type) 43 | ReservedWord::Word.const_get(type.upcase) 44 | end 45 | 46 | def self.method_missing(sym, *args, &block) 47 | sym = $1.to_sym if sym.to_s =~ /(.*)=$/ 48 | super if ReservedWord.singleton_methods.include? sym 49 | 50 | if @words[sym] == nil 51 | if args.first.class == Array 52 | @words[sym] = args.first 53 | else 54 | super 55 | end 56 | else 57 | @words[sym] 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /lib/reserved_word/version.rb: -------------------------------------------------------------------------------- 1 | module ReservedWord 2 | VERSION = "0.0.2" 3 | end 4 | -------------------------------------------------------------------------------- /lib/reserved_word/word.rb: -------------------------------------------------------------------------------- 1 | module ReservedWord 2 | module Word 3 | end 4 | end 5 | 6 | Dir[File.dirname(__FILE__) + "/word/*.rb"].each { |file| require file } 7 | -------------------------------------------------------------------------------- /lib/reserved_word/word/site_slug.rb: -------------------------------------------------------------------------------- 1 | ReservedWord::Word::SITE_SLUG = %w( 2 | about 3 | access 4 | admin 5 | android 6 | api 7 | apps 8 | atom 9 | blog 10 | career 11 | categories 12 | category 13 | clients 14 | communities 15 | community 16 | company 17 | contact 18 | contact-us 19 | contact_us 20 | contactus 21 | dashboard 22 | download 23 | edu 24 | education 25 | enterprise 26 | faq 27 | feature 28 | features 29 | feed 30 | feedback 31 | founder 32 | help 33 | home 34 | info 35 | information 36 | inquiry 37 | iphone 38 | job 39 | jobs 40 | language 41 | languages 42 | legal 43 | license 44 | link 45 | links 46 | linux 47 | log-in 48 | log-out 49 | log_in 50 | log_out 51 | login 52 | logout 53 | mac 54 | member 55 | members 56 | mobile 57 | news 58 | overview 59 | page 60 | pages 61 | payment 62 | plan 63 | policy 64 | press 65 | price 66 | pricing 67 | privacy 68 | privacy-policy 69 | privacy_policy 70 | privacypolicy 71 | rss 72 | search 73 | service 74 | services 75 | sign-in 76 | sign-up 77 | sign_in 78 | sign_up 79 | signin 80 | signup 81 | sitemap 82 | smartphone 83 | staff 84 | status 85 | support 86 | team 87 | terms 88 | terms-of-service 89 | terms_of_service 90 | termsofservice 91 | top 92 | tutorial 93 | usage 94 | user 95 | users 96 | welcome 97 | windows 98 | work 99 | works 100 | ) 101 | -------------------------------------------------------------------------------- /reserved_word.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'reserved_word/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "reserved_word" 8 | spec.version = ReservedWord::VERSION 9 | spec.authors = ["rono23"] 10 | spec.email = ["rono23@gmail.com"] 11 | spec.description = %q{Make your reserved words on Rails} 12 | spec.summary = spec.description 13 | spec.homepage = "https://github.com/rono23/reserved_word" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files`.split($/) 17 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 18 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 19 | spec.require_paths = ["lib"] 20 | 21 | spec.required_ruby_version = '>= 1.9.3' 22 | 23 | spec.add_development_dependency "bundler", "~> 1.3" 24 | spec.add_development_dependency "rake" 25 | spec.add_development_dependency "rspec" 26 | 27 | spec.add_dependency "rails", ">= 3.2.13" 28 | end 29 | -------------------------------------------------------------------------------- /spec/reserved_word_spec.rb: -------------------------------------------------------------------------------- 1 | require File.join(File.dirname(__FILE__), '../lib/reserved_word') 2 | 3 | describe "ReservedWord" do 4 | before do 5 | ReservedWord.clear_all 6 | ReservedWord.load!(:site_slug) 7 | end 8 | 9 | it "should have default words" do 10 | ReservedWord.default_words.should be_kind_of(Array) 11 | end 12 | 13 | describe ".include?" do 14 | it "should include a word in default words" do 15 | ReservedWord.include?('about').should be_true 16 | end 17 | 18 | it "should not include a word in default words" do 19 | ReservedWord.include?('foo').should be_false 20 | end 21 | end 22 | 23 | describe ".list" do 24 | it "should return default words" do 25 | ReservedWord.list.should eq ReservedWord.default_words 26 | end 27 | end 28 | 29 | describe ".clear" do 30 | it "should clear default words" do 31 | ReservedWord.clear 32 | ReservedWord.list.should be_empty 33 | end 34 | end 35 | 36 | describe ".clear_all" do 37 | it "should clear all words" do 38 | ReservedWord.foo = %w(bar) 39 | ReservedWord.clear_all 40 | ReservedWord.list.should be_empty 41 | lambda { ReservedWord.foo }.should raise_error 42 | end 43 | end 44 | 45 | describe ".word_list" do 46 | it "should return the list of ReservedWord::Word" do 47 | ReservedWord.word_list.should be_kind_of(Array) 48 | ReservedWord.word_list.include?(:site_slug).should be_true 49 | end 50 | end 51 | 52 | describe ".all" do 53 | it "should return all words as hash" do 54 | hash = { default_words: ReservedWord.list } 55 | ReservedWord.all.should eq hash 56 | end 57 | 58 | it "should return all words with original words as hash" do 59 | words = %w(foo bar) 60 | hash = { default_words: ReservedWord.list, test: words } 61 | ReservedWord.test = words 62 | ReservedWord.all.should eq hash 63 | end 64 | end 65 | 66 | describe ".load!" do 67 | before { ReservedWord.clear } 68 | 69 | it "should be loaded one of ReservedWord::Word" do 70 | ReservedWord.load!(:site_slug) 71 | ReservedWord.include?('about').should be_true 72 | end 73 | 74 | it "should be loaded one of ReservedWord::Word as test" do 75 | ReservedWord.load!(:site_slug, :test) 76 | ReservedWord.test.include?('about').should be_true 77 | end 78 | end 79 | 80 | describe ".load_words" do 81 | it "should return words from ReservedWord::Word" do 82 | ReservedWord.load_words(:site_slug).should be_kind_of(Array) 83 | ReservedWord.load_words(:site_slug).include?('about') 84 | end 85 | end 86 | 87 | describe ".method_missing" do 88 | context 'when new word is set up' do 89 | before { ReservedWord.new_word = %w(foo bar) } 90 | 91 | it 'should be accessed' do 92 | ReservedWord.new_word.should be_kind_of(Array) 93 | ReservedWord.new_word.include?("foo").should be_true 94 | ReservedWord.new_word.include?("bar").should be_true 95 | end 96 | end 97 | 98 | context 'when class method is overriden' do 99 | it 'should raise error' do 100 | lambda { ReservedWord.all = %w(foo bar) }.should raise_error 101 | lambda { ReservedWord.list = %w(foo bar) }.should raise_error 102 | end 103 | end 104 | end 105 | end 106 | --------------------------------------------------------------------------------