initializer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ module Initializer
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ extend self
8
+
9
+ def initializer_macro(*parameters)
10
+ Macro.generate_definitions self, parameters
11
+ end
12
+ alias :initializer :initializer_macro
13
+
14
+ def r(parameter_name)
15
+ return Parameter.new(parameter_name, :reader)
16
+ end
17
+
18
+ def w(parameter_name)
19
+ return Parameter.new(parameter_name, :writer)
20
+ end
21
+
22
+ def a(parameter_name)
23
+ return Parameter.new(parameter_name, :accessor)
24
+ end
25
+ alias :rw :a
26
+
27
+ def na(parameter_name)
28
+ return Parameter.new(parameter_name, :no_accessor)
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,174 @@
1
+ module Initializer
2
+ class Macro
3
+ DEFAULT_VISIBILITY = :reader
4
+
5
+ attr_reader :target_class
6
+ attr_reader :parameters
7
+
8
+ def initialize(target_class, parameters)
9
+ @target_class = target_class
10
+ @parameters = parameters
11
+ end
12
+
13
+ def self.build(target_class, parameters)
14
+ parameters, options = separate_parameters(parameters)
15
+ options.extend InitializerOptions
16
+ parameters = NormalizeParameters.normalize(parameters, options.visibility)
17
+ instance = new target_class, parameters
18
+ instance
19
+ end
20
+
21
+ def self.generate_definitions(target_class, parameters)
22
+ instance = build target_class, parameters
23
+ instance.generate_definitions
24
+ end
25
+
26
+ def self.separate_parameters(parameters)
27
+ options = { :default => DEFAULT_VISIBILITY }
28
+ options = parameters.pop if options?(parameters)
29
+
30
+ return parameters, options
31
+ end
32
+
33
+ def self.options?(parameters)
34
+ parameters.last.is_a? Hash
35
+ end
36
+
37
+ def generate_definitions
38
+ define_attributes
39
+ InitializerDefinition.define_initializer(target_class, parameters)
40
+ end
41
+
42
+ def define_attributes
43
+ parameters.each do |p|
44
+ Attribute.define target_class, p.name, p.visibility
45
+ end
46
+ end
47
+
48
+ class InitializerDefinition
49
+ attr_reader :target_class
50
+ attr_reader :parameters
51
+
52
+ def initialize(target_class, parameters)
53
+ @target_class = target_class
54
+ @parameters = parameters
55
+ end
56
+
57
+ def self.define_initializer(target_class, parameters)
58
+ parameters = parameters.map do |parameter|
59
+ parameter.extend InitializerParameter
60
+ end
61
+ instance = new target_class, parameters
62
+ instance.define_initializer
63
+ end
64
+
65
+ def define_initializer
66
+ body = build_initializer_definition
67
+ target_class.class_eval body
68
+ end
69
+
70
+ def variable_assignment_statements
71
+ variable_assignments = parameters.inject("") do|assignments, parameter|
72
+ "#{assignments}#{parameter.assignment_statement}\n"
73
+ end
74
+ variable_assignments
75
+ end
76
+
77
+ def parameter_names
78
+ parameter_names = parameters.map{|item| item.name }.to_a
79
+ parameter_names = parameter_names.join(", ")
80
+ parameter_names
81
+ end
82
+
83
+ def build_initializer_definition
84
+ body =
85
+ <<CTOR
86
+ def initialize(#{parameter_names})
87
+ #{variable_assignment_statements}
88
+ end
89
+ CTOR
90
+ end
91
+
92
+ module InitializerParameter
93
+ def variable_name
94
+ "@#{name}"
95
+ end
96
+
97
+ def assignment_statement
98
+ "#{variable_name} = #{name}"
99
+ end
100
+ end
101
+ end
102
+ end
103
+ module InitializerOptions
104
+ def visibility
105
+ self[:default]
106
+ end
107
+ end
108
+
109
+ class Attribute
110
+ attr_reader :target_class
111
+ attr_reader :name
112
+ attr_reader :visibility
113
+
114
+ def initialize(target_class, name, visibility)
115
+ @target_class = target_class
116
+ @name = name
117
+ @visibility = visibility
118
+ end
119
+
120
+ def self.define(target_class, name, visibility)
121
+ instance = new target_class, name, visibility
122
+ instance.define
123
+ end
124
+
125
+ def define
126
+ define_getter if [:reader, :accessor].include? visibility
127
+ define_setter if [:writer, :accessor].include? visibility
128
+ end
129
+
130
+ def define_getter
131
+ name = self.name
132
+ target_class.send :define_method, name do
133
+ instance_variable_get("@#{name}")
134
+ end
135
+ end
136
+
137
+ def define_setter
138
+ name = self.name
139
+ target_class.send :define_method, "#{name}=" do |value|
140
+ instance_variable_set("@#{name}", value)
141
+ end
142
+ end
143
+ end
144
+
145
+ class NormalizeParameters
146
+ attr_reader :parameters
147
+ attr_reader :default_visibility
148
+
149
+ def initialize(parameters, default_visibility)
150
+ @parameters = parameters
151
+ @default_visibility = default_visibility
152
+ end
153
+
154
+ def self.normalize(parameters, default_visibility)
155
+ instance = new parameters, default_visibility
156
+ instance.normalize
157
+ instance.parameters
158
+ end
159
+
160
+ def normalize
161
+ @parameters = parameters.map do |p|
162
+ normalize_parameter p
163
+ end
164
+ end
165
+
166
+ def normalize_parameter(parameter)
167
+ if parameter.is_a? Symbol
168
+ return Parameter.new parameter, default_visibility
169
+ else
170
+ return parameter
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,11 @@
1
+ module Initializer
2
+ class Parameter
3
+ attr_reader :name
4
+ attr_reader :visibility
5
+
6
+ def initialize(name, visibility)
7
+ @name = name
8
+ @visibility = visibility
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ require 'initializer/parameter'
2
+ require 'initializer/macro'
3
+ require 'initializer/initializer'
4
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: initializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - The Sans Collective
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/initializer/initializer.rb
21
+ - lib/initializer/macro.rb
22
+ - lib/initializer/parameter.rb
23
+ - lib/initializer.rb
24
+ homepage: https://github.com/Sans/initializer
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: 1.9.2
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.25
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Initializer Library
48
+ test_files: []