reality-facets 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 943e935de008b8a29ef6a1b73342e4f87e4526d7
4
- data.tar.gz: 675f3d593911cbca6b8c94b24fe54118bfaf2013
3
+ metadata.gz: d17f63aa3fd221512dc2cf8fb3305c70a33f18df
4
+ data.tar.gz: 0e66691871d7376d787d10eed9bf6d84f28ed982
5
5
  SHA512:
6
- metadata.gz: a76f2d59374464b3c86311951ce66cd8a33575b29e8d379a4fbdc32530832e9ca169bd7b85e57a6a0a66c62249d4b37a7b791a2731c319417f7a729671e18989
7
- data.tar.gz: 00a56c862ea83d52db49a307e66f4aca086dfb402bd2f49d7a9f4b711bb2009adc8c62ea14b481bd87fa499b6e105d6a4e2063f1730fa85f490e334969c0e62c
6
+ metadata.gz: b39725276dde3d9d2cd95068d597525f5e9c92e9836087f8af4bdae090d642f230c8699c99b957278cc6dd4fcfc34a9088a97fd1bc9cbf6e5f8b9693aa79b3ba
7
+ data.tar.gz: f06caabcc236eb7899f1eac6aa95fd11535689d14eefc4a6dd337996b7dfdab85365ea0ab4c0b40b69243f4ee6e21bf9505ed8ff4301a65dfda70e64859f126f
@@ -27,6 +27,7 @@ module Reality #nodoc
27
27
  @facet_container = facet_container
28
28
  @required_facets = []
29
29
  @suggested_facets = []
30
+ @model_extension_instances = {}
30
31
  facet_container.send :register_facet, self
31
32
  super(options, &block)
32
33
  facet_container.target_manager.targets.each do |target|
@@ -51,13 +52,18 @@ module Reality #nodoc
51
52
  end
52
53
  end
53
54
 
55
+ def enhanced?(model_class)
56
+ !!@model_extension_instances[model_class]
57
+ end
58
+
54
59
  def enhance(model_class, &block)
55
60
  target_manager = facet_container.target_manager
56
61
  target = target_manager.target_by_model_class(model_class)
57
62
 
58
- extension_name = "#{::Reality::Naming.pascal_case(self.key)}#{model_class.name.gsub(/^.*\:\:([^\:]+)/, '\1')}Facet"
59
- definitions = target_manager.container.facet_definitions
60
- definitions.class_eval(<<-RUBY)
63
+ if @model_extension_instances[model_class].nil?
64
+ extension_name = "#{::Reality::Naming.pascal_case(self.key)}#{model_class.name.gsub(/^.*\:\:([^\:]+)/, '\1')}Facet"
65
+ definitions = target_manager.container.facet_definitions
66
+ definitions.class_eval(<<-RUBY)
61
67
  class #{extension_name} < Reality.base_element(:container_key => :#{target.inverse_access_method})
62
68
  def facet_key
63
69
  :#{self.key}
@@ -79,21 +85,21 @@ class #{extension_name} < Reality.base_element(:container_key => :#{target.inver
79
85
  self.#{target.inverse_access_method}
80
86
  end
81
87
  end
82
- RUBY
83
- extension_instance = definitions.const_get(extension_name)
84
- extension_instance.class_eval(&block) if block_given?
85
-
86
- model_extension = target.extension_module
87
- model_extension.class_eval <<-RUBY
88
- def #{self.key}
89
- self.facet_#{self.key}
90
- end
88
+ RUBY
89
+ @model_extension_instances[model_class] = definitions.const_get(extension_name)
91
90
 
