initializer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,32 +1,45 @@
1
1
  module Initializer
2
+ NO_DEFAULT_VALUE = Object.new
3
+
2
4
  def self.included(base)
3
- base.extend ClassMethods
5
+ Extension.! base, ClassMethods
6
+ end
7
+
8
+ class Statement
9
+ attr_reader :code_fragment
10
+
11
+ def initialize(code_fragment)
12
+ @code_fragment = code_fragment
13
+ end
4
14
  end
5
15
 
6
16
  module ClassMethods
7
17
  extend self
8
-
18
+
9
19
  def initializer_macro(*parameters)
10
20
  Macro.generate_definitions self, parameters
11
21
  end
12
22
  alias :initializer :initializer_macro
13
23
 
14
- def r(parameter_name)
15
- return Parameter.new(parameter_name, :reader)
24
+ def r(parameter_name, default = NO_DEFAULT_VALUE)
25
+ return Parameter.build(parameter_name, :reader, default)
16
26
  end
17
27
 
18
- def w(parameter_name)
19
- return Parameter.new(parameter_name, :writer)
28
+ def w(parameter_name, default = NO_DEFAULT_VALUE)
29
+ return Parameter.build(parameter_name, :writer, default)
20
30
  end
21
31
 
22
- def a(parameter_name)
23
- return Parameter.new(parameter_name, :accessor)
32
+ def a(parameter_name, default = NO_DEFAULT_VALUE)
33
+ return Parameter.build(parameter_name, :accessor, default)
24
34
  end
25
35
  alias :rw :a
26
36
 
27
- def na(parameter_name)
28
- return Parameter.new(parameter_name, :no_accessor)
37
+ def na(parameter_name, default = NO_DEFAULT_VALUE)
38
+ return Parameter.build(parameter_name, :no_accessor, default)
29
39
  end
30
- end
31
40
 
41
+ def deferred(statement)
42
+ Statement.new(statement)
43
+ end
44
+ end
32
45
  end
@@ -12,7 +12,7 @@ module Initializer
12
12
 
13
13
  def self.build(target_class, parameters)
14
14
  parameters, options = separate_parameters(parameters)
15
- options.extend InitializerOptions
15
+ Extension.! options, InitializerOptions
16
16
  parameters = NormalizeParameters.normalize(parameters, options.visibility)
17
17
  instance = new target_class, parameters
18
18
  instance
@@ -56,7 +56,7 @@ module Initializer
56
56
 
57
57
  def self.define_initializer(target_class, parameters)
58
58
  parameters = parameters.map do |parameter|
59
- parameter.extend InitializerParameter
59
+ Extension.! parameter, InitializerParameter
60
60
  end
61
61
  instance = new target_class, parameters
62
62
  instance.define_initializer
@@ -68,14 +68,14 @@ module Initializer
68
68
  end
69
69
 
70
70
  def variable_assignment_statements
71
- variable_assignments = parameters.inject("") do|assignments, parameter|
71
+ variable_assignments = parameters.inject("") do |assignments, parameter|
72
72
  "#{assignments}#{parameter.assignment_statement}\n"
73
73
  end
74
74
  variable_assignments
75
75
  end
76
76
 
77
- def parameter_names
78
- parameter_names = parameters.map{|item| item.name }.to_a
77
+ def parameter_definitions
78
+ parameter_names = parameters.map{ |item| item.argument_definition }.to_a
79
79
  parameter_names = parameter_names.join(", ")
80
80
  parameter_names
81
81
  end
@@ -83,7 +83,7 @@ module Initializer
83
83
  def build_initializer_definition
84
84
  body =
85
85
  <<CTOR
86
- def initialize(#{parameter_names})
86
+ def initialize(#{parameter_definitions})
87
87
  #{variable_assignment_statements}
88
88
  end
89
89
  CTOR
@@ -100,6 +100,7 @@ CTOR
100
100
  end
101
101
  end
102
102
  end
103
+
103
104
  module InitializerOptions
104
105
  def visibility
105
106
  self[:default]
@@ -2,10 +2,55 @@ module Initializer
2
2
  class Parameter
3
3
  attr_reader :name
4
4
  attr_reader :visibility
5
+ attr_reader :default
5
6
 
6
7
  def initialize(name, visibility)
7
8
  @name = name
8
9
  @visibility = visibility
9
10
  end
11
+
12
+ def default_value(value)
13
+ @default = value
14
+ end
15
+
16
+ def argument_definition
17
+ return "#{@name} = #{default.code_fragment}" if @default
18
+
19
+ return @name
20
+ end
21
+
22
+ def self.build_default_value(value)
23
+ result = value
24
+
25
+ unless result.respond_to?(:code_fragment)
26
+ if [String, Symbol].include?(result.class)
27
+ result = StringDefaultValue.new(result.to_s)
28
+ else
29
+ result = Statement.new(result)
30
+ end
31
+ end
32
+ result
33
+ end
34
+
35
+ def self.build(name, visibility, default = NO_DEFAULT_VALUE)
36
+ instance = new(name, visibility)
37
+
38
+ unless default.eql?(NO_DEFAULT_VALUE)
39
+ default = build_default_value(default)
40
+ instance.default_value(default)
41
+ end
42
+
43
+ instance
44
+ end
45
+
46
+ class StringDefaultValue
47
+ def initialize(value)
48
+ @value = value
49
+ end
50
+
51
+ def code_fragment
52
+ "'#{@value}'"
53
+ end
54
+ end
10
55
  end
11
56
  end
data/lib/initializer.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'extension'
2
+
1
3
  require 'initializer/parameter'
2
4
  require 'initializer/macro'
3
5
  require 'initializer/initializer'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: initializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-21 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-07-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: extension
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description:
15
31
  email:
16
32
  executables: []