modeling 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 984bdb92ad9c4e829cf239f3df9eb1479e41c37d5659944933edb8d69282020f
4
- data.tar.gz: b47547c32079a0d5680d99dd7cca3f8fa5736a2eb56bd9a5bfcab9547aa14687
3
+ metadata.gz: 8a46a2db8c7a4651f21562902a6c44fad7a2e69df37dafb4b6718d5fe019b307
4
+ data.tar.gz: 7d688b753d7898bc5e452fa7370643c784174972a09320102bce25f7d484c2a2
5
5
  SHA512:
6
- metadata.gz: b3d8cd4181cf41ea9007cfaf8aa5e4eaa80c2bd68ff65e42a00fd4902590dca277226a18f011f8cb854d2f18791b910042e2b4c2e16e0410c0536253c785fd29
7
- data.tar.gz: e66cc1a8d2c45d361508ea206dcbfb94ed54875d05966b603447d6fe77d122f37d013068e70223de41bb2c777fc7df43a4439f265eb364a904ea648f084d1b54
6
+ metadata.gz: bd0fe43ec29e6a6256d8850dd36e6397f5c402b5d6a0c5eb8b9d6d5bf429b9e5c23c766b50ffd8e9acdd2dee8b80ead237768aa2c74ecfbb2e341a9855953fa4
7
+ data.tar.gz: 17fa31f142f571b7311b79bbf842a1fbf9b96ab8df595007698e67df022aa113b8602a1c4646c6418d74e053482b7af2a3d973e5635daef3f3587d9bca2de56b
@@ -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.2"
3
3
  end
metadata CHANGED
@@ -1,23 +1,26 @@
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.2
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: "\n ## Sample:\n ```RUBY\n require 'modeling'\n\n class Foo\n\n model
14
+ :@first, :@second\n \n end\n\n # ==\n\n class Foo\n\n def initialize first,
15
+ second\n @first = first\n @second = second\n end\n\n attr_accessor
16
+ :first, :second\n\n end\n ```\n ## Check the documentation or home page for more.\n\n"
14
17
  email: oficjalnyadreslukasza@gmail.com
15
18
  executables: []
16
19
  extensions: []
17
20
  extra_rdoc_files: []
18
21
  files:
19
22
  - lib/modeling.rb
20
- - lib/modeling/model_argument.rb
23
+ - lib/modeling/model_field.rb
21
24
  - lib/modeling/module.rb
22
25
  - lib/modeling/version.rb
23
26
  homepage: https://github.com/lpogic/modeling
@@ -25,6 +28,7 @@ licenses:
25
28
  - Zlib
26
29
  metadata:
27
30
  documentation_uri: https://github.com/lpogic/modeling/blob/main/doc/wiki/README.md
31
+ homepage_uri: https://github.com/lpogic/modeling
28
32
  post_install_message:
29
33
  rdoc_options: []
30
34
  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