92
- def facet_#{self.key}
93
- Reality::Facets.error("Attempted to access '#{self.key}' facet for model '#{model_class.name}' when facet disabled.") unless #{self.key}?
94
- @facet_#{self.key} ||= #{extension_instance.name}.new(self)
95
- end
96
- RUBY
91
+ target.extension_module.class_eval <<-RUBY
92
+ def #{self.key}
93
+ self.facet_#{self.key}
94
+ end
95
+
96
+ def facet_#{self.key}
97
+ Reality::Facets.error("Attempted to access '#{self.key}' facet for model '#{model_class.name}' when facet disabled.") unless #{self.key}?
98
+ @facet_#{self.key} ||= #{@model_extension_instances[model_class].name}.new(self)
99
+ end
100
+ RUBY
101
+ end
102
+ @model_extension_instances[model_class].class_eval(&block) if block_given?
97
103
  end
98
104
  end
99
105
  end
@@ -19,17 +19,29 @@ module Reality #nodoc
19
19
  base.class_eval 'module FacetDefinitions; end'
20
20
  end
21
21
 
22
- def facet?(key)
23
- facet_by_name?(key)
22
+ # An array of modules that should be mixed in to every extension object
23
+ def facet_extensions
24
+ facet_extension_list.dup
24
25
  end
25
26
 
26
- def facet_by_name?(key)
27
- !!facet_map[key.to_s]
27
+ # Add a ruby module that will be applied to all extension objects
28
+ def facet_extension(extension)
29
+ Facets.error("Attempting to define facet extension #{extension} after facet manager is locked") if locked?
30
+ facet_extension_list << extension
31
+ target_manager.lock!
28
32
  end
29
33
 
30
- def facet_by_name(key)
31
- facet = facet_map[key.to_s]
32
- Facets.error("Unknown facet '#{key}'") unless facet
34
+ def facet?(name)
35
+ facet_by_name?(name)
36
+ end
37
+
38
+ def facet_by_name?(name)
39
+ !!facet_map[name.to_s]
40
+ end
41
+
42
+ def facet_by_name(name)
43
+ facet = facet_map[name.to_s]
44
+ Facets.error("Unknown facet '#{name}'") unless facet
33
45
  facet
34
46
  end
35
47
 
@@ -48,6 +60,15 @@ module Reality #nodoc
48
60
  facet_map.values
49
61
  end
50
62
 
63
+ def lock!
64
+ @locked = true
65
+ apply_facet_extensions
66
+ end
67
+
68
+ def locked?
69
+ !!(@locked ||= nil)
70
+ end
71
+
51
72
  def target_manager
52
73
  @target_manager ||= Reality::Facets::TargetManager.new(self)
53
74
  end
@@ -73,35 +94,35 @@ module Reality #nodoc
73
94
  results
74
95
  end
75
96
 
76
- def activate_facet(object, facet_key)
77
- return if object.facet_enabled?(facet_key)
97
+ def activate_facet(object, facet_name)
98
+ return if object.facet_enabled?(facet_name)
78
99
 
79
- facet = facet_by_name(facet_key)
100
+ facet = facet_by_name(facet_name)
80
101
  facet.required_facets.each do |required_facet_key|
81
102
  activate_facet(object, required_facet_key)
82
103
  end
83
104
  facet.suggested_facets.each do |suggested_facet_key|
84
105
  activate_facet(object, suggested_facet_key)
85
106
  end
86
- object.send(:"_enable_facet_#{facet_key}!")
107
+ object.send(:"_enable_facet_#{facet_name}!")
87
108
 
88
109
  each_contained_model_object(object) do |child|
89
- activate_facet(child, facet_key)
110
+ activate_facet(child, facet_name)
90
111
  end
91
112
  end
92
113
 
93
- def deactivate_facet(object, facet_key)
94
- return unless object.facet_enabled?(facet_key)
114
+ def deactivate_facet(object, facet_name)
115
+ return unless object.facet_enabled?(facet_name)
95
116
  each_contained_model_object(object) do |child|
96
- deactivate_facet(child, facet_key)
117
+ deactivate_facet(child, facet_name)
97
118
  end
98
119
 
99
120
  facets.each do |facet|
100
- if facet.required_facets.include?(facet_key)
121
+ if facet.required_facets.include?(facet_name)
101
122
  deactivate_facet(object, facet.key)
