attr_required-1.0.2/ 0000755 0000041 0000041 00000000000 14572524260 014423 5 ustar www-data www-data attr_required-1.0.2/.gitignore 0000644 0000041 0000041 00000000240 14572524260 016407 0 ustar www-data www-data ## MAC OS
.DS_Store
## TEXTMATE
*.tmproj
tmtags
## EMACS
*~
\#*
.\#*
## VIM
*.swp
## PROJECT::GENERAL
coverage*
rdoc
pkg
Gemfile.lock
## PROJECT::SPECIFIC
attr_required-1.0.2/README.rdoc 0000644 0000041 0000041 00000004320 14572524260 016230 0 ustar www-data www-data = attr_required
This gem provide attr_required and attr_optional like attr_accessor.
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.
https://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.
attr_required-1.0.2/.github/ 0000755 0000041 0000041 00000000000 14572524260 015763 5 ustar www-data www-data attr_required-1.0.2/.github/workflows/ 0000755 0000041 0000041 00000000000 14572524260 020020 5 ustar www-data www-data attr_required-1.0.2/.github/workflows/spec.yml 0000644 0000041 0000041 00000001103 14572524260 021470 0 ustar www-data www-data name: Spec
on:
push:
branches:
- main
pull_request:
permissions:
contents: read
jobs:
spec:
strategy:
matrix:
os: ['ubuntu-20.04', 'ubuntu-22.04']
ruby-version: ['3.1', '3.2', '3.3']
include:
- os: 'ubuntu-20.04'
ruby-version: '3.0'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Run Specs
run: bundle exec rake spec
attr_required-1.0.2/.github/FUNDING.yml 0000644 0000041 0000041 00000000073 14572524260 017600 0 ustar www-data www-data # These are supported funding model platforms
github: nov
attr_required-1.0.2/lib/ 0000755 0000041 0000041 00000000000 14572524260 015171 5 ustar www-data www-data attr_required-1.0.2/lib/attr_optional.rb 0000644 0000041 0000041 00000001702 14572524260 020375 0 ustar www-data www-data 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.2/lib/attr_required.rb 0000644 0000041 0000041 00000002543 14572524260 020374 0 ustar www-data www-data 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.2/spec/ 0000755 0000041 0000041 00000000000 14572524260 015355 5 ustar www-data www-data attr_required-1.0.2/spec/spec_helper.rb 0000644 0000041 0000041 00000001374 14572524260 020200 0 ustar www-data www-data require 'simplecov'
SimpleCov.start do
add_filter 'spec'
end
RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = [:should, :expect]
end
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.2/spec/attr_required_spec.rb 0000644 0000041 0000041 00000007443 14572524260 021576 0 ustar www-data www-data require 'spec_helper'
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 == true
@c.attr_optional?(:attr_optional_b).should == 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 == true
B.attr_required?(:attr_required_a).should == true
B.attr_required?(:attr_required_b).should == true
B.attr_required?(:to_s).should == 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 == true
@b.attr_required?(:attr_required_a).should == true
@b.attr_required?(:attr_required_b).should == true
@a.attr_required?(:attr_required_b).should == false
@b.attr_required?(:to_s).should == false
end
end
describe '#attr_missing?' do
it 'should answer whether any attributes are missing' do
@a.attr_missing?.should == true
@b.attr_missing?.should == true
@a.attr_required_a = 'attr_required_a'
@b.attr_required_a = 'attr_required_a'
@a.attr_missing?.should == false
@b.attr_missing?.should == true
@b.attr_required_b = 'attr_required_b'
@b.attr_missing?.should == 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.2/spec/attr_optional_spec.rb 0000644 0000041 0000041 00000004516 14572524260 021601 0 ustar www-data www-data require 'spec_helper'
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 == false
@c.attr_optional?(:attr_required_b).should == 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 == true
B.attr_optional?(:attr_optional_a).should == true
B.attr_optional?(:attr_optional_b).should == true
B.attr_optional?(:to_s).should == 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 == true
@b.attr_optional?(:attr_optional_a).should == true
@b.attr_optional?(:attr_optional_b).should == true
@b.attr_optional?(:to_s).should == 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.2/.rspec 0000644 0000041 0000041 00000000037 14572524260 015540 0 ustar www-data www-data --color
--format=documentation
attr_required-1.0.2/Rakefile 0000644 0000041 0000041 00000000622 14572524260 016070 0 ustar www-data www-data 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.2/Gemfile 0000644 0000041 0000041 00000000046 14572524260 015716 0 ustar www-data www-data source 'https://rubygems.org'
gemspec
attr_required-1.0.2/LICENSE 0000644 0000041 0000041 00000002036 14572524260 015431 0 ustar www-data www-data 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.2/attr_required.gemspec 0000644 0000041 0000041 00000001367 14572524260 020651 0 ustar www-data www-data Gem::Specification.new do |s|
s.name = 'attr_required'
s.version = File.read('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 = 'https://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'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'rspec'
end
attr_required-1.0.2/VERSION 0000644 0000041 0000041 00000000005 14572524260 015466 0 ustar www-data www-data 1.0.2