modeling 0.0.2 → 0.0.4
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 +50 -24
- data/lib/modeling/module.rb +32 -11
- 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: 1808a9621f5f4e419a137e8f2cc01c85aebcc29e94f20c270301e1d2dafd6806
|
4
|
+
data.tar.gz: 0d24e7745e3c3e177060bd436856b63adc59c89ca09ff7b01e184fc804cc622c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7728919bd5af78a82bed604551715f2bb4d10c4cf736d232b8d73293e7db99a34ef233dc5308121993073c7b7ca8b9e4287a278ae5e9a798a1bbf2fd8b7281b7
|
7
|
+
data.tar.gz: 76d4cc5dd6006574ad4c225a695c36a6e6886d8ae9a40aa26159e873ce572992db6ff4e45f54b8b463298eaa2195d0d5fbf039a64f30c3bde8156f8bb44ce729
|
data/lib/modeling/model_field.rb
CHANGED
@@ -1,60 +1,86 @@
|
|
1
1
|
module Modeling
|
2
2
|
class ModelField
|
3
|
-
def initialize name,
|
3
|
+
def initialize name, filter, create_attr, writer, reader, tester
|
4
4
|
@name = name
|
5
|
-
@
|
6
|
-
@
|
7
|
-
@
|
5
|
+
@filter = filter
|
6
|
+
@create_attr = create_attr
|
7
|
+
@writer = writer
|
8
|
+
@reader = reader
|
9
|
+
@tester = tester
|
8
10
|
end
|
9
11
|
|
10
12
|
attr :name
|
13
|
+
attr :filter
|
11
14
|
|
12
15
|
def attribute_name
|
13
16
|
"@#{name}"
|
14
17
|
end
|
15
18
|
|
16
|
-
def
|
17
|
-
@
|
19
|
+
def create_attr?
|
20
|
+
@create_attr
|
18
21
|
end
|
19
22
|
|
20
|
-
def
|
21
|
-
@
|
23
|
+
def writer?
|
24
|
+
@writer
|
22
25
|
end
|
23
26
|
|
24
|
-
def
|
25
|
-
@
|
27
|
+
def reader?
|
28
|
+
@reader
|
29
|
+
end
|
30
|
+
|
31
|
+
def tester?
|
32
|
+
@tester
|
26
33
|
end
|
27
34
|
|
28
35
|
class << self
|
29
|
-
def parse argument
|
36
|
+
def parse argument, filter = nil
|
30
37
|
case argument
|
31
38
|
when ModelField
|
32
39
|
argument
|
33
|
-
when String
|
34
|
-
from_string argument
|
35
40
|
when Symbol
|
36
|
-
|
41
|
+
from_symbol argument, filter
|
42
|
+
when String
|
43
|
+
from_string argument, filter
|
37
44
|
when Array
|
38
|
-
from_array argument
|
45
|
+
from_array argument, filter
|
46
|
+
when Hash
|
47
|
+
from_hash argument
|
39
48
|
else
|
40
49
|
raise "Unsupported argument #{argument}"
|
41
50
|
end
|
42
51
|
end
|
43
52
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
53
|
+
def from_symbol argument, filter
|
54
|
+
at = argument.start_with? "@"
|
55
|
+
bang = argument.end_with? "!"
|
56
|
+
hook = argument.end_with? "?"
|
57
|
+
rail = argument.end_with? "="
|
58
|
+
bald = !(at || bang || hook || rail)
|
59
|
+
name = argument.to_s[/\w+/].to_sym
|
60
|
+
ModelField.new name, filter, bald || hook || at || rail, bald || rail, bald || hook, false
|
61
|
+
end
|
62
|
+
|
63
|
+
def from_string argument, filter
|
64
|
+
if argument =~ /(\w+)\s*(:?\/([traw\s]*))/
|
65
|
+
ModelField.new $1.to_sym, filter, $2.include?("a"), $2.include?("w"), $2.include?("r"), $2.include?("t")
|
50
66
|
else
|
51
67
|
raise "Invalid argument string format '#{argument}'"
|
52
68
|
end
|
53
69
|
end
|
54
70
|
|
55
|
-
def from_array argument
|
56
|
-
name = argument.shift
|
57
|
-
ModelField.new name, argument.include?(:
|
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?
|
58
84
|
end
|
59
85
|
|
60
86
|
end
|
data/lib/modeling/module.rb
CHANGED
@@ -2,27 +2,48 @@ require_relative 'model_field'
|
|
2
2
|
|
3
3
|
module Modeling
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
attr_accessor :model_fields
|
6
|
+
|
7
|
+
def model *fields, **filtered_fields, &initializer
|
8
|
+
self.model_fields = model_fields = fields.map{ ModelField.parse _1 } +
|
9
|
+
filtered_fields.map{ ModelField.parse _1, _2 }
|
10
|
+
define_initialize self, &initializer
|
8
11
|
model_fields.each do |f|
|
9
|
-
attr_writer f.name if f.
|
10
|
-
attr_reader f.name if f.
|
12
|
+
attr_writer f.name if f.writer?
|
13
|
+
attr_reader f.name if f.reader?
|
14
|
+
attr_tester f.name if f.tester?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def attr_tester symbol
|
19
|
+
define_method "#{symbol}?" do
|
20
|
+
!!instance_variable_get(symbol)
|
11
21
|
end
|
12
22
|
end
|
13
23
|
|
14
24
|
private
|
15
25
|
|
16
26
|
def self.model_arguments fields, *a, **na
|
17
|
-
fields.zip(a).map
|
27
|
+
fields.zip(a).map do |f, arg|
|
28
|
+
value = na.key?(f.name) ? na[f.name] : arg
|
29
|
+
if !f.filter.nil? && !(f.filter === value)
|
30
|
+
if f.filter.respond_to? :from
|
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]
|
37
|
+
end.to_h
|
18
38
|
end
|
19
39
|
|
20
|
-
def define_initialize
|
40
|
+
def define_initialize initializer_class, &initializer
|
21
41
|
define_method :initialize do |*a, **na, &b|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
42
|
+
model_fields = initializer_class.model_fields
|
43
|
+
model_arguments = Modeling.model_arguments model_fields, *a, **na
|
44
|
+
super(*(a[model_arguments.size..] || []), **na.except(*model_arguments.keys), &b)
|
45
|
+
model_fields.each do |f|
|
46
|
+
instance_variable_set f.attribute_name, model_arguments[f.name] if f.create_attr?
|
26
47
|
end
|
27
48
|
instance_exec **model_arguments, &initializer if initializer
|
28
49
|
end
|
data/lib/modeling/version.rb
CHANGED
metadata
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modeling
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
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-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: "
|
14
|
-
|
15
|
-
|
16
|
-
:first, :second\n\n end\n ```\n ## Check the documentation or home page for more.\n\n"
|
13
|
+
description: " Enables writting class initializers and attribute accessors in one
|
14
|
+
line. \n Struct class alternative. Keyword & positional arguments mixing. Optional
|
15
|
+
initializer arguments filtering.\n"
|
17
16
|
email: oficjalnyadreslukasza@gmail.com
|
18
17
|
executables: []
|
19
18
|
extensions: []
|