From 72a8a4e7aeff0fc93d2199c4d0cd25e3dffc1196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 8 Oct 2009 22:31:55 +0200 Subject: add Optioned module --- lib/ayk/options.rb | 42 ++++++++++++++++++++---------------------- 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 -- cgit v1.1-2-g2b99