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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e3cc5b79ca0d36340e35381ab51d1b4d09486812bb8479267128a557e0130c01
4
- data.tar.gz: b8ff810f7317108cbf1b8ef7290355041a8d29d20ecb3a57ad47f66fd3edc2f2
3
+ metadata.gz: cb09e609fc85c6ee5944802fc722aad61fea684980dd9bb31e076a8a3a9c898d
4
+ data.tar.gz: 74438aa9e3f63e36183857d15a1a9b7434107aa66b27e951c494cb59a51f4bdd
5
5
  SHA512:
6
- metadata.gz: 6044d7ac29c9b23834aa7a848dc10c11430be05262bcaab38ad5417ee7a605d515a1d66bce01f6ff639750e566826856e39ed4aa46a33ffb26f36b71011970bb
7
- data.tar.gz: 3a94d368fc0e022e2aaffccfb10247cfab48a7df9da53150a7a618b21cac7c529dbe906cd8c36169831dd499a9db93e586947147528ec4d17aba67260eb15341
6
+ metadata.gz: 7e76f03bae73c853c82c13e6f6358529748435c669d47b2581fe269908b69972ae8fad5ee1e8bcb13de6a3868f94257cc55ca44d035a600966692cb8cfef8708
7
+ data.tar.gz: 7bef9aa465dbc4f2a73ba129a2c86037e8d2baa66aa7f98b8a836a7fd7b4872d00b8f3f2d311ec2d0105fd7664ba3556d4cb722eee7f4327086ff3b0e09d7124
@@ -1,23 +1,21 @@
1
1
  module Modeling
2
2
  class ModelField
3
- def initialize name, filter, create_attr, writer, reader, tester
3
+ def initialize name, instance_variable, writer, reader, tester
4
4
  @name = name
5
- @filter = filter
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 attribute_name
13
+ def instance_variable_name
16
14
  "@#{name}"
17
15
  end
18
16
 
19
- def create_attr?
20
- @create_attr
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
- from_symbol argument, filter
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 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")
66
- else
67
- raise "Invalid argument string format '#{argument}'"
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
@@ -4,36 +4,29 @@ module Modeling
4
4
 
5
5
  attr_accessor :model_fields
6
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 }
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 |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?
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 symbol
19
- define_method "#{symbol}?" do
20
- !!instance_variable_get(symbol)
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 |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]
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
- super_call = proc do |*asc, **nasc, &bsc|
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 |f|
52
- instance_variable_set f.attribute_name, model_arguments[f.name] if f.create_attr?
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 super_call, **model_arguments, &initializer
50
+ instance_exec super_initialize, **model_arguments, &initializer
56
51
  else
57
- super_call.call
52
+ super_initialize.call
58
53
  end
59
54
  end
60
55
  end
61
-
62
56
  end
@@ -1,3 +1,3 @@
1
1
  module Modeling
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
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.5
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-04-20 00:00:00.000000000 Z
11
+ date: 2024-11-11 00:00:00.000000000 Z
12
12
  dependencies: []
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"
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.6
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