pedlar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/pedlar.rb +109 -0
  2. data/lib/pedlar/version.rb +3 -0
  3. metadata +63 -0
@@ -0,0 +1,109 @@
1
+ # encoding: utf-8
2
+
3
+ # maybe i like that one too much...
4
+ require 'forwardable'
5
+
6
+ module Pedlar
7
+
8
+ # Forwardable is available to the extending class
9
+ include Forwardable
10
+
11
+ def peddles(*brands)
12
+ # do we have a list of interface to setup
13
+ # or one interface with an alias ?
14
+ brands = [brands] unless brands.all? { |brand| brand.is_a? Class }
15
+
16
+ # Ruby is too nice a pal... It accepts to assign iteration
17
+ # variables in a juicy DWIM way : depending on the arguments
18
+ # we got, `brands` is either a flat list of classes
19
+ # or a list of one list. But it's the same to my pal...
20
+ brands.each do |brand, as|
21
+ # lousy lousy active_support neat methods mockery
22
+ as ||= brand.to_s.downcase.gsub('::', '_')
23
+
24
+ # three class methods per `brand` to setup accessors/mutators.
25
+ %w|accessor writer reader|.each do |type|
26
+ # ex: brand=Date defines `date_accessor`, `date_writer`, `date_reader`.
27
+ # Each of these three calls private methods (`type`) setting up
28
+ # the actual accessor/mutator with user-defined name.
29
+ define_singleton_method "#{as}_#{type}".to_sym do |*accessors|
30
+ # `accessors` below being the user-defined accessors names.
31
+ accessors.each do |accessor|
32
+ send type, brand, accessor
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ # hand-made delegation setup
40
+ def safe_delegator(delegate, delegation, method=delegation)
41
+ safe_delegation delegate, delegation, method
42
+ end
43
+
44
+ # hand-made delegations setup
45
+ def safe_delegators(delegate, *delegations)
46
+ delegations.each { |d| safe_delegation delegate, d }
47
+ end
48
+
49
+ private
50
+
51
+ # hand-made delegation
52
+ # tests if delegate is not nil to avoid crash.
53
+ # it returns nil in this case.
54
+ def safe_delegation(delegate, delegation, method=delegation)
55
+ define_method method.to_sym do |*args, &block|
56
+ # Forwardable makes an eval so it does not care whether
57
+ # delegate is a method or an instance variable.
58
+ # I chose not to do it (so far), so i have to try both
59
+ # ways to get the delegate, in a violent way. But i like it.
60
+ interface = begin
61
+ send :instance_variable_get, delegate.to_s
62
+ rescue
63
+ send delegate
64
+ end
65
+
66
+ interface && interface.send(delegation, *args, &block)
67
+ end
68
+ end
69
+
70
+ # defines two instance writer methods
71
+ # - `accessor=`
72
+ # - `accessor_with`
73
+ def writer(brand, accessor)
74
+ # classic setter scheme, it does nothing if value
75
+ # parameter is not a `brand` instance though
76
+ define_method "#{accessor}=".to_sym do |value|
77
+ instance_variable_set "@#{accessor}", value if value.is_a? brand
78
+ end
79
+
80
+ # pedlar setter `accessor_with`
81
+ # it sets instance variable with :
82
+ # - fitting user-defined method
83
+ # - or instance creation with passed params
84
+ # - or value that must a be an instance of `brand`
85
+ define_method "#{accessor}_with".to_sym do |*values|
86
+ instance_variable_set "@#{accessor}",
87
+ if respond_to? "#{accessor}_setter", true
88
+ send "#{accessor}_setter", *values
89
+ elsif !values.first.is_a? brand
90
+ brand.new *values
91
+ else
92
+ values.first
93
+ end
94
+ end
95
+ end
96
+
97
+ # defines an instance reader method with `accessor` as its name
98
+ def reader(brand, accessor)
99
+ define_method "#{accessor}".to_sym do
100
+ instance_variable_get "@#{accessor}"
101
+ end
102
+ end
103
+
104
+ # defines reader and writer
105
+ def accessor(brand, accessor)
106
+ reader(brand, accessor) && writer(brand, accessor)
107
+ end
108
+
109
+ end
@@ -0,0 +1,3 @@
1
+ module Pedlar
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pedlar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - lacravate
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: pedlar peddles interfaces (through accessors and delegation)
31
+ email:
32
+ - lacravate@lacravate.fr
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/pedlar.rb
38
+ - lib/pedlar/version.rb
39
+ homepage: https://github.com/lacravate/pedlar
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project: ! '[none]'
59
+ rubygems_version: 1.8.24
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: pedlar peddles interfaces (through accessors and delegation)
63
+ test_files: []