Permalink
Browse files

atterisk

  • Loading branch information...
0 parents commit 5a96aabeda1d9efe60f66d68537d5587d061f5e5 @ELLIOTTCABLE committed Oct 10, 2008
Showing with 418 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +6 −0 .manifest
  3. +139 −0 README.markdown
  4. +140 −0 Rakefile.rb
  5. +116 −0 attr_splat.gemspec
  6. +3 −0 lib/attr_splat.rb
  7. +5 −0 spec/attr_splat_spec.rb
  8. +6 −0 spec/spec_helper.rb
@@ -0,0 +1,3 @@
+.yardoc
+meta
+pkg
@@ -0,0 +1,6 @@
+lib/attr_splat.rb
+Rakefile.rb
+README.markdown
+spec/attr_splat_spec.rb
+spec/spec_helper.rb
+.manifest
@@ -0,0 +1,139 @@
+attr_*
+======
+*`attr_*`* is a library to extend the basic Ruby class/module attribute
+functions with some useful functionality. It supports the original attribute
+API, as well as additional features such as defaults values for attributes and
+attribute initialization.
+
+Specifically, three types of attribute initialization are supported:
+
+- none (as per the functionality of the original attr_accessor et al., the
+ instance variables won’t be initialized to any value — `instance_variable_defined?(:@ivar)`
+ will return false)
+- first-access initialization (the attribute will be undefined until first
+ accessed)
+- instance initialization (the attribute will be defined with the instance,
+ thus being defined for all parts of the lifecycle of the instance, for all
+ intents and purposes)
+
+Usage
+-----
+`attr_*` exposes a single method, `Module#attr_splat`, that takes a series of
+Symbols from which to create attributes, along with an options hash at the end.
+
+However, most users will want to replace the existing `attr_*` functionality
+with this (the API is fully backwards compatible; as long as none of your code
+depends on the particular inner workings of the existing `attr_*` methods, it
+should continue to work fine as-is). This isn't difficult, and for the most
+part amounts to the following:
+
+ class Module
+ def attr_accessor *args
+ attr_splat(*args)
+ end
+
+ def attr_reader *args
+ opts = args.last.is_a?(Hash) ? args.pop : Hash.new
+ args << {:writer => false}.merge(opts)
+ attr_splat(*args)
+ end
+
+ def attr_writer *args
+ opts = args.last.is_a?(Hash) ? args.pop : Hash.new
+ args << {:writer => false}.merge(opts)
+ attr_splat(*args)
+ end
+ end
+
+If you're lazy, you can shorten the above to something like this:
+
+ Module::attr_splat!
+
+Using `attr_*` is pretty simple - just use the `attr_*` methods as you
+normally would. If you need to supply a default value for an attribute, you
+can add a `:default` parameter as follows:
+
+ class Something
+ attr_accessor :something, :default => "summat"
+ end
+
+You can define more than one attribute with the same default at the same time:
+
+ attr_accessor :something, :something_else, :default => "summat"
+
+Attributes with a default flag will (by default) to the second initialization
+method described above - that is, the default value will be initialized into
+the attribute's variable only after the first time the attribute's getter is
+called. If you want default values to be initialized along with the instance
+of the class, you can pass the `:initialize` flag - it's disabled by default,
+because it relies on some metaprogrammy magic that this author prefers to
+avoid calling into every class all over Ruby's source.
+
+ attr_accessor :something, :something_else,
+ :default => "summat", :initialze => true
+
+If you call the initialize flag without a default, the attribute is
+initialized to `nil`.
+
+ attr_accessor :something, :something_else, :initialze => true
+
+Installation
+------------
+You can install `attr_*` as a pre-built gem, or as a gem generated directly
+from the source.
+
+The easiest way to install `attr_*` is to use [RubyGems][] to acquire the
+latest 'release' version from [RubyForge][], using the `gem` command line tool:
+
+ sudo gem install attr_splat # You'll be asked for your account password.
+
+Alternatively, you can acquire it (possibly slightly more up-to-date,
+depending on how often I update the gemspec) from GitHub as follows:
+
+ # If you've ever done this before, you don't need to do it now - see http://gems.github.com
+ gem sources -a http://gems.github.com
+ sudo gem install elliottcable-attr_splat # You'll be asked for your account password.
+
+Finally, you can build a gem from the latest source yourself. You need [git][],
+as well as [Rake][] and [echoe][]:
+
+ git clone git://github.com/elliottcable/attr_splat.git
+ cd attr_splat
+ rake package:install # You'll be asked for your account password.
+
+[RubyGems]: <http://rubyforge.org/projects/rubygems/> "RubyGems - Ruby package manager"
+[RubyForge]: <http://rubyforge.org/projects/attr_splat/> "attr_* on RubyForge"
+[git]: <http://git-scm.com/> "git - Fast Version Control System"
+[Rake]: <http://rake.rubyforge.org/> "RAKE - Ruby Make"
+[echoe]: <http://github.com/fauna/echoe> "If you don't want to hoe, echoe"
+
+Contributing
+------------
+You can contribute bug fixes or new features to `attr_*` by forking the project
+on GitHub (you'll need to register for an account first), and sending me a
+pull request once you've committed your changes.
+
+Links
+-----
+- [GitHub](http://github.com/elliottcable/attr_splat "attr_* on GitHub") is the
+ project's primary repository host, and currently also the project's home
+ page
+- [RubyForge](http://rubyforge.org/projects/attr_splat "attr_* on RubyForge") is
+ out primary RubyGem host, as well as an alternative repository host
+- [integrity](http://integrit.yreality.net/attr_splat "attr_* on yreality's integrity server")
+ is out continuous integration server - if the top build on that page is
+ green, you can assume the latest git HEAD is safe to run/install/utilize.
+- [Gitorious](http://gitorious.org/projects/attr_splat "attr_* on Gitorious") is
+ an alternative repository host
+- [repo.or.cz](http://repo.or.cz/w/attr_splat.git "attr_* on repo.or.cz") is
+ an alternative repository host
+
+License
+-------
+`attr_*` is copyright 2008 by elliott cable.
+
+`attr_*` is released under the [GNU General Public License v3.0][gpl], which
+allows you to freely utilize, modify, and distribute all `attr_*`'s source code
+(subject to the terms of the aforementioned license).
+
+[gpl]: <http://www.gnu.org/licenses/gpl.txt> "The GNU General Public License v3.0"
@@ -0,0 +1,140 @@
+($:.unshift File.expand_path(File.join( File.dirname(__FILE__), 'lib' ))).uniq!
+begin
+ require 'attr_splat'
+rescue LoadError
+ module AttrSplat; Version = -1; end
+end
+
+# =======================
+# = Gem packaging tasks =
+# =======================
+begin
+ require 'echoe'
+
+ task :package => :'package:install'
+ task :manifest => :'package:manifest'
+ namespace :package do
+ Echoe.new('attr_splat', AttrSplat::Version) do |g|
+ g.project = 'attr_splat'
+ g.author = ['elliottcable']
+ g.email = ['attr_splat@elliottcable.com']
+ g.summary = "attr_* puts Ruby Core's attr_accessor, attr_reader, and attr_reader on steroids"
+ g.url = 'http://by.elliottcable.name/attr_splat.xhtml'
+ g.development_dependencies = ['echoe >=3.0.1', 'rspec', 'rcov', 'yard', 'stringray']
+ g.manifest_name = '.manifest'
+ g.ignore_pattern = /^\.git\/|^meta\/|\.gemspec/
+ end
+
+ desc 'tests packaged files to ensure they are all present'
+ task :verify => :package do
+ # An error message will be displayed if files are missing
+ if system %(ruby -e "require 'rubygems'; require 'pkg/attr_splat-#{AttrSplat::Version}/lib/attr_splat'")
+ puts "\nThe library files are present"
+ end
+ end
+
+ task :copy_gemspec => [:package] do
+ pkg = Dir['pkg/*'].select {|dir| File.directory? dir}.last
+ mv File.join(pkg, pkg.gsub(/^pkg\//,'').gsub(/\-\d+$/,'.gemspec')), './'
+ end
+
+ desc 'builds a gemspec as GitHub wants it'
+ task :gemspec => [:package, :copy_gemspec, :clobber_package]
+ end
+
+rescue LoadError
+ desc 'You need the `echoe` gem to package attr_*'
+ task :package
+end
+
+# =======================
+# = Spec/Coverage tasks =
+# =======================
+begin
+ require 'spec'
+ require 'rcov'
+ require 'spec/rake/spectask'
+
+ task :default => :'coverage:run'
+ task :coverage => :'coverage:run'
+ namespace :coverage do
+ Spec::Rake::SpecTask.new(:run) do |t|
+ t.spec_opts = ["--format", "specdoc"]
+ t.spec_opts << "--colour" unless ENV['CI']
+ t.spec_files = Dir['spec/**/*_spec.rb'].sort
+ t.libs = ['lib']
+ t.rcov = true
+ t.rcov_opts = [ '--include-file', '"^lib"', '--exclude-only', '".*"']
+ t.rcov_dir = File.join('meta', 'coverage')
+ end
+
+ begin
+ require 'spec/rake/verify_rcov'
+ # For the moment, this is the only way I know of to fix RCov. I may
+ # release the fix as it's own gem at some point in the near future.
+ require 'stringray/core_ext/spec/rake/verify_rcov'
+ RCov::VerifyTask.new(:verify) do |t|
+ t.threshold = 95.0
+ t.index_html = File.join('meta', 'coverage', 'index.html')
+ t.require_exact_threshold = false
+ end
+ rescue LoadError
+ desc 'You need the `stringray` gem to verify coverage'
+ task :verify
+ end
+
+ task :open do
+ system 'open ' + File.join('meta', 'coverage', 'index.html') if PLATFORM['darwin']
+ end
+ end
+
+rescue LoadError
+ desc 'You need the `rcov` and `rspec` gems to run specs/coverage'
+ task :coverage
+end
+
+# =======================
+# = Documentation tasks =
+# =======================
+begin
+ require 'yard'
+ require 'yard/rake/yardoc_task'
+
+ task :documentation => :'documentation:generate'
+ namespace :documentation do
+ YARD::Rake::YardocTask.new :generate do |t|
+ t.files = ['lib/**/*.rb']
+ t.options = ['--output-dir', File.join('meta', 'documentation'),
+ '--readme', 'README.markdown']
+ end
+
+ YARD::Rake::YardocTask.new :dotyardoc do |t|
+ t.files = ['lib/**/*.rb']
+ t.options = ['--no-output',
+ '--readme', 'README.markdown']
+ end
+
+ task :open do
+ system 'open ' + File.join('meta', 'documentation', 'index.html') if PLATFORM['darwin']
+ end
+ end
+
+rescue LoadError
+ desc 'You need the `yard` gem to generate documentation'
+ task :documentation
+end
+
+# =========
+# = Other =
+# =========
+desc 'Removes all meta producs'
+task :clobber do
+ `rm -rf #{File.expand_path(File.join( File.dirname(__FILE__), 'meta' ))}`
+end
+
+desc 'Check everything over before commiting'
+task :aok => [:'documentation:generate', :'documentation:open',
+ :'package:manifest', :'package:gemspec',
+ :'coverage:run', :'coverage:open', :'coverage:verify']
+
+task :ci => [:'documentation:generate', :'coverage:run', :'coverage:verify']
Oops, something went wrong.

0 comments on commit 5a96aab

Please sign in to comment.