modeling 0.0.5 → 0.1.0
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 +4 -4
- data/lib/modeling/model_field.rb +25 -47
- data/lib/modeling/module.rb +22 -28
- data/lib/modeling/version.rb +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb09e609fc85c6ee5944802fc722aad61fea684980dd9bb31e076a8a3a9c898d
|
4
|
+
data.tar.gz: 74438aa9e3f63e36183857d15a1a9b7434107aa66b27e951c494cb59a51f4bdd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e76f03bae73c853c82c13e6f6358529748435c669d47b2581fe269908b69972ae8fad5ee1e8bcb13de6a3868f94257cc55ca44d035a600966692cb8cfef8708
|
7
|
+
data.tar.gz: 7bef9aa465dbc4f2a73ba129a2c86037e8d2baa66aa7f98b8a836a7fd7b4872d00b8f3f2d311ec2d0105fd7664ba3556d4cb722eee7f4327086ff3b0e09d7124
|
data/lib/modeling/model_field.rb
CHANGED
@@ -1,23 +1,21 @@
|
|
1
1
|
module Modeling
|
2
2
|
class ModelField
|
3
|
-
def initialize name,
|
3
|
+
def initialize name, instance_variable, writer, reader, tester
|
4
4
|
@name = name
|
5
|
-
@
|
6
|
-
@create_attr = create_attr
|
5
|
+
@instance_variable = instance_variable
|
7
6
|
@writer = writer
|
8
7
|
@reader = reader
|
9
8
|
@tester = tester
|
10
9
|
end
|
11
10
|
|
12
11
|
attr :name
|
13
|
-
attr :filter
|
14
12
|
|
15
|
-
def
|
13
|
+
def instance_variable_name
|
16
14
|
"@#{name}"
|
17
15
|
end
|
18
16
|
|
19
|
-
def
|
20
|
-
@
|
17
|
+
def instance_variable?
|
18
|
+
@instance_variable
|
21
19
|
end
|
22
20
|
|
23
21
|
def writer?
|
@@ -37,52 +35,32 @@ module Modeling
|
|
37
35
|
case argument
|
38
36
|
when ModelField
|
39
37
|
argument
|
40
|
-
when Symbol
|
41
|
-
|
42
|
-
when String
|
43
|
-
from_string argument, filter
|
44
|
-
when Array
|
45
|
-
from_array argument, filter
|
46
|
-
when Hash
|
47
|
-
from_hash argument
|
38
|
+
when Symbol, String
|
39
|
+
parse_model_field argument
|
48
40
|
else
|
49
|
-
raise "Unsupported argument #{argument}"
|
41
|
+
raise "Unsupported argument #{argument} of #{argument.class} class."
|
50
42
|
end
|
51
43
|
end
|
52
44
|
|
53
|
-
def
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
45
|
+
def parse_model_field argument
|
46
|
+
instance_variable = reader = writer = tester = false
|
47
|
+
if argument.start_with? "@"
|
48
|
+
meta, name = argument.to_s.split "_", 2
|
49
|
+
meta.to_s.each_char do |char|
|
50
|
+
case char
|
51
|
+
when "r", "R" then reader = true
|
52
|
+
when "w", "W" then writer = true
|
53
|
+
when "t", "T" then tester = true
|
54
|
+
when "i", "I" then instance_variable = true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
else
|
58
|
+
name = argument
|
59
|
+
reader = writer = instance_variable = true
|
68
60
|
end
|
61
|
+
raise "Invalid model field #{argument}" unless name =~ /\w+/
|
62
|
+
ModelField.new name.to_sym, instance_variable, writer, reader, tester
|
69
63
|
end
|
70
|
-
|
71
|
-
def from_array argument, filter
|
72
|
-
name = argument.shift.to_sym
|
73
|
-
ModelField.new name, filter, argument.include?(:attr), argument.include?(:writer),
|
74
|
-
argument.include?(:reader), argument.include?(:tester)
|
75
|
-
end
|
76
|
-
|
77
|
-
def from_hash argument
|
78
|
-
name = argument[:name] || argument.keys.first
|
79
|
-
filter = argument[:filter] || argument[name]
|
80
|
-
parsed = parse name, filter
|
81
|
-
ModelField.new parsed.name, parsed.filter, argument[:attr] || parsed.create_attr?,
|
82
|
-
argument[:writer] || parsed.writer?, argument[:reader] || parsed.reader?,
|
83
|
-
argument[:tester] || parsed.tester?
|
84
|
-
end
|
85
|
-
|
86
64
|
end
|
87
65
|
end
|
88
66
|
end
|
data/lib/modeling/module.rb
CHANGED
@@ -4,36 +4,29 @@ module Modeling
|
|
4
4
|
|
5
5
|
attr_accessor :model_fields
|
6
6
|
|
7
|
-
def model *fields,
|
8
|
-
self.model_fields = model_fields = fields.map{ ModelField.parse _1 }
|
9
|
-
filtered_fields.map{ ModelField.parse _1, _2 }
|
7
|
+
def model *fields, &initializer
|
8
|
+
self.model_fields = model_fields = fields.map{ ModelField.parse _1 }
|
10
9
|
define_initialize self, &initializer
|
11
|
-
model_fields.each do |
|
12
|
-
attr_writer
|
13
|
-
attr_reader
|
14
|
-
attr_tester
|
10
|
+
model_fields.each do |field|
|
11
|
+
attr_writer field.name if field.writer?
|
12
|
+
attr_reader field.name if field.reader?
|
13
|
+
attr_tester "#{field.name}?".to_sym, field.instance_variable_name if field.tester?
|
15
14
|
end
|
16
15
|
end
|
16
|
+
|
17
|
+
private
|
17
18
|
|
18
|
-
def attr_tester
|
19
|
-
define_method
|
20
|
-
|
19
|
+
def attr_tester tester, instance_variable
|
20
|
+
define_method tester do
|
21
|
+
instance_variable_get(instance_variable) ? true : false
|
21
22
|
end
|
22
23
|
end
|
23
|
-
|
24
|
-
private
|
25
24
|
|
26
25
|
def self.model_arguments fields, *a, **na
|
27
|
-
fields.zip(a).map do |
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
value = f.filter.from value
|
32
|
-
else
|
33
|
-
raise ArgumentError.new "#{value}(#{value.class}) does not match #{f.filter}"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
[f.name, value]
|
26
|
+
fields.zip(a).map do |field, arg|
|
27
|
+
name = field.name
|
28
|
+
value = na.key?(name) ? na[name] : arg
|
29
|
+
[name, value]
|
37
30
|
end.to_h
|
38
31
|
end
|
39
32
|
|
@@ -41,22 +34,23 @@ module Modeling
|
|
41
34
|
define_method :initialize do |*a, **na, &b|
|
42
35
|
model_fields = initializer_class.model_fields
|
43
36
|
model_arguments = Modeling.model_arguments model_fields, *a, **na
|
44
|
-
|
37
|
+
super_initialize = proc do |*asc, **nasc, &bsc|
|
45
38
|
if asc.empty? && nasc.empty? && !bsc
|
46
39
|
super(*(a[model_arguments.size..] || []), **na.except(*model_arguments.keys), &b)
|
47
40
|
else
|
48
41
|
super(*asc, **nasc, &bsc)
|
49
42
|
end
|
50
43
|
end
|
51
|
-
model_fields.each do |
|
52
|
-
|
44
|
+
model_fields.each do |field|
|
45
|
+
if field.instance_variable?
|
46
|
+
instance_variable_set field.instance_variable_name, model_arguments[field.name]
|
47
|
+
end
|
53
48
|
end
|
54
49
|
if initializer
|
55
|
-
instance_exec
|
50
|
+
instance_exec super_initialize, **model_arguments, &initializer
|
56
51
|
else
|
57
|
-
|
52
|
+
super_initialize.call
|
58
53
|
end
|
59
54
|
end
|
60
55
|
end
|
61
|
-
|
62
56
|
end
|
data/lib/modeling/version.rb
CHANGED
metadata
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modeling
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Łukasz Pomietło
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: "
|
14
|
-
|
15
|
-
initializer arguments filtering.\n"
|
13
|
+
description: " Simplify common class definition by encoding attributes with their
|
14
|
+
access modes.\n"
|
16
15
|
email: oficjalnyadreslukasza@gmail.com
|
17
16
|
executables: []
|
18
17
|
extensions: []
|
@@ -43,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
42
|
- !ruby/object:Gem::Version
|
44
43
|
version: '0'
|
45
44
|
requirements: []
|
46
|
-
rubygems_version: 3.5.
|
45
|
+
rubygems_version: 3.5.23
|
47
46
|
signing_key:
|
48
47
|
specification_version: 4
|
49
48
|
summary: A concise way to define the class shape
|