modeling 0.0.1 → 0.0.3

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: 984bdb92ad9c4e829cf239f3df9eb1479e41c37d5659944933edb8d69282020f
4
- data.tar.gz: b47547c32079a0d5680d99dd7cca3f8fa5736a2eb56bd9a5bfcab9547aa14687
3
+ metadata.gz: 8ba8acab76eeef9bbc6a79b4eab6c0fe1cb1d02d074ab9f6063fed05a5717bcd
4
+ data.tar.gz: '0971e3332d5bceae49f1a9dcd060ddbc154c446ecc3a9914c4a06b612b2a773a'
5
5
  SHA512:
6
- metadata.gz: b3d8cd4181cf41ea9007cfaf8aa5e4eaa80c2bd68ff65e42a00fd4902590dca277226a18f011f8cb854d2f18791b910042e2b4c2e16e0410c0536253c785fd29
7
- data.tar.gz: e66cc1a8d2c45d361508ea206dcbfb94ed54875d05966b603447d6fe77d122f37d013068e70223de41bb2c777fc7df43a4439f265eb364a904ea648f084d1b54
6
+ metadata.gz: de58c6a741ce4eb22033c2f0ff8307381f35b0a3eb65bb93a18ad3dd91b928978e4134f4d5eff299cf98d8efa1dac902145c59c633fa5ba71c93ab531962471f
7
+ data.tar.gz: aa922c332e3ee9bee58919eda6613918c03e5c87b181b26beeb6386673866e330e59d2f566bdc1a6d8bd9a6e021bfbc9a620ed8ba360a30b0415c7122a979138
@@ -0,0 +1,62 @@
1
+ module Modeling
2
+ class ModelField
3
+ def initialize name, assign, setter, getter
4
+ @name = name
5
+ @assign = assign
6
+ @setter = setter
7
+ @getter = getter
8
+ end
9
+
10
+ attr :name
11
+
12
+ def attribute_name
13
+ "@#{name}"
14
+ end
15
+
16
+ def assign?
17
+ @assign
18
+ end
19
+
20
+ def setter?
21
+ @setter
22
+ end
23
+
24
+ def getter?
25
+ @getter
26
+ end
27
+
28
+ class << self
29
+ def parse argument
30
+ case argument
31
+ when ModelField
32
+ argument
33
+ when String
34
+ from_string argument
35
+ when Symbol
36
+ from_string argument.to_s
37
+ when Array
38
+ from_array argument
39
+ else
40
+ raise "Unsupported argument #{argument}"
41
+ end
42
+ end
43
+
44
+ def from_string argument
45
+ if argument =~ /(\W*)(\w+)(\W*)/
46
+ modifiers = $1 + $3
47
+ ModelField.new $2.to_sym, modifiers.include?("@") || modifiers.include?("?"),
48
+ modifiers.include?("@") || modifiers.include?("!") || modifiers.include?("="),
49
+ modifiers.include?("@") || modifiers.include?("!") || modifiers.include?(".")
50
+ else
51
+ raise "Invalid argument string format '#{argument}'"
52
+ end
53
+ end
54
+
55
+ def from_array argument
56
+ name = argument.shift
57
+ ModelField.new name, argument.include?(:assign), argument.include?(:setter), argument.include?(:getter)
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -1,26 +1,30 @@
1
- require_relative 'model_argument'
1
+ require_relative 'model_field'
2
2
 
3
3
  module Modeling
4
4
 
5
- def model *fields, &initialize
6
- fields.map!{ ModelArgument.parse _1 }
7
- initialize_context = Struct.new(nil, *fields.map(&:name))
8
- define_method :initialize do |*a, **na|
9
- arguments = fields.zip(a).map do |f, arg|
10
- value = (f.keyword? ? na[f.name] : nil) || arg
11
- instance_variable_set f.attribute_name, value if f.attribute?
12
- value
13
- end
14
- initialize_context.new(*arguments).instance_exec &initialize if initialize
15
- end
16
- fields.each do |f|
5
+ def model *fields, &initializer
6
+ model_fields = fields.map{ ModelField.parse _1 }
7
+ define_initialize model_fields, &initializer
8
+ model_fields.each do |f|
17
9
  attr_writer f.name if f.setter?
