conject 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ Conject v0.1.3
2
+ * Changed the way Class.construct_with rewrites Class.new(). It is now safe to re-load the source code of a class that uses construct_with.
3
+
1
4
  Conject v0.1.2
2
5
  * Aliasing objects via 'is', eg: context.configure_objects album: { is: "and_justice_for_all" }
3
6
  Conject v0.1.1
@@ -78,63 +78,30 @@ class Class
78
78
  end
79
79
  klass.meta_eval do private :object_context_prep end
80
80
 
81
- unless klass.methods.include?(:actual_new) # only do this once per family tree (subclasses will inherit the Conjected .new)
82
- # Alias :new such that we can wrap and invoke it later
83
- klass.meta_eval do
84
- alias_method :actual_new, :new
81
+ klass.meta_def :new do |component_map|
82
+ obj = allocate
83
+ if has_object_definition?
84
+ # Apply the given components
85
+ obj.send(:set_components, component_map)
86
+ else
87
+ raise "#{self.class} has an ancestor that uses construct_with, but has not declared any component dependencies. Will not be able to instantiate!"
85
88
  end
86
89
 
87
- # Override default :new behavior for this class.
88
- #
89
- # The .new method is rewritten to accept a single argument:
90
- # component_map: a Hash containing all required objects to construct a new instance.
91
- # Keys are expected to be symbols.
92
- #
93
- # If user defines their own #initialize method, all components sent into .new
94
- # will be installed BEFORE the user-defined #initialize, and it may accept arguments thusly:
95
- # * zero args. Nothing will be passed to #initialize
96
- # * single arg. The component_map will be passed.
97
- # * var args (eg, def initialize(*args)). args[0] will be the component map. NO OTHER ARGS WILL BE PASSED. See Footnote a)
98
- #
99
- klass.meta_def :new do |component_map|
100
- # We only want to do the following one time, but we've waited until now
101
- # in order to make sure our metaprogramming didn't get ahead of the user's
102
- # own definition of initialize:
103
- unless object_context_prep[:initialize_has_been_wrapped]
104
- # Define a new wrapper'd version of initialize that accepts and uses a component map
105
- init_alias = "original_#{self.name}_initialize".to_sym
106
- alias_method init_alias, :initialize
107
- class_def :initialize do |component_map|
108
- if self.class.has_object_definition?
109
- # Apply the given components
110
- set_components component_map
111
- else
112
- raise "#{self.class} has an ancestor that uses construct_with, but has not declared any component dependencies. Will not be able to instantiate!"
113
- end
114
-
115
- # Invoke the normal initialize method.
116
- # User-defined initialize method may accept 0 args, or it may accept a single arg
117
- # which will be the component map.
118
- arg_count = method(init_alias).arity
119
- case arg_count
120
- when 0
121
- self.send init_alias
122
-
123
- when 1, -1 # See Footnote a) at the bottom of this file
124
- self.send init_alias, component_map
125
-
126
- else
127
- # We're not equipped to handle this
128
- raise "User-defined initialize method defined with #{arg_count} parameters; must either be 0, other wise 1 or -1 (varargs) to receive the component map."
129
- end
130
- end
131
- # Make a note that the initialize wrapper has been applied
132
- object_context_prep[:initialize_has_been_wrapped] = true
133
- end
90
+ arg_count = obj.method(:initialize).arity
91
+ case arg_count
92
+ when 0
93
+ obj.send :initialize
94
+
95
+ when 1, -1 # See Footnote a) at the bottom of this file
96
+ obj.send :initialize, component_map
134
97
 
135
- # Instantiate an instance
136
- actual_new component_map
98
+ else
99
+ # We're not equipped to handle this
100
+ raise "User-defined initialize method defined with #{arg_count} parameters; must either be 0, other wise 1 or -1 (varargs) to receive the component map."
137
101
  end
102
+
103
+ obj
104
+
138
105
  end
139
106
  end
140
107
 
@@ -1,3 +1,3 @@
1
1
  module Conject
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/scratch/car.rb ADDED
@@ -0,0 +1,18 @@
1
+ class Car
2
+ construct_with :engine, :chassis
3
+ def initialize
4
+ # puts "(car being constructed with #{engine} and #{chassis})#{caller.join("\n")}"
5
+ puts "(car being constructed with #{engine} and #{chassis})"
6
+ end
7
+ def to_s
8
+ "Car has #{engine} and #{chassis}"
9
+ end
10
+ end
11
+
12
+ class Engine
13
+ def to_s; "Engine!"; end
14
+ end
15
+
16
+ class Chassis
17
+ def to_s; "Chassis!"; end
18
+ end
@@ -0,0 +1,81 @@
1
+ $: << File.expand_path(File.dirname(__FILE__) + "/../lib")
2
+ require 'conject/extended_metaid.rb'
3
+
4
+ # module CJ
5
+ # class DepDef
6
+ # attr_reader :object_name, :constructor_time
7
+ #
8
+ # def initialize(opts)
9
+ # @object_name = opts[:object_name]
10
+ # @constructor_time = opts[:constructor_time] || false
11
+ # end
12
+ #
13
+ # def constructor_time?
14
+ # @constructor_time
15
+ # end
16
+ # end
17
+ # end
18
+
19
+ class Class
20
+ def construct_with(*object_names)
21
+ klass = self
22
+ @_conject_construct_with = object_names
23
+
24
+ self.
25
+ object_names.each do |n|
26
+ self.class_def_private n do
27
+ components[n]
28
+ end
29
+ end
30
+
31
+
32
+ # @dep_defs = object_names.map do |x| CJ::DepDef.new(object_name: x, constructor_time: true) end
33
+
34
+ end
35
+
36
+ def constructor_dependencies
37
+ @_conject_construct_with
38
+ end
39
+
40
+ # def self.new
41
+ # puts "?"
42
+ # end
43
+ end
44
+
45
+ class Class
46
+ def new(*a)
47
+ puts "?"
48
+ obj = allocate
49
+ obj.initialize
50
+ obj
51
+ end
52
+ # raise "No!"
53
+ end
54
+
55
+ class Car
56
+ construct_with :engine, :chassis
57
+ def initialize
58
+ puts "Car#initialize"
59
+ end
60
+
61
+ def hi
62
+ puts "hi"
63
+ end
64
+ end
65
+
66
+ class Engine
67
+ end
68
+
69
+ class Chassis
70
+ end
71
+
72
+ c = Car.new
73
+ c.hi
74
+
75
+
76
+ # class Dog
77
+ # def initialize
78
+ # puts "Dog initialize"
79
+ # end
80
+ # end
81
+ #
@@ -0,0 +1,20 @@
1
+ here = File.expand_path(File.dirname(__FILE__))
2
+ $: << here + "/../lib"
3
+
4
+ require 'conject'
5
+
6
+ load here + "/car.rb"
7
+
8
+ # puts Car.respond_to?(:conject_new)
9
+ # puts String.respond_to?(:conject_new)
10
+ #
11
+ context = Conject.default_object_context
12
+ context.configure_objects car: { cache: false }
13
+ car = context[:car]
14
+ puts car
15
+
16
+ load here + "/car.rb"
17
+
18
+ car = context[:car]
19
+ puts car
20
+
@@ -72,7 +72,7 @@ describe "basic inheritance" do
72
72
  attr_accessor :got_map
73
73
  def initialize(map)
74
74
  @got_map = map
75
- super
75
+ super() # YOU GOTTA USE PARENS LIKE THIS TO INDICATE THAT YOU DON'T INTEND TO PASS ARGS TO SUPER CONSTRUCTOR
76
76
  end
77
77
  end
78
78
 
@@ -95,7 +95,7 @@ describe "basic inheritance" do
95
95
  attr_accessor :legs
96
96
  def initialize
97
97
  @legs = 4
98
- super :whoa_there # superclass has a Conjected #initialize which requires the component map arg, though it's not used. Ugh.
98
+ super
99
99
  end
100
100
  end
101
101
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conject
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-17 00:00:00.000000000 Z
12
+ date: 2013-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -106,14 +106,17 @@ files:
106
106
  - lib/conject/version.rb
107
107
  - rake_tasks/rspec.rake
108
108
  - scratch/arity_funny_business_in_different_ruby_versions.rb
109
+ - scratch/car.rb
109
110
  - scratch/class_singletons.rb
110
111
  - scratch/depends_on_spike.rb
112
+ - scratch/different_way.rb
111
113
  - scratch/donkey_fail.rb
112
114
  - scratch/donkey_journey.rb
113
115
  - scratch/go.rb
114
116
  - scratch/metaid.rb
115
117
  - scratch/namespace_support.rb
116
118
  - scratch/object_definition.rb
119
+ - scratch/reloading.rb
117
120
  - scratch/sample.rb
118
121
  - scratch/special_construct.rb
119
122
  - spec/acceptance/dev/README
@@ -198,7 +201,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
198
201
  version: '0'
199
202
  segments:
200
203
  - 0
201
- hash: -606989410081679107
204
+ hash: 4140830812471281535
202
205
  required_rubygems_version: !ruby/object:Gem::Requirement
203
206
  none: false
204
207
  requirements:
@@ -207,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
210
  version: '0'
208
211
  segments:
209
212
  - 0
210
- hash: -606989410081679107
213
+ hash: 4140830812471281535
211
214
  requirements: []
212
215
  rubyforge_project:
213
216
  rubygems_version: 1.8.24