Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first cut

  • Loading branch information...
commit 81ec75299345803fe557a4cd0f6c27c43cf0deaf 0 parents
@nov authored
21 .gitignore
@@ -0,0 +1,21 @@
+## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+## PROJECT::SPECIFIC
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format=documentation
1  Gemfile
@@ -0,0 +1 @@
+gemspec
27 Gemfile.lock
@@ -0,0 +1,27 @@
+PATH
+ remote: .
+ specs:
+ attr_required (0.0.1)
+
+GEM
+ specs:
+ diff-lcs (1.1.2)
+ rake (0.8.7)
+ rcov (0.9.9)
+ rspec (2.1.0)
+ rspec-core (~> 2.1.0)
+ rspec-expectations (~> 2.1.0)
+ rspec-mocks (~> 2.1.0)
+ rspec-core (2.1.0)
+ rspec-expectations (2.1.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.1.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ attr_required!
+ rake (>= 0.8)
+ rcov (>= 0.9)
+ rspec (>= 2)
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 nov matake
+
+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.
25 README.rdoc
@@ -0,0 +1,25 @@
+= attr_required
+
+Provide attr_required with presence validation
+
+== Installation
+
+ gem install attr_required
+
+== Resources
+
+== Usage
+
+== Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 2010 nov matake. See LICENSE for details.
20 Rakefile
@@ -0,0 +1,20 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec)
+
+RSpec::Core::RakeTask.new(:rcov) do |spec|
+ spec.rcov = true
+ spec.rcov_opts = ['-Ilib -Ispec --exclude spec,gems']
+end
+
+task :default => :spec
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "attr_required #{File.read('VERSION')}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1  VERSION
@@ -0,0 +1 @@
+0.0.1
19 attr_required.gemspec
@@ -0,0 +1,19 @@
+Gem::Specification.new do |s|
+ s.name = "attr_required"
+ s.version = File.read("VERSION")
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
+ s.authors = ["nov matake"]
+ s.description = %q{Provide attr_required with presence validation}
+ s.summary = %q{Provide attr_required with presence validation}
+ s.email = "nov@matake.jp"
+ s.extra_rdoc_files = ["LICENSE", "README.rdoc"]
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.homepage = "http://github.com/nov/attr_required"
+ s.require_paths = ["lib"]
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.add_development_dependency "rake", ">= 0.8"
+ s.add_development_dependency "rcov", ">= 0.9"
+ s.add_development_dependency "rspec", ">= 2"
+end
59 lib/attr_required.rb
@@ -0,0 +1,59 @@
+module AttrRequired
+ class AttrMissing < StandardError; end
+
+ def self.included(klass)
+ class << klass
+
+ def attr_required(*keys)
+ @required_attributes ||= []
+ @required_attributes += Array(keys)
+ attr_accessor *keys
+ end
+
+ def attr_required?(key)
+ required_attributes.include?(key)
+ end
+
+ def required_attributes
+ Array(@required_attributes)
+ end
+
+ def inherited(subclass)
+ unless required_attributes.empty?
+ subclass.attr_required *required_attributes
+ end
+ end
+
+ end
+ end
+
+ def required_attributes
+ self.class.required_attributes
+ end
+
+ def attr_required?(key)
+ self.class.attr_required? key
+ end
+
+ def attr_missing?
+ !attr_missing.empty?
+ end
+
+ def attr_missing!
+ if attr_missing?
+ raise AttrMissing.new("'#{attr_missing.join('\', \'')}' required.")
+ end
+ end
+
+ def attr_missing
+ required_attributes.select do |key|
+ value = send(key)
+ if value.respond_to?(:empty?)
+ value.empty?
+ else
+ value.nil?
+ end
+ end
+ end
+
+end
117 spec/attr_required_spec.rb
@@ -0,0 +1,117 @@
+require 'spec_helper.rb'
+
+class A
+ include AttrRequired
+ attr_required :attr_a
+end
+
+class B < A
+ attr_required :attr_b
+end
+
+describe AttrRequired, '.attr_required' do
+ before do
+ @a, @b = A.new, B.new
+ end
+
+ it 'should define accessible attributes' do
+ @a.should respond_to(:attr_a)
+ @a.should respond_to(:attr_a=)
+ @b.should respond_to(:attr_b)
+ @b.should respond_to(:attr_b=)
+ end
+
+ it 'should be inherited' do
+ @b.should respond_to(:attr_a)
+ @b.should respond_to(:attr_a=)
+ end
+end
+
+describe AttrRequired, '.attr_required?' do
+ it 'should answer whether the attributes is required or not' do
+ A.attr_required?(:attr_a).should be_true
+ B.attr_required?(:attr_a).should be_true
+ B.attr_required?(:attr_b).should be_true
+ B.attr_required?(:to_s).should be_false
+ end
+end
+
+describe AttrRequired, '#attr_required?' do
+ before do
+ @a, @b = A.new, B.new
+ end
+
+ it 'should answer whether the attributes is required or not' do
+ @a.attr_required?(:attr_a).should be_true
+ @b.attr_required?(:attr_a).should be_true
+ @b.attr_required?(:attr_b).should be_true
+ @b.attr_required?(:to_s).should be_false
+ end
+end
+
+describe AttrRequired, '#attr_missing?' do
+ before do
+ @a, @b = A.new, B.new
+ end
+
+ it 'should answer whether any attributes are missing' do
+ @a.attr_missing?.should be_true
+ @b.attr_missing?.should be_true
+ @a.attr_a = 'attr_a'
+ @b.attr_a = 'attr_a'
+ @a.attr_missing?.should be_false
+ @b.attr_missing?.should be_true
+ @b.attr_b = 'attr_b'
+ @b.attr_missing?.should be_false
+ end
+end
+
+describe AttrRequired, '#attr_missing!' do
+ before do
+ @a, @b = A.new, B.new
+ end
+
+ it 'should raise AttrMissing error when any attributes are missing' do
+ lambda { @a.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
+ lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
+ @a.attr_a = 'attr_a'
+ @b.attr_a = 'attr_a'
+ lambda { @a.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
+ lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
+ @b.attr_b = 'attr_b'
+ lambda { @b.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
+ end
+end
+
+describe AttrRequired, '.required_attributes' do
+ it 'should return all required attributes keys' do
+ A.required_attributes.should == [:attr_a]
+ B.required_attributes.should == [:attr_a, :attr_b]
+ end
+end
+
+describe AttrRequired, '#required_attributes' do
+ before do
+ @a, @b = A.new, B.new
+ end
+
+ it 'should return required attributes keys' do
+ @a.required_attributes.should == [:attr_a]
+ @b.required_attributes.should == [:attr_a, :attr_b]
+ end
+end
+
+describe AttrRequired, '#attr_missing' do
+ before do
+ @a, @b = A.new, B.new
+ end
+
+ it 'should return missing attributes keys' do
+ @a.attr_missing.should == [:attr_a]
+ @b.attr_missing.should == [:attr_a, :attr_b]
+ @a.attr_a = 'attr_a'
+ @b.attr_a = 'attr_a'
+ @a.attr_missing.should == []
+ @b.attr_missing.should == [:attr_b]
+ end
+end
5 spec/spec_helper.rb
@@ -0,0 +1,5 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+require 'attr_required'
+require 'rspec'
Please sign in to comment.
Something went wrong with that request. Please try again.