koine-attributes 0.1.2 → 0.1.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 +4 -4
- data/lib/koine/attributes/builder.rb +97 -73
- data/lib/koine/attributes/version.rb +1 -1
- data/lib/koine/attributes.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9467d0514737ba4503f2b9f3fa95eb308c14c1cf
|
4
|
+
data.tar.gz: 9c3b0f397e5eef27e60f7274b1e9c3cc55964e1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 707a937b70778bbb5bd9532ed1a6e46d850f0e4e48fe7cb725657fa6e85d46431c7df914004b08d59945f95e32eca0adfe5b1918c4270e23250b4795585324e2
|
7
|
+
data.tar.gz: b7b90869b1e5a6caa2edd3fa2b381e01e8c57ce0e23878b4dd1f92fa67682432259519c5805bc59a82ba3c70d7614011c8c12a58b7bb4b20c25be706e68913c2
|
@@ -2,117 +2,141 @@ require 'koine/attributes/hash_helper'
|
|
2
2
|
|
3
3
|
module Koine
|
4
4
|
module Attributes
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
module Builder
|
6
|
+
class ClassBuilder
|
7
|
+
attr_reader :attributes
|
8
|
+
attr_reader :target
|
9
|
+
|
10
|
+
def initialize(target:)
|
11
|
+
@target = target
|
12
|
+
@attributes = []
|
13
|
+
@getter = GetterBuilder.new(attributes: attributes, target: target)
|
14
|
+
@setter = SetterBuilder.new(attributes: attributes, target: target)
|
15
|
+
@constructor = ConstructorBuilder.new(attributes: attributes, target: target)
|
16
|
+
end
|
17
|
+
|
18
|
+
def build(name, driver)
|
19
|
+
attributes.push(name.to_sym)
|
20
|
+
@getter.build(name, driver)
|
21
|
+
@setter.build(name, driver)
|
22
|
+
end
|
8
23
|
|
9
|
-
|
10
|
-
|
11
|
-
|
24
|
+
def build_constructor(strict: true, freeze: false)
|
25
|
+
@constructor.build(strict: strict, freeze: freeze)
|
26
|
+
end
|
12
27
|
end
|
13
28
|
|
14
|
-
|
15
|
-
attributes
|
16
|
-
|
17
|
-
|
29
|
+
class BaseBuilder
|
30
|
+
attr_reader :attributes, :target
|
31
|
+
|
32
|
+
def initialize(attributes:, target:)
|
33
|
+
@attributes = attributes
|
34
|
+
@target = target
|
35
|
+
end
|
18
36
|
end
|
19
37
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
38
|
+
class GetterBuilder < BaseBuilder
|
39
|
+
def build(name, driver)
|
40
|
+
target.class_eval do
|
41
|
+
define_method(name) do
|
42
|
+
instance_variable_get("@#{name}") || driver.default_value
|
43
|
+
end
|
24
44
|
end
|
25
45
|
end
|
26
46
|
end
|
27
47
|
|
28
|
-
|
29
|
-
|
48
|
+
class SetterBuilder < BaseBuilder
|
49
|
+
def build(name, driver)
|
50
|
+
setter = "#{name}="
|
30
51
|
|
31
|
-
|
32
|
-
|
33
|
-
|
52
|
+
target.class_eval do
|
53
|
+
define_method(setter) do |*args|
|
54
|
+
instance_variable_set("@#{name}", driver.coerce(*args))
|
55
|
+
end
|
34
56
|
end
|
35
57
|
end
|
36
58
|
end
|
37
59
|
|
38
|
-
|
39
|
-
|
60
|
+
class ConstructorBuilder < BaseBuilder
|
61
|
+
def build(strict: true, freeze: false)
|
62
|
+
valid_attributes = attributes
|
40
63
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
64
|
+
target.class_eval do
|
65
|
+
def initialize(args = {})
|
66
|
+
initialize_attributes(args)
|
67
|
+
end
|
68
|
+
|
69
|
+
protected
|
45
70
|
|
46
|
-
|
71
|
+
define_method(:initialize_attributes) do |constructor_args|
|
72
|
+
invalid_attributes = []
|
73
|
+
constructor_args = HashHelper.new.symbolize_keys(constructor_args)
|
47
74
|
|
48
|
-
|
49
|
-
|
50
|
-
|
75
|
+
constructor_args.each do |name, value|
|
76
|
+
if valid_attributes.include?(name)
|
77
|
+
setter = "#{name}=".to_sym
|
78
|
+
send(setter, value)
|
79
|
+
next
|
80
|
+
end
|
51
81
|
|
52
|
-
|
53
|
-
|
54
|
-
setter = "#{name}=".to_sym
|
55
|
-
send(setter, value)
|
56
|
-
next
|
82
|
+
next unless strict
|
83
|
+
invalid_attributes << name
|
57
84
|
end
|
58
85
|
|
59
|
-
|
60
|
-
|
61
|
-
|
86
|
+
unless invalid_attributes.empty?
|
87
|
+
attributes = invalid_attributes.join(', ')
|
88
|
+
raise ArgumentError, "Invalid attributes (#{attributes})"
|
89
|
+
end
|
62
90
|
|
63
|
-
|
64
|
-
attributes = invalid_attributes.join(', ')
|
65
|
-
raise ArgumentError, "Invalid attributes (#{attributes})"
|
91
|
+
self.freeze if freeze
|
66
92
|
end
|
67
|
-
|
68
|
-
self.freeze if freeze
|
69
93
|
end
|
70
|
-
end
|
71
94
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
95
|
+
if strict
|
96
|
+
make_setters_private
|
97
|
+
create_with_methods
|
98
|
+
create_comparator
|
99
|
+
end
|
76
100
|
end
|
77
|
-
end
|
78
101
|
|
79
|
-
|
102
|
+
protected
|
80
103
|
|
81
|
-
|
82
|
-
|
104
|
+
def make_setters_private
|
105
|
+
attrs = attributes
|
83
106
|
|
84
|
-
|
85
|
-
|
86
|
-
|
107
|
+
target.instance_eval do
|
108
|
+
attrs.each do |attr|
|
109
|
+
private "#{attr}="
|
110
|
+
end
|
87
111
|
end
|
88
112
|
end
|
89
|
-
end
|
90
113
|
|
91
|
-
|
92
|
-
|
93
|
-
|
114
|
+
def create_with_methods
|
115
|
+
attributes.each do |attr|
|
116
|
+
create_with_method(attr)
|
117
|
+
end
|
94
118
|
end
|
95
|
-
end
|
96
119
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
120
|
+
def create_with_method(attr)
|
121
|
+
target.class_eval do
|
122
|
+
define_method "with_#{attr}" do |*args|
|
123
|
+
dup.tap do |new_object|
|
124
|
+
new_object.send("#{attr}=", *args)
|
125
|
+
new_object.freeze
|
126
|
+
end
|
103
127
|
end
|
104
128
|
end
|
105
129
|
end
|
106
|
-
end
|
107
130
|
|
108
|
-
|
109
|
-
|
131
|
+
def create_comparator
|
132
|
+
attrs = attributes
|
110
133
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
134
|
+
target.class_eval do
|
135
|
+
define_method :== do |that|
|
136
|
+
a = attrs.map { |attr| send(attr) }
|
137
|
+
b = attrs.map { |attr| that.send(attr) }
|
138
|
+
a == b
|
139
|
+
end
|
116
140
|
end
|
117
141
|
end
|
118
142
|
end
|
data/lib/koine/attributes.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: koine-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcelo Jacobus
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|