102
123
  end
103
124
  end
104
- object.send(:"_disable_facet_#{facet_key}!")
125
+ object.send(:"_disable_facet_#{facet_name}!")
105
126
  end
106
127
 
107
128
  def extension_point(object, action)
@@ -111,13 +132,13 @@ module Reality #nodoc
111
132
  Facets.debug "Running '#{action}' hook on #{object.class} #{name}"
112
133
  object.send(action)
113
134
  end
114
- object.enabled_facets.each do |facet_key|
135
+ object.enabled_facets.each do |facet_name|
115
136
  # Need to check for the magic facet_X method rather than X method directly as
116
137
  # sometimes there is a global method of the same name.
117
- method_name = "facet_#{facet_key}"
138
+ method_name = "facet_#{facet_name}"
118
139
  extension_object = object.respond_to?(method_name) ? object.send(method_name) : nil
119
140
  if extension_object && extension_object.respond_to?(action, true)
120
- Facets.debug "Running '#{action}' hook on #{facet_key} facet of #{object.class} #{name}"
141
+ Facets.debug "Running '#{action}' hook on #{facet_name} facet of #{object.class} #{name}"
121
142
  extension_object.send(action)
122
143
  end
123
144
  end
@@ -145,8 +166,27 @@ module Reality #nodoc
145
166
  true
146
167
  end
147
168
 
169
+ def apply_facet_extensions
170
+ facet_extension_list.each do |extension|
171
+ facet_map.values.each do |facet|
172
+ target_manager.targets.each do |target|
173
+ if facet.enhanced?(target.model_class)
174
+ facet.enhance(target.model_class) do
175
+ include extension
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ def facet_extension_list
184
+ @facet_extensions ||= []
185
+ end
186
+
148
187
  def register_facet(facet)
149
- target_manager.lock_targets
188
+ target_manager.lock!
189
+ Facets.error("Attempting to define facet #{facet.key} after facet manager is locked") if locked?
150
190
  Facets.error("Attempting to redefine facet #{facet.key}") if facet_map[facet.key.to_s]
151
191
  facet_map[facet.key.to_s] = facet
152
192
  end
@@ -75,6 +75,7 @@ module Reality #nodoc
75
75
 
76
76
  def initialize(container)
77
77
  @container = container
78
+ @locked = false
78
79
  end
79
80
 
80
81
  def is_target_valid?(key)
@@ -107,6 +108,7 @@ module Reality #nodoc
107
108
  end
108
109
 
109
110
  def apply_extension(model)
111
+ container.lock!
110
112
  self.target_by_model_class(model.class).apply_extension_to(model)
111
113
  end
112
114
 
@@ -118,19 +120,23 @@ module Reality #nodoc
118
120
  target_map.values.select { |target| target.container_key == container_key }
119
121
  end
120
122
 
121
- def lock_targets
122
- @targets_locked = true
123
+ def lock!
124
+ @locked = true
125
+ end
126
+
127
+ def locked?
128
+ !!@locked
123
129
  end
124
130
 
125
131
  def reset_targets
126
132
  target_map.clear
127
- @targets_locked = false
133
+ @locked = false
128
134
  end
129
135
 
130
136
  private
131
137
 
132
138
  def register_target(target)
133
- Reality::Facets.error("Attempting to define target #{target.key} when targets have been locked.") if (@targets_locked ||= false)
139
+ Reality::Facets.error("Attempting to define target #{target.key} when targets have been locked.") if locked?
134
140
  Reality::Facets.error("Attempting to redefine target #{target.key}") if target_map[target.key]
135
141
  target_map[target.key] = target
136
142
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{reality-facets}
5
- s.version = '1.5.0'
5
+ s.version = '1.6.0'
6
6
  s.platform = Gem::Platform::RUBY
7
7
 
8
8
  s.authors = ['Peter Donald']
@@ -31,10 +31,10 @@ class Reality::Facets::TestFacet < Reality::TestCase
31
31
  :access_method => :comps,
