auser-dslify 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/dslify.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{dslify}
5
- s.version = "0.0.4"
5
+ s.version = "0.0.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Ari Lerner"]
9
- s.date = %q{2009-03-16}
9
+ s.date = %q{2009-04-14}
10
10
  s.description = %q{Easily add DSL-like calls to any class}
11
11
  s.email = ["arilerner@mac.com"]
12
12
  s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "website/index.txt"]
data/lib/dslify/dslify.rb CHANGED
@@ -1,23 +1,69 @@
1
1
  # Quick 1-file dsl accessor
2
- class Object
3
- def h
4
- @h||={}
5
- end
6
- def dsl_options;h;end
7
- def set_vars_from_options(h={})
8
- h.each{|k,v|send k.to_sym, v } unless h.empty?
2
+ module Dslify
3
+ module ClassMethods
4
+ def default_options(hsh={})
5
+ @default_dsl_options ||= hsh
6
+ end
9
7
  end
10
- def method_missing(m,*a,&block)
11
- if block
12
- if args.empty?
13
- ((a[0].class==self.class)?a[0].instance_eval(&block): super)
8
+
9
+ module InstanceMethods
10
+ def default_dsl_options;self.class.default_options;end
11
+ def dsl_options(hsh={})
12
+ @dsl_options ||= default_dsl_options.merge(hsh)
13
+ end
14
+ alias :options :dsl_options
15
+ def set_vars_from_options(h={}, contxt=self)
16
+ h.each{|k,v| contxt.send k.to_sym, v }
17
+ end
18
+ def add_method(meth)
19
+ # instance_eval <<-EOM
20
+ # def #{meth}(n=nil)
21
+ # puts "called #{meth}(\#\{n\}) from \#\{self\}"
22
+ # n ? (__h[:#{meth}] = n) : __h[:#{meth}]
23
+ # end
24
+ # def #{meth}=(n)
25
+ # __h[:#{meth}] = n
26
+ # end
27
+ # EOM
28
+ end
29
+ def method_missing(m,*a,&block)
30
+ if block
31
+ if a.empty?
32
+ (a[0].class == self.class) ? a[0].instance_eval(&block) : super
33
+ else
34
+ inst = a[0]
35
+ inst.instance_eval(&block)
36
+ dsl_options[m] = inst
37
+ end
14
38
  else
15
- inst = a[0]
16
- inst.instance_eval(&block)
17
- h[m] = inst
39
+ if a.empty?
40
+ if dsl_options.has_key?(m)
41
+ dsl_options[m]
42
+ elsif m.to_s.index("?") == (m.to_s.length - 1)
43
+ if options.has_key?(val = m.to_s.gsub(/\?/, '').to_sym)
44
+ options[val] != false
45
+ else
46
+ false
47
+ end
48
+ else
49
+ if self.class.superclass.respond_to?(:default_options) && self.class.superclass.default_options.has_key?(m)
50
+ self.class.superclass.default_options[m]
51
+ elsif ((respond_to? :parent) && (parent != self))
52
+ parent.send m, *a, &block
53
+ else
54
+ super
55
+ end
56
+ end
57
+ else
58
+ clean_meth = m.to_s.gsub(/\=/,"").to_sym
59
+ dsl_options[clean_meth] = (a.size > 1 ? a : a[0])
60
+ end
18
61
  end
19
- else
20
- ((a.empty?)?h[m]:h[m.to_s.gsub(/\=/,"").to_sym]=(a.size>1?a:a[0]))
21
62
  end
22
63
  end
23
- end
64
+
65
+ def self.included(receiver)
66
+ receiver.extend ClassMethods
67
+ receiver.send :include, InstanceMethods
68
+ end
69
+ end
@@ -1,10 +1,10 @@
1
1
  module Dslify
2
2
  module VERSION #:nodoc:
