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 +4 -4
- data/lib/reality/base_element.rb +54 -0
- data/reality-core.gemspec +1 -1
- data/test/test_base_element.rb +68 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ec01d2c7db8b2a8a50d462accc1cd9d0087c0a4
|
4
|
+
data.tar.gz: da5c9751c965418db64296601a89017333d8a593
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e040f80ee44f23428bc990edede6d817c966e706d6c1858e45b0219e38a8f69fda3e94c60977d99d10713c24b193041e5aa7115f5e20c56be210d19b06c95660
|
7
|
+
data.tar.gz: c2deb93199c2343cf0bbd2ce7ce7caee40c75381cd166c8ccc16e83b2ccb53bae50c767847f5fb10adba860b201aab7880518249b13da4244d953a966fafaf76
|
data/lib/reality/base_element.rb
CHANGED
@@ -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
data/test/test_base_element.rb
CHANGED
@@ -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.
|
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
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|