32
32
  :inverse_access_method => :comp)
33
33
 
34
- Reality::Facets::Facet.new(TestFacetContainer, :gwt)
34
+ facet_gwt = Reality::Facets::Facet.new(TestFacetContainer, :gwt)
35
35
  Reality::Facets::Facet.new(TestFacetContainer, :gwt_rpc, :required_facets => [:gwt])
36
36
 
37
- Reality::Facets::Facet.new(TestFacetContainer, :imit, :suggested_facets => [:gwt_rpc]) do |f|
37
+ facet_imit = Reality::Facets::Facet.new(TestFacetContainer, :imit, :suggested_facets => [:gwt_rpc]) do |f|
38
38
  f.enhance(Project) do
39
39
  def name
40
40
  "Gwt#{project.name}"
@@ -47,6 +47,11 @@ class Reality::Facets::TestFacet < Reality::TestCase
47
47
  assert_equal true, TestFacetContainer.facet_by_name?(:gwt_rpc)
48
48
  assert_equal true, TestFacetContainer.facet_by_name?(:imit)
49
49
 
50
+ assert_equal false, facet_gwt.enhanced?(Project)
51
+ assert_equal false, facet_gwt.enhanced?(Component)
52
+ assert_equal true, facet_imit.enhanced?(Project)
53
+ assert_equal true, facet_imit.enhanced?(Component)
54
+
50
55
  project = Project.new(:MyProject) do |p|
51
56
  p.enable_facets(:imit)
52
57
  p.component(:MyComponent)
@@ -86,4 +91,61 @@ class Reality::Facets::TestFacet < Reality::TestCase
86
91
  assert_equal component, component.imit.comp
87
92
  assert_equal component, component.imit.parent
88
93
  end
94
+
95
+ class Component2 < Reality.base_element(:name => true, :container_key => :project, :pre_config_code => 'Reality::TestCase::TestFacetContainer.target_manager.apply_extension(self)')
96
+ end
97
+
98
+ class Project2 < Reality.base_element(:name => true, :pre_config_code => 'Reality::TestCase::TestFacetContainer.target_manager.apply_extension(self)')
99
+ def component(name, options = {}, &block)
100
+ component_map[name.to_s] = Component2.new(self, name, options, &block)
101
+ end
102
+
103
+ def components
104
+ component_map.values
105
+ end
106
+
107
+ def component_map
108
+ @component_map ||= {}
109
+ end
110
+ end
111
+
112
+ module MyExtensionModule
113
+ def hello_message
114
+ 'yo'
115
+ end
116
+ end
117
+
118
+ def test_common_extension_modules
119
+
120
+ assert_equal false, TestFacetContainer.facet_by_name?(:gwt)
121
+
122
+ TestFacetContainer.target_manager.target(Project2, :project)
123
+ TestFacetContainer.target_manager.target(Component2,
124
+ :component,
125
+ :project)
126
+
127
+
128
+ assert_equal [], TestFacetContainer.facet_extensions
129
+ TestFacetContainer.facet_extension(MyExtensionModule)
130
+ assert_equal [MyExtensionModule], TestFacetContainer.facet_extensions
131
+
132
+ Reality::Facets::Facet.new(TestFacetContainer, :gwt) do |f|
133
+ f.enhance(Project2) do
134
+ def name
135
+ "Gwt#{project.name}"
136
+ end
137
+ end
138
+ end
139
+
140
+ project = Project2.new(:MyProject) do |p|
141
+ p.enable_facets(:gwt)
142
+ p.component(:MyComponent)
143
+ end
144
+ component = project.components[0]
145
+
146
+ assert_equal true, project.gwt?
147
+ assert_equal 'GwtMyProject', project.gwt.name
148
+ assert_equal 'yo', project.gwt.hello_message
149
+ assert_equal false, component.respond_to?(:gwt)
150
+ end
89
151
  end
