reality-core 1.2.0 → 1.3.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: bffb706bbea3e0adc8b535cf3a366da837396890
4
- data.tar.gz: 8a417de663f22b482076b09ebbea0c0f49cc16c2
3
+ metadata.gz: 6ec01d2c7db8b2a8a50d462accc1cd9d0087c0a4
4
+ data.tar.gz: da5c9751c965418db64296601a89017333d8a593
5
5
  SHA512:
6
- metadata.gz: 9710d6a98541d66f4038b178b12d080a7202f1ca0a44f654a9321003a3de00daea3545633d9143af051d566e13c2f750c2b02b1174728dea1eba14f4b943c2fc
7
- data.tar.gz: 2cf3e9ffe3578737acd8551921ea44a0dc1c9e08a6f41b471e2f0379dacb05afe0f387dbf0ab9af545ec2696c6072a0824ecf2078919c84ba1fd62efcb3024eb
6
+ metadata.gz: e040f80ee44f23428bc990edede6d817c966e706d6c1858e45b0219e38a8f69fda3e94c60977d99d10713c24b193041e5aa7115f5e20c56be210d19b06c95660
7
+ data.tar.gz: c2deb93199c2343cf0bbd2ce7ce7caee40c75381cd166c8ccc16e83b2ccb53bae50c767847f5fb10adba860b201aab7880518249b13da4244d953a966fafaf76
@@ -36,4 +36,58 @@ module Reality #nodoc
36
36
  end
37
37
  end
38
38
  end
39
+
40
+ def self.base_element(options = {})
41
+ type = Class.new(BaseElement)
42
+
43
+ parent_key = options[:parent_key]
44
+ pre_config_code = options[:pre_config_code]
45
+ has_name = !!options[:name]
46
+ has_key = !!options[:key]
47
+
48
+ code = ''
49
+
50
+ parameters = []
51
+ initializers = ''
52
+
53
+ if parent_key
54
+ code += "attr_reader :#{parent_key}\n"
55
+ parameters << parent_key
56
+ initializers += <<-RUBY
57
+ @#{parent_key} = #{parent_key}
58
+ raise "Nil #{parent_key} parameter passed to #{self.class} instance" if @#{parent_key}.nil?
59
+ RUBY
60
+ end
61
+
62
+ if has_key
63
+ code += "attr_reader :key\n"
64
+ parameters << 'key'
65
+ initializers += <<-RUBY
66
+ @key = key
67
+ raise "Nil key parameter passed to #{self.class} instance" if @key.nil?
68
+ RUBY
69
+ end
70
+
71
+ if has_name
72
+ code += "attr_reader :name\n"
73
+ parameters << 'name'
74
+ initializers += <<-RUBY
75
+ @name = name
76
+ raise "Nil name parameter passed to #{self.class} instance" if @name.nil?
77
+ RUBY
78
+ end
79
+
80
+
81
+ parameters = parameters.join(', ')
82
+ parameters += ', ' if parameters.size > 0
83
+ code += <<RUBY
84
+ def initialize(#{parameters}options = {}, &block)
85
+ #{initializers}
86
+ #{pre_config_code}
87
+ super(options, &block)
88
+ end
89
+ RUBY
90
+ type.class_eval(code)
91
+ type
92
+ end
39
93
  end
data/reality-core.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{reality-core}
5
- s.version = '1.2.0'
5
+ s.version = '1.3.0'
6
6
  s.platform = Gem::Platform::RUBY
7
7
 
8
8
  s.authors = ['Peter Donald']
@@ -33,4 +33,72 @@ class Reality::TestBaseElement < Reality::TestCase
33
33
  TestElement.new(:x => '1')
34
34
  end
35
35
  end
36
+
37
+ class TestElementA < Reality.base_element
38
+ end
39
+
40
+ class TestElementB < Reality.base_element(:parent_key => 'container')
41
+ end
42
+
43
+ class TestElementC < Reality.base_element(:name => true)
44
+ end
45
+
46
+ class TestElementD < Reality.base_element(:key => true)
47
+ end
48
+
49
+ class TestElementE < Reality.base_element(:name => true, :key => true)
50
+ end
51
+
52
+ class TestElementF < Reality.base_element(:parent_key => 'container', :name => true, :key => true)
53
+ end
54
+
55
+ class TestElementG < Reality.base_element(:pre_config_code => 'self.foo = 1')
56
+ attr_accessor :foo
57
+ end
58
+
59
+ def test_base_element_constructor
60
+ begin
61
+ e = TestElementA.new
62
+ assert_raise(NoMethodError) { e.key }
63
+ assert_raise(NoMethodError) { e.name }
64
+ end
65
+
66
+ begin
67
+ e = TestElementB.new('FakeContainer')
68
+ assert_raise(NoMethodError) { e.key }
69
+ assert_raise(NoMethodError) { e.name }
70
+ assert_equal e.container, 'FakeContainer'
71
+ end
72
+
73
+ begin
74
+ e = TestElementC.new('myName')
75
+ assert_raise(NoMethodError) { e.key }
76
+ assert_equal e.name, 'myName'
77
+ end
78
+
79
+ begin
80
+ e = TestElementD.new('myKey')
81
+ assert_raise(NoMethodError) { e.name }
82
+ assert_equal e.key, 'myKey'
83
+ end
84
+
85
+ begin
86
+ e = TestElementE.new('myKey', 'myName')
87
+ assert_equal e.key, 'myKey'
88
+ assert_equal e.name, 'myName'
89
+ end
90
+
91
+ begin
92
+ e = TestElementF.new('FakeContainer', 'myKey', 'myName')
93
+ assert_equal e.container, 'FakeContainer'
94
+ assert_equal e.key, 'myKey'
95
+ assert_equal e.name, 'myName'
96
+ end
97
+
98
+ begin
99
+ TestElementG.new do |e|
100
+ assert_equal e.foo, 1
101
+ end
102
+ end
103
+ end
36
104
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reality-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.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-11-30 00:00:00.000000000 Z
11
+ date: 2016-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest