maiha-dsl_accessor 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/core_ext/class/dsl_accessor.rb +0 -5
- data/core_ext/module/delegation.rb +95 -0
- data/lib/dsl_accessor.rb +8 -0
- metadata +4 -2
data/Rakefile
CHANGED
@@ -33,7 +33,7 @@ AUTHOR = "maiha"
|
|
33
33
|
EMAIL = "maiha@wota.jp"
|
34
34
|
HOMEPAGE = "http://github.com/maiha/dsl_accessor"
|
35
35
|
SUMMARY = "This plugin gives hybrid accessor class methods to classes by DSL like definition"
|
36
|
-
GEM_VERSION = "0.3.
|
36
|
+
GEM_VERSION = "0.3.3"
|
37
37
|
|
38
38
|
spec = Gem::Specification.new do |s|
|
39
39
|
# s.rubyforge_project = 'merb'
|
@@ -1,8 +1,3 @@
|
|
1
|
-
unless Class.new.respond_to?(:write_inheritable_attribute)
|
2
|
-
require File.dirname(__FILE__) + "/../duplicable" unless Object.new.respond_to?(:duplicable?)
|
3
|
-
require File.dirname(__FILE__) + "/inheritable_attributes"
|
4
|
-
end
|
5
|
-
|
6
1
|
module DslAccessor
|
7
2
|
def dsl_accessor(*args)
|
8
3
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
@@ -0,0 +1,95 @@
|
|
1
|
+
class Module
|
2
|
+
# Provides a delegate class method to easily expose contained objects' methods
|
3
|
+
# as your own. Pass one or more methods (specified as symbols or strings)
|
4
|
+
# and the name of the target object as the final <tt>:to</tt> option (also a symbol
|
5
|
+
# or string). At least one method and the <tt>:to</tt> option are required.
|
6
|
+
#
|
7
|
+
# Delegation is particularly useful with Active Record associations:
|
8
|
+
#
|
9
|
+
# class Greeter < ActiveRecord::Base
|
10
|
+
# def hello() "hello" end
|
11
|
+
# def goodbye() "goodbye" end
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# class Foo < ActiveRecord::Base
|
15
|
+
# belongs_to :greeter
|
16
|
+
# delegate :hello, :to => :greeter
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Foo.new.hello # => "hello"
|
20
|
+
# Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>
|
21
|
+
#
|
22
|
+
# Multiple delegates to the same target are allowed:
|
23
|
+
#
|
24
|
+
# class Foo < ActiveRecord::Base
|
25
|
+
# belongs_to :greeter
|
26
|
+
# delegate :hello, :goodbye, :to => :greeter
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Foo.new.goodbye # => "goodbye"
|
30
|
+
#
|
31
|
+
# Methods can be delegated to instance variables, class variables, or constants
|
32
|
+
# by providing them as a symbols:
|
33
|
+
#
|
34
|
+
# class Foo
|
35
|
+
# CONSTANT_ARRAY = [0,1,2,3]
|
36
|
+
# @@class_array = [4,5,6,7]
|
37
|
+
#
|
38
|
+
# def initialize
|
39
|
+
# @instance_array = [8,9,10,11]
|
40
|
+
# end
|
41
|
+
# delegate :sum, :to => :CONSTANT_ARRAY
|
42
|
+
# delegate :min, :to => :@@class_array
|
43
|
+
# delegate :max, :to => :@instance_array
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# Foo.new.sum # => 6
|
47
|
+
# Foo.new.min # => 4
|
48
|
+
# Foo.new.max # => 11
|
49
|
+
#
|
50
|
+
# Delegates can optionally be prefixed using the <tt>:prefix</tt> option. If the value
|
51
|
+
# is <tt>true</tt>, the delegate methods are prefixed with the name of the object being
|
52
|
+
# delegated to.
|
53
|
+
#
|
54
|
+
# Person = Struct.new(:name, :address)
|
55
|
+
#
|
56
|
+
# class Invoice < Struct.new(:client)
|
57
|
+
# delegate :name, :address, :to => :client, :prefix => true
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# john_doe = Person.new("John Doe", "Vimmersvej 13")
|
61
|
+
# invoice = Invoice.new(john_doe)
|
62
|
+
# invoice.client_name # => "John Doe"
|
63
|
+
# invoice.client_address # => "Vimmersvej 13"
|
64
|
+
#
|
65
|
+
# It is also possible to supply a custom prefix.
|
66
|
+
#
|
67
|
+
# class Invoice < Struct.new(:client)
|
68
|
+
# delegate :name, :address, :to => :client, :prefix => :customer
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# invoice = Invoice.new(john_doe)
|
72
|
+
# invoice.customer_name # => "John Doe"
|
73
|
+
# invoice.customer_address # => "Vimmersvej 13"
|
74
|
+
#
|
75
|
+
def delegate(*methods)
|
76
|
+
options = methods.pop
|
77
|
+
unless options.is_a?(Hash) && to = options[:to]
|
78
|
+
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
|
79
|
+
end
|
80
|
+
|
81
|
+
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
|
82
|
+
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
|
83
|
+
end
|
84
|
+
|
85
|
+
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
|
86
|
+
|
87
|
+
methods.each do |method|
|
88
|
+
module_eval(<<-EOS, "(__DELEGATION__)", 1)
|
89
|
+
def #{prefix}#{method}(*args, &block)
|
90
|
+
#{to}.__send__(#{method.inspect}, *args, &block)
|
91
|
+
end
|
92
|
+
EOS
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/dsl_accessor.rb
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
unless Class.new.respond_to?(:write_inheritable_attribute)
|
2
|
+
require File.dirname(__FILE__) + "/../core_ext/duplicable" unless Object.new.respond_to?(:duplicable?)
|
3
|
+
require File.dirname(__FILE__) + "/../core_ext/class/inheritable_attributes"
|
4
|
+
end
|
5
|
+
|
6
|
+
unless Module.new.respond_to?(:delegate)
|
7
|
+
require File.dirname(__FILE__) + "/../core_ext/module/delegation"
|
8
|
+
end
|
1
9
|
|
2
10
|
require File.dirname(__FILE__) + '/../core_ext/class/dsl_accessor'
|
3
11
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maiha-dsl_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maiha
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-08 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -27,6 +27,8 @@ files:
|
|
27
27
|
- README
|
28
28
|
- Rakefile
|
29
29
|
- core_ext/duplicable.rb
|
30
|
+
- core_ext/module
|
31
|
+
- core_ext/module/delegation.rb
|
30
32
|
- core_ext/class
|
31
33
|
- core_ext/class/dsl_accessor.rb
|
32
34
|
- core_ext/class/inheritable_attributes.rb
|