18
10
  attr_reader f.name if f.getter?
19
- if f.tester?
20
- define_method "#{f.name}?" do
21
- !!instance_variable_get(f.attribute_name)
22
- end
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def self.model_arguments fields, *a, **na
17
+ fields.zip(a).map{|f, arg| [f.name, na.key?(f.name) ? na[f.name] : arg]}.to_h
18
+ end
19
+
20
+ def define_initialize fields, &initializer
21
+ define_method :initialize do |*a, **na, &b|
22
+ model_arguments = Modeling.model_arguments fields, *a, **na
23
+ super *(a[model_arguments.size..] || []), **na.except(*model_arguments.keys), &b
24
+ fields.each do |f|
25
+ instance_variable_set f.attribute_name, model_arguments[f.name] if f.assign?
23
26
  end
27
+ instance_exec **model_arguments, &initializer if initializer
24
28
  end
25
29
  end
26
30
 
@@ -1,3 +1,3 @@
1
1
  module Modeling
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,23 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modeling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
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-02-24 00:00:00.000000000 Z
11
+ date: 2024-02-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: "\n"
13
+ description: " Enables writting class initializers and attribute accessors in one
14
+ line. \n Struct class alternative.\n"
14
15
  email: oficjalnyadreslukasza@gmail.com
15
16
  executables: []
16
17
  extensions: []
17
18
  extra_rdoc_files: []
18
19
  files:
19
20
  - lib/modeling.rb
20
- - lib/modeling/model_argument.rb
21
+ - lib/modeling/model_field.rb
21
22
  - lib/modeling/module.rb
22
23
  - lib/modeling/version.rb
23
24
  homepage: https://github.com/lpogic/modeling
@@ -25,6 +26,7 @@ licenses:
25
26
  - Zlib
26
27
  metadata:
27
28
  documentation_uri: https://github.com/lpogic/modeling/blob/main/doc/wiki/README.md
29
+ homepage_uri: https://github.com/lpogic/modeling
28
30
  post_install_message:
29
31
  rdoc_options: []
30
32
  require_paths:
@@ -1,78 +0,0 @@
1
- module Modeling
2
- class ModelArgument
3
- def initialize name, attribute, setter, getter, keyword, tester
4
- @name = name
5
- @attribute = attribute
6
- @setter = setter
7
- @getter = getter
8
- @keyword = keyword
9
- @tester = tester
10
- end
11
-
12
- attr :name
13
-
14
- def attribute_name
15
- "@#{name}"
16
- end
17
-
18
- def attribute?
19
- @attribute
20
- end
21
-
22
- def setter?
23
- @setter
24
- end
25
-
26
- def getter?
27
- @getter
28
- end
29
-
30
- def keyword?
31
- @keyword
32
- end
33
-
34
- def tester?
35
- @tester
36
- end
37
-
38
- class << self
39
- def parse argument
40
- case argument
41
- when ModelArgument
42
- argument
43
- when String
44
- from_string argument
45
- when Symbol
46
- from_symbol argument
47
- when Array
48
- from_array argument
49
- else
50
- raise "Unsupported argument #{argument}"
51
- end
52
- end
53
-
54
- def from_symbol argument
55
- name = argument.to_s.delete_prefix("@").to_sym
56
- attribute = argument.start_with?("@")
57
- ModelArgument.new name, attribute, attribute, attribute, false, false
58
- end
59
-
60
- def from_string argument
61
- if argument =~ /(\W*)(\w+)(\W*)/
62
- modifiers = $1 + $3
63
- ModelArgument.new $2.to_sym, modifiers.include?("@"), modifiers.include?("="),
64
- modifiers.include?("+"), modifiers.include?(":"), modifiers.include?("?")
65
- else
66
- raise "Invalid argument string format '#{argument}'"
67
- end
68
- end
69
-
70
- def from_array argument
71
- name = argument.shift
72
- ModelArgument.new name, argument.include?(:attribute), argument.include?(:setter), argument.include?(:getter),
73
- argument.include?(:keyword), argument.include?(:tester)
74
- end
75
-
76
- end
77
- end
78
- end