3
- MAJOR = 0
4
- MINOR = 0
5
- TINY = 4
3
+ MAJOR = 0 unless const_defined?("MAJOR")
4
+ MINOR = 0 unless const_defined?("MINOR")
5
+ TINY = 5 unless const_defined?("TINY")
6
6
 
7
- STRING = [MAJOR, MINOR, TINY].join('.')
7
+ STRING = [MAJOR, MINOR, TINY].join('.') unless const_defined?("STRING")
8
8
  self
9
9
  end
10
10
  end
data/test/test_dslify.rb CHANGED
@@ -4,6 +4,7 @@ require "matchy"
4
4
  require "context"
5
5
 
6
6
  class Quickie
7
+ include Dslify
7
8
  end
8
9
 
9
10
  class QuickieTest < Test::Unit::TestCase
@@ -24,7 +25,7 @@ class QuickieTest < Test::Unit::TestCase
24
25
  end
25
26
  it "should set these values in the h Hash on the object" do
26
27
  @q.movies "can be fun"
27
- @q.h.keys.should == [:movies]
28
+ @q.dsl_options.keys.should == [:movies]
28
29
  end
29
30
  it "should set multiple keys with set_vars_from_options" do
30
31
  @q.set_vars_from_options({:a => "a", :b => "b"})
@@ -42,5 +43,72 @@ class QuickieTest < Test::Unit::TestCase
42
43
  end
43
44
  @q.bobs.franks.should == "franke"
44
45
  end
46
+ it "should not blow up when called with a ? at the end of the method" do
47
+ @q.set_vars_from_options({:pete => "and pete"})
48
+ lambda{@q.pete?}.should_not raise_error
49
+ end
50
+ it "should return false if the method exists" do
51
+ @q.bones "is a tv show"
52
+ @q.bops?.should == false
53
+ end
54
+ it "should return true if the option is set" do
55
+ @q.bones "is a tv show"
56
+ @q.bones?.should == true
57
+ end
58
+ end
59
+
60
+ context "with inheritance and classes" do
61
+ before do
62
+ class Pop
63
+ include Dslify
64
+ def initialize(h={})
65
+ dsl_options h
66
+ end
67
+ default_options :name => "pop"
68
+ end
69
+
70
+ class Foo < Pop
71
+ default_options :name=>'fooey'
72
+ end
73
+
74
+ class Bar < Pop
75
+ default_options :name=>'pangy', :taste => "spicy"
76
+ end
77
+
78
+ class Dad < Pop
79
+ end
80
+
81
+ @pop = Pop.new
82
+ @poptart = Pop.new :name => "Cinnamon"
83
+ @foo = Foo.new
84
+ @bar = Bar.new
85
+ end
86
+ it "should take the default options set on the class" do
87
+ @pop.dsl_options[:name].should == "pop"
88
+ end
89
+ it "should allow us to add defaults on the instance by calling dsl_options" do
90
+ @poptart.name.should == "Cinnamon"
91
+ end
92
+ it "should take the default options on a second class that inherits from the base" do
93
+ @foo.name.should == "fooey"
94
+ end
95
+ it "should take the default options on a third inheriting class" do
96
+ @bar.name.should == "pangy"
97
+ end
98
+ it "should not add a method not in the default_dsl_options" do
99
+ @bar.respond_to?(:boat).should == false
100
+ end
101
+ it "should return the original default options test" do
102
+ @bar.default_dsl_options[:taste].should == "spicy"
103
+ @bar.default_dsl_options[:name].should == "pangy"
104
+ end
105
+ it "should set the default options of the child to the superclass's if it doesn't exist" do
106
+ Dad.new.name.should == "pop"
107
+ end
108
+ it "should raise if the method isn't found on itself, the parent or in the rest of the method missing chain" do
109
+ lambda {
110
+ Class.new.sanitorium
111
+ }.should raise_error
112
+ end
45
113
  end
46
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auser-dslify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Lerner
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-16 00:00:00 -07:00
12
+ date: 2009-04-14 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency