greenhouse-ioc 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a6c353f40dd8a1449529f20fbc1cb9e1217764a
4
+ data.tar.gz: d33c4c4e3c8050584eaf4d7f721ab447ece62b39
5
+ SHA512:
6
+ metadata.gz: 4eaf74d194febe5ff049a8ca716d03ad4c8e1fa92cb6bfe71bf98e9108553f65bf26152e132819ad8b9e77112e3eb75bda2469e211d399af23f555e779ed571e
7
+ data.tar.gz: b1e76ac902d4a6094c34b18e7d441043c49b1473ebe145f746e944ec9f6cc7f37641e9e9a10a2ed2748d03d41a9debaa76f47553218a7fea75a0af2f2372265e
@@ -0,0 +1,6 @@
1
+ module GreenhouseIoc
2
+
3
+ autoload :VERSION, 'greenhouse_ioc/version'
4
+ autoload :Kernel, 'greenhouse_ioc/kernel'
5
+
6
+ end
@@ -0,0 +1,102 @@
1
+ module GreenhouseIoc
2
+
3
+ class Kernel
4
+
5
+ def initialize
6
+
7
+ create_components
8
+ do_ioc_wiring
9
+
10
+ end
11
+
12
+ def create_components
13
+
14
+ # List all 'ioc' properties
15
+ ioc_properties=ioc_properties self
16
+
17
+ #
18
+ # Create class instance for each 'IoC' property
19
+ # and assign to 'IoC' property
20
+ #
21
+ # NOTE: Naming convention in use: e.g. setIoc_game_state: -> GameState.new
22
+ ioc_properties.each do |setter_method_name|
23
+ property_name=setter_method_name.gsub("setIoc_","").gsub(":","")
24
+
25
+ # turn 'game_state' into 'GameState'
26
+ # note: requires 'bubble-wrap' gem
27
+ #class_name=property_name.camelize
28
+ # note: doesn't require 'bubble-wrap' gem
29
+ class_name=property_name.split('_').map {|w| w.capitalize}.join
30
+
31
+ # create instance of the component (GameState.new)
32
+ c=Object.const_get(class_name)
33
+
34
+ begin
35
+ property_value=c.new
36
+ # assign instance of the component to 'IoC' property
37
+ self.send("#{setter_method_name}",property_value)
38
+ rescue
39
+ puts "ERROR: Greenhouse: Unable to create instance of #{class_name}. Is a no-argument initializer provided?"
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ def do_ioc_wiring
47
+
48
+ # List all 'IoC' properties
49
+ ioc_properties=ioc_properties self
50
+
51
+ #
52
+ # Extract component names from 'IoC' property names
53
+ # e.g. setIoc_game_state -> game_state
54
+ components=ioc_properties.map{|e| e.gsub("setIoc_","ioc_").gsub(":","")}
55
+
56
+ #
57
+ # Wire each 'IoC' component with
58
+ # all required IoC dependencies
59
+ components.each{|c| wire_component self.send(c)}
60
+
61
+ end
62
+
63
+ def wire_component(component)
64
+
65
+ # List all 'IoC' properties of the component
66
+ ioc_properties=ioc_properties component
67
+
68
+ #
69
+ # Wire each 'IoC' property with instance of a component
70
+ # NOTE: Naming convention in use: e.g. setIoc_game_state: -> instance of GameState
71
+ ioc_properties.each do |setter_method_name|
72
+ property_name=setter_method_name.gsub("setIoc_","").gsub(":","")
73
+ ioc_property_name = "ioc_#{property_name}"
74
+ property_value=nil
75
+ begin
76
+ property_value=self.send(ioc_property_name)
77
+ rescue NoMethodError=>e
78
+ puts "ERROR: Greenhouse: Instance of '#{property_name.camelize}' class is not assigned to #{ioc_property_name}'."
79
+ end
80
+
81
+ if property_value
82
+ begin
83
+ component.send("#{setter_method_name}",property_value)
84
+ rescue NoMethodError=>e
85
+ puts "ERROR: Greenhouse: Unable to set '#{ioc_property_name}' property on '#{component.class}' component. No such method."
86
+ end
87
+ end
88
+
89
+ end
90
+ end
91
+
92
+ def ioc_properties(ioc_object)
93
+ ioc_object.methods.select{|e| setter_method_name=e.to_s; setter_method_name.start_with?"setIoc_" and setter_method_name.end_with?":" }
94
+ end
95
+
96
+ def all_ioc_component_names
97
+ ioc_properties self
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -0,0 +1,5 @@
1
+ module GreenhouseIoc
2
+
3
+ VERSION = "0.0.1"
4
+
5
+ end
@@ -0,0 +1,13 @@
1
+ require 'test/unit'
2
+ require 'greenhouse-ioc'
3
+
4
+ class KernelTest < Test::Unit::TestCase
5
+
6
+ def test_can_initialize
7
+
8
+ result=GreenhouseIoc::Kernel.new
9
+ assert_not_nil result
10
+
11
+ end
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: greenhouse-ioc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Maciukiewicz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby's implementation of Inversion of Control pattern done in a neat
14
+ way.
15
+ email: mm@csquirrel.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/greenhouse-ioc.rb
21
+ - lib/greenhouse_ioc/kernel.rb
22
+ - lib/greenhouse_ioc/version.rb
23
+ - test/test_kernel.rb
24
+ homepage: https://github.com/cSquirrel/greenhouse-ioc
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project: nowarning
44
+ rubygems_version: 2.0.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Greenhouse - Inversion of Control
48
+ test_files:
49
+ - test/test_kernel.rb