diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2009-10-08 22:31:55 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2011-06-30 08:08:47 +0200 |
commit | 72a8a4e7aeff0fc93d2199c4d0cd25e3dffc1196 (patch) | |
tree | 7e7c1b2fdeb740c3032f9b1763afd5a2875d47e7 | |
parent | c829d9395bc1152fac97ed16f2fcf6ed93fb60ee (diff) | |
download | ayk-72a8a4e7aeff0fc93d2199c4d0cd25e3dffc1196.zip ayk-72a8a4e7aeff0fc93d2199c4d0cd25e3dffc1196.tar.gz |
add Optioned module
-rw-r--r-- | lib/ayk/options.rb | 42 | ||||
-rw-r--r-- | spec/options.rb | 4 |
2 files changed, 24 insertions, 22 deletions
diff --git a/lib/ayk/options.rb b/lib/ayk/options.rb index 5650609..5b34481 100644 --- a/lib/ayk/options.rb +++ b/lib/ayk/options.rb @@ -32,6 +32,8 @@ module AYK yield(self) if block_given? end # + attr_reader :name + # # Shortcut for instance_eval def dsl &block instance_eval(&block) if block @@ -142,26 +144,22 @@ module AYK def pretty_print q; q.pp_hash @hash end end # -# # extend your class with this -# module Optioned -# def self.included(into) -# into.extend(SingletonMethods) -# -# snaked = into.name.split('::').last -# snaked = snaked.gsub(/\B[A-Z][^A-Z]/, '_\&').downcase.gsub(' ', '_') -# -# options = Innate.options.sub(snaked) -# into.instance_variable_set(:@options, options) -# end -# -# module SingletonMethods -# attr_reader :options -# end -# -# private -# -# def options -# self.class.options -# end -# end + # extend your class with this + module Optioned + def self.included(into) + into.extend(SingletonMethods) + snaked = into.name.split('::').last + snaked = snaked.gsub(/\B[A-Z][^A-Z]/, '_\&').downcase.gsub(' ', '_') + into.instance_variable_set :@options, Options.new(snaked) + end + # + module SingletonMethods + attr_reader :options + end + # + private + def options + self.class.options + end + end end diff --git a/spec/options.rb b/spec/options.rb index 25741db..0fd84d5 100644 --- a/spec/options.rb +++ b/spec/options.rb @@ -81,6 +81,10 @@ describe AYK::Options do end opts.var.should eql 666 end + it "should be nicely included" do + class A__A; include AYK::Optioned end + A__A.options.name.should eql 'a__a' + end end # end |