attr-required-1.0.0/ 0000755 0001750 0001750 00000000000 12526154677 015605 5 ustar balasankarc balasankarc attr-required-1.0.0/Gemfile 0000644 0001750 0001750 00000000045 12526154677 017077 0 ustar balasankarc balasankarc source 'http://rubygems.org'
gemspec
attr-required-1.0.0/.gitignore 0000644 0001750 0001750 00000000240 12526154677 017571 0 ustar balasankarc balasankarc ## MAC OS
.DS_Store
## TEXTMATE
*.tmproj
tmtags
## EMACS
*~
\#*
.\#*
## VIM
*.swp
## PROJECT::GENERAL
coverage*
rdoc
pkg
Gemfile.lock
## PROJECT::SPECIFIC
attr-required-1.0.0/Rakefile 0000644 0001750 0001750 00000000623 12526154677 017253 0 ustar balasankarc balasankarc require 'bundler'
Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
namespace :coverage do
desc "Open coverage report"
task :report do
require 'simplecov'
`open "#{File.join SimpleCov.coverage_path, 'index.html' }"`
end
end
task :spec do
Rake::Task[:'coverage:report'].invoke unless ENV['TRAVIS_RUBY_VERSION']
end
task :default => :spec attr-required-1.0.0/metadata.yml 0000644 0001750 0001750 00000004463 12526154677 020117 0 ustar balasankarc balasankarc --- !ruby/object:Gem::Specification
name: attr_required
version: !ruby/object:Gem::Version
version: 1.0.0
platform: ruby
authors:
- nov matake
autorequire:
bindir: bin
cert_chain: []
date: 2014-01-29 00:00:00.000000000 Z
dependencies:
- !ruby/object:Gem::Dependency
name: rake
requirement: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0.8'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0.8'
- !ruby/object:Gem::Dependency
name: simplecov
requirement: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
- !ruby/object:Gem::Dependency
name: rspec
requirement: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '2'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '2'
description: attr_required and attr_optional
email: nov@matake.jp
executables: []
extensions: []
extra_rdoc_files:
- LICENSE
- README.rdoc
files:
- .gitignore
- .rspec
- .travis.yml
- Gemfile
- LICENSE
- README.rdoc
- Rakefile
- VERSION
- attr_required.gemspec
- lib/attr_optional.rb
- lib/attr_required.rb
- spec/attr_optional_spec.rb
- spec/attr_required_spec.rb
- spec/spec_helper.rb
homepage: http://github.com/nov/attr_required
licenses:
- MIT
metadata: {}
post_install_message:
rdoc_options:
- --charset=UTF-8
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
required_rubygems_version: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: 1.3.6
requirements: []
rubyforge_project:
rubygems_version: 2.0.3
signing_key:
specification_version: 4
summary: attr_required and attr_optional
test_files:
- spec/attr_optional_spec.rb
- spec/attr_required_spec.rb
- spec/spec_helper.rb
attr-required-1.0.0/VERSION 0000644 0001750 0001750 00000000005 12526154677 016650 0 ustar balasankarc balasankarc 1.0.0 attr-required-1.0.0/lib/ 0000755 0001750 0001750 00000000000 12526154677 016353 5 ustar balasankarc balasankarc attr-required-1.0.0/lib/attr_optional.rb 0000644 0001750 0001750 00000001676 12526154677 021571 0 ustar balasankarc balasankarc module AttrOptional
def self.included(klass)
klass.send :extend, ClassMethods
end
module ClassMethods
def inherited(klass)
super
unless optional_attributes.empty?
klass.attr_optional *optional_attributes
end
end
def attr_optional(*keys)
if defined? undef_required_attributes
undef_required_attributes *keys
end
optional_attributes.concat(keys)
attr_accessor *keys
end
def attr_optional?(key)
optional_attributes.include?(key)
end
def optional_attributes
@optional_attributes ||= []
end
def undef_optional_attributes(*keys)
keys.each do |key|
if attr_optional?(key)
undef_method key, :"#{key}="
optional_attributes.delete key
end
end
end
end
def optional_attributes
self.class.optional_attributes
end
def attr_optional?(key)
self.class.attr_optional? key
end
end attr-required-1.0.0/lib/attr_required.rb 0000644 0001750 0001750 00000002537 12526154677 021561 0 ustar balasankarc balasankarc module AttrRequired
class AttrMissing < StandardError; end
def self.included(klass)
klass.send :extend, ClassMethods
end
module ClassMethods
def inherited(klass)
super
unless required_attributes.empty?
klass.attr_required *required_attributes
end
end
def attr_required(*keys)
if defined? undef_optional_attributes
undef_optional_attributes *keys
end
required_attributes.concat keys
attr_accessor *keys
end
def attr_required?(key)
required_attributes.include? key
end
def required_attributes
@required_attributes ||= []
end
def undef_required_attributes(*keys)
keys.each do |key|
if attr_required?(key)
undef_method key, :"#{key}="
required_attributes.delete key
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 attr-required-1.0.0/LICENSE 0000644 0001750 0001750 00000002036 12526154677 016613 0 ustar balasankarc balasankarc 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.
attr-required-1.0.0/.rspec 0000644 0001750 0001750 00000000037 12526154677 016722 0 ustar balasankarc balasankarc --color
--format=documentation
attr-required-1.0.0/spec/ 0000755 0001750 0001750 00000000000 12526154677 016537 5 ustar balasankarc balasankarc attr-required-1.0.0/spec/attr_optional_spec.rb 0000644 0001750 0001750 00000004521 12526154677 022757 0 ustar balasankarc balasankarc require 'spec_helper.rb'
describe AttrOptional do
before do
@a, @b, @c = A.new, B.new, C.new
end
describe '.attr_optional' do
it 'should define accessible attributes' do
@a.should respond_to :attr_optional_a
@a.should respond_to :attr_optional_a=
@b.should respond_to :attr_optional_b
@b.should respond_to :attr_optional_b=
end
it 'should be inherited' do
@b.should respond_to :attr_optional_a
@b.should respond_to :attr_optional_a=
end
context 'when already required' do
it 'should be optional' do
@c.attr_required?(:attr_required_b).should be_false
@c.attr_optional?(:attr_required_b).should be_true
end
end
context 'when AttrRequired not included' do
it 'should do nothing' do
OnlyOptional.optional_attributes.should == [:only_optional]
end
end
end
describe '.attr_optional?' do
it 'should answer whether the attributes is optional or not' do
A.attr_optional?(:attr_optional_a).should be_true
B.attr_optional?(:attr_optional_a).should be_true
B.attr_optional?(:attr_optional_b).should be_true
B.attr_optional?(:to_s).should be_false
end
end
describe '#attr_optional?' do
it 'should answer whether the attributes is optional or not' do
@a.attr_optional?(:attr_optional_a).should be_true
@b.attr_optional?(:attr_optional_a).should be_true
@b.attr_optional?(:attr_optional_b).should be_true
@b.attr_optional?(:to_s).should be_false
end
end
describe '.optional_attributes' do
it 'should return all optional attributes keys' do
A.optional_attributes.should == [:attr_optional_a]
B.optional_attributes.should == [:attr_optional_a, :attr_optional_b]
end
end
describe '#optional_attributes' do
it 'should return optional attributes keys' do
@a.optional_attributes.should == [:attr_optional_a]
@b.optional_attributes.should == [:attr_optional_a, :attr_optional_b]
end
end
describe '.undef_optional_attributes' do
it 'should undefine accessors and remove from optional attributes' do
C.optional_attributes.should_not include :attr_optional_a
@c.optional_attributes.should_not include :attr_optional_a
@c.should_not respond_to :attr_optional_a
@c.should_not respond_to :attr_optional_a=
end
end
end
attr-required-1.0.0/spec/spec_helper.rb 0000644 0001750 0001750 00000001220 12526154677 021350 0 ustar balasankarc balasankarc require 'simplecov'
SimpleCov.start do
add_filter 'spec'
end
require 'attr_required'
require 'attr_optional'
require 'rspec'
class A
include AttrRequired, AttrOptional
attr_required :attr_required_a
attr_optional :attr_optional_a
end
class B < A
attr_required :attr_required_b
attr_optional :attr_optional_b
end
class C < B
undef_required_attributes :attr_required_a
undef_optional_attributes :attr_optional_a
attr_optional :attr_required_b
attr_required :attr_optional_b
end
class OnlyRequired
include AttrRequired
attr_required :only_required
end
class OnlyOptional
include AttrOptional
attr_optional :only_optional
end attr-required-1.0.0/spec/attr_required_spec.rb 0000644 0001750 0001750 00000007446 12526154677 022763 0 ustar balasankarc balasankarc require 'spec_helper.rb'
describe AttrRequired do
before do
@a, @b, @c = A.new, B.new, C.new
end
describe '.attr_required' do
it 'should define accessible attributes' do
@a.should respond_to :attr_required_a
@a.should respond_to :attr_required_a=
@b.should respond_to :attr_required_b
@b.should respond_to :attr_required_b=
end
it 'should be inherited' do
@b.should respond_to :attr_required_a
@b.should respond_to :attr_required_a=
end
context 'when already optional' do
it 'should be optional' do
@c.attr_required?(:attr_optional_b).should be_true
@c.attr_optional?(:attr_optional_b).should be_false
end
end
context 'when AttrOptional not included' do
it 'should do nothing' do
OnlyRequired.required_attributes.should == [:only_required]
end
end
end
describe '.attr_required?' do
it 'should answer whether the attributes is required or not' do
A.attr_required?(:attr_required_a).should be_true
B.attr_required?(:attr_required_a).should be_true
B.attr_required?(:attr_required_b).should be_true
B.attr_required?(:to_s).should be_false
end
end
describe '#attr_required?' do
it 'should answer whether the attributes is required or not' do
@a.attr_required?(:attr_required_a).should be_true
@b.attr_required?(:attr_required_a).should be_true
@b.attr_required?(:attr_required_b).should be_true
@a.attr_required?(:attr_required_b).should be_false
@b.attr_required?(:to_s).should be_false
end
end
describe '#attr_missing?' do
it 'should answer whether any attributes are missing' do
@a.attr_missing?.should be_true
@b.attr_missing?.should be_true
@a.attr_required_a = 'attr_required_a'
@b.attr_required_a = 'attr_required_a'
@a.attr_missing?.should be_false
@b.attr_missing?.should be_true
@b.attr_required_b = 'attr_required_b'
@b.attr_missing?.should be_false
end
end
describe '#attr_missing!' do
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_required_a = 'attr_required_a'
@b.attr_required_a = 'attr_required_a'
lambda { @a.attr_missing! }.should_not raise_error
lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
@b.attr_required_b = 'attr_required_b'
lambda { @b.attr_missing! }.should_not raise_error
end
end
describe '#attr_missing' do
it 'should return missing attributes keys' do
@a.attr_missing.should == [:attr_required_a]
@b.attr_missing.should == [:attr_required_a, :attr_required_b]
@a.attr_required_a = 'attr_required_a'
@b.attr_required_a = 'attr_required_a'
@a.attr_missing.should == []
@b.attr_missing.should == [:attr_required_b]
end
end
describe '.required_attributes' do
it 'should return all required attributes keys' do
A.required_attributes.should == [:attr_required_a]
B.required_attributes.should == [:attr_required_a, :attr_required_b]
end
end
describe '#required_attributes' do
it 'should return required attributes keys' do
@a.required_attributes.should == [:attr_required_a]
@b.required_attributes.should == [:attr_required_a, :attr_required_b]
end
end
describe '.undef_required_attributes' do
it 'should undefine accessors and remove from required attributes' do
C.required_attributes.should_not include :attr_required_a
@c.required_attributes.should_not include :attr_required_a
@c.should_not respond_to :attr_required_a
@c.should_not respond_to :attr_required_a=
end
end
end
attr-required-1.0.0/attr_required.gemspec 0000644 0001750 0001750 00000001566 12526154677 022034 0 ustar balasankarc balasankarc 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{attr_required and attr_optional}
s.summary = %q{attr_required and attr_optional}
s.email = "nov@matake.jp"
s.extra_rdoc_files = ["LICENSE", "README.rdoc"]
s.license = 'MIT'
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 "simplecov"
s.add_development_dependency "rspec", ">= 2"
end
attr-required-1.0.0/.travis.yml 0000644 0001750 0001750 00000000031 12526154677 017710 0 ustar balasankarc balasankarc rvm:
- 1.9.3
- 2.0.0
attr-required-1.0.0/README.rdoc 0000644 0001750 0001750 00000004473 12526154677 017423 0 ustar balasankarc balasankarc = attr_required
This gem provide attr_required
and attr_optional
like attr_accessor
.
{
}[http://travis-ci.org/nov/attr_required]
REQUIRED and OPTIONAL are common terminology in RFCs, and used for protocol parameters.
This gem helps RFC library developers to define which parameters (attributes in Ruby world) are REQUIRED and which are OPTIONAL.
It might be also helpful for other developers.
I've developed this gem to use for rack-oauth2, a Rack-based OAuth 2.0 library.
http://github.com/nov/rack-oauth2
== Installation
gem install attr_required
== Usage
# Attributes Definitions
require 'attr_required'
require 'attr_optional'
class A
include AttrRequired, AttrOptional
attr_required :required_a
attr_optional :optional_a
end
class B < A
attr_required :required_b
attr_optional :optional_b
end
# Class Methods
A.required_attributes #=> [:required_a]
B.required_attributes #=> [:required_a, :required_b]
A.optional_attributes #=> [:optional_a]
B.optional_attributes #=> [:optional_a, :optional_b]
A.attr_required?(:required_a) #=> true
B.attr_optional?(:optional_b) #=> true
# Instance Methods
@a = A.new
@b = B.new
@a.required_attributes #=> [:required_a]
@b.required_attributes #=> [:required_a, :required_b]
@a.optional_attributes #=> [:optional_a]
@b.optional_attributes #=> [:optional_a, :optional_b]
@a.attr_required?(:required_a) #=> true
@a.attr_optional?(:optiona_a) #=> true
@a.attr_missing? #=> true
@a.attr_missing #=> [:required_a]
@a.attr_missing! #=> raise AttrRequired::AttrMissing
@a.required_a = "foo"
@a.attr_missing? #=> false
@a.attr_missing #=> []
@a.attr_missing! #=> do nothing
Check spec/attr_(required|optional).rb for more details.
== 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.