@@ -7,6 +7,9 @@ class Reality::Facets::TestFacetContainer < Reality::TestCase
7
7
  class Component2 < Reality.base_element(:name => true)
8
8
  end
9
9
 
10
+ module MyExtensionModule
11
+ end
12
+
10
13
  def test_basic_operation
11
14
 
12
15
  assert_equal false, TestFacetContainer.facet_by_name?(:gwt)
@@ -36,6 +39,10 @@ class Reality::Facets::TestFacetContainer < Reality::TestCase
36
39
  assert_equal %w(gwt), TestFacetContainer.facet_keys
37
40
  assert_equal 1, TestFacetContainer.facets.size
38
41
 
42
+ assert_equal [], TestFacetContainer.facet_extensions
43
+ TestFacetContainer.facet_extension(MyExtensionModule)
44
+ assert_equal [MyExtensionModule], TestFacetContainer.facet_extensions
45
+
39
46
  assert_facet_error("Unknown facet 'gwt_rpc'") { TestFacetContainer.facet_by_name(:gwt_rpc) }
40
47
 
41
48
  assert_equal TestFacetContainer, TestFacetContainer.facet_by_name(:gwt).facet_container
@@ -276,4 +276,18 @@ class Reality::Facets::TestFacetedModel < Reality::TestCase
276
276
  assert_equal true, attribute1.jpa.hook2?
277
277
  assert_equal true, attribute2.jpa.hook2?
278
278
  end
279
+
280
+ def test_facet_container_locking
281
+ TestFacetContainer.target_manager.target(Repository, :repository)
282
+
283
+ TestFacetContainer.facet(:json)
284
+
285
+ assert_equal 1, TestFacetContainer.facets.size
286
+
287
+ Repository.new(:MyRepo) do |r|
288
+ TestFacetContainer.target_manager.apply_extension(r)
289
+ end
290
+
291
+ assert_facet_error('Attempting to define facet gwt after facet manager is locked') { TestFacetContainer.facet(:gwt) }
292
+ end
279
293
  end
@@ -193,4 +193,36 @@ class Reality::Facets::TestTargetManager < Reality::TestCase
193
193
 
194
194
  assert_equal [:json], fragment1.enabled_facets
195
195
  end
196
+
197
+ class Project2 < Reality.base_element(:name => true)
198
+ end
199
+
200
+ def test_apply_extension_with_multiple_enhances
201
+ TestFacetContainer.target_manager.target(Project2, :project)
202
+
203
+ TestFacetContainer.facet(:gwt) do |f|
204
+ f.enhance(Project2) do
205
+ def name
206
+ "Gwt#{project.name}"
207
+ end
208
+ end
209
+ f.enhance(Project2) do
210
+ def name2
211
+ "Gwt2#{project.name}"
212
+ end
213
+ end
214
+ end
215
+
216
+ project = Project2.new(:MyProject)
217
+ assert_equal false, project.respond_to?(:facet_enabled?)
218
+ assert_equal false, project.respond_to?(:parent)
219
+
220
+ TestFacetContainer.target_manager.apply_extension(project)
221
+
222
+ assert_equal true, project.respond_to?(:facet_enabled?)
223
+ project.enable_facets(:gwt)
224
+
225
+ assert_equal 'GwtMyProject', project.gwt.name
226
+ assert_equal 'Gwt2MyProject', project.gwt.name2
227
+ end
196
228
  end
data/test/helper.rb CHANGED
@@ -13,7 +13,9 @@ class Reality::TestCase < Minitest::Test
13
13
  class << self
14
14
 
15
15
  def reset
16
+ @locked = false
16
17
  facet_map.clear
18
+ facet_extension_list.clear
17
19
  target_manager.reset_targets
18
20
  TestFacetContainer::FacetDefinitions.constants.each do |constant|
19
21
  TestFacetContainer::FacetDefinitions.send(:remove_const, constant)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reality-facets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Donald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-27 00:00:00.000000000 Z
11
+ date: 2016-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reality-core