u-attributes 0.8.0 → 0.9.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +10 -13
- data/lib/micro/attributes.rb +4 -9
- data/lib/micro/attributes/attributes_utils.rb +6 -0
- data/lib/micro/attributes/macros.rb +25 -25
- data/lib/micro/attributes/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63911d6718fe30f139ee9acfb5b4a4577a71c3e01222087a17a4d0f349b31935
|
4
|
+
data.tar.gz: 8a3a4266c67d1e8eade0d742cf2d057c0bafb2f2e25c26c768f73cea4d40aa97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 927eb73d0b9cfce21e9d8abe11ab899296a6f45721d7821ea175e0bc58d144015ba22e488f1bdb6be563821a8b4e25a1082eba4c103ceedbed556ded654ab721
|
7
|
+
data.tar.gz: 85bf3683290458bfb863b215fe5eca4e57e041bb2dd167e1908a629c9081d9c8417584b847b9a775d46658f8069c5d960484a9c7a2be7ffb462eac1104f4b4ca
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -68,7 +68,7 @@ puts person.age # 21
|
|
68
68
|
class Person
|
69
69
|
include Micro::Attributes
|
70
70
|
|
71
|
-
attribute :name
|
71
|
+
attribute :name, 'John Doe' # .attribute() accepts a second arg as its default value
|
72
72
|
attribute :age
|
73
73
|
|
74
74
|
def initialize(options)
|
@@ -76,9 +76,9 @@ class Person
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
person = Person.new(
|
79
|
+
person = Person.new(age: 20)
|
80
80
|
|
81
|
-
puts person.name # John
|
81
|
+
puts person.name # John Doe
|
82
82
|
puts person.age # 20
|
83
83
|
|
84
84
|
#--------------#
|
@@ -87,14 +87,14 @@ puts person.age # 20
|
|
87
87
|
#
|
88
88
|
# Use the #attribute() method with a valid attribute name to get its value
|
89
89
|
|
90
|
-
puts person.attribute(:name) # John
|
90
|
+
puts person.attribute(:name) # John Doe
|
91
91
|
puts person.attribute('age') # 20
|
92
92
|
puts person.attribute('foo') # nil
|
93
93
|
|
94
94
|
#
|
95
95
|
# If you pass a block, it will be executed only if the attribute is valid.
|
96
96
|
|
97
|
-
person.attribute(:name) { |value| puts value } # John
|
97
|
+
person.attribute(:name) { |value| puts value } # John Doe
|
98
98
|
person.attribute('age') { |value| puts value } # 20
|
99
99
|
person.attribute('foo') { |value| puts value } # !! Nothing happened, because of the attribute not exists.
|
100
100
|
|
@@ -117,16 +117,16 @@ person.attribute!('foo') { |value| puts value } # NameError (undefined attribute
|
|
117
117
|
class Person
|
118
118
|
include Micro::Attributes
|
119
119
|
|
120
|
-
attributes :
|
120
|
+
attributes :age, name: 'John Doe' # Use a hash to define attributes with default values
|
121
121
|
|
122
122
|
def initialize(options)
|
123
123
|
self.attributes = options
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
|
-
person = Person.new(
|
127
|
+
person = Person.new(age: 32)
|
128
128
|
|
129
|
-
puts person.name #
|
129
|
+
puts person.name # 'John Doe'
|
130
130
|
puts person.age # 32
|
131
131
|
```
|
132
132
|
|
@@ -137,10 +137,7 @@ A: Use `Micro::Attributes.to_initialize`
|
|
137
137
|
class Person
|
138
138
|
include Micro::Attributes.to_initialize
|
139
139
|
|
140
|
-
attributes :age, name: 'John Doe'
|
141
|
-
|
142
|
-
# attribute name: 'John Doe'
|
143
|
-
# attribute :age
|
140
|
+
attributes :age, name: 'John Doe'
|
144
141
|
end
|
145
142
|
|
146
143
|
person = Person.new(age: 18)
|
@@ -200,7 +197,7 @@ puts instance.respond_to?(:foo) # true
|
|
200
197
|
# The methods above allow redefining the attributes default data
|
201
198
|
|
202
199
|
class AnotherSubclass < Person
|
203
|
-
attribute! name
|
200
|
+
attribute! :name, 'Alfa'
|
204
201
|
end
|
205
202
|
|
206
203
|
alfa_person = AnotherSubclass.new({})
|
data/lib/micro/attributes.rb
CHANGED
@@ -11,18 +11,13 @@ module Micro
|
|
11
11
|
base.extend(::Micro::Attributes.const_get(:Macros))
|
12
12
|
|
13
13
|
base.class_eval do
|
14
|
-
private_class_method :
|
15
|
-
private_class_method :
|
16
|
-
private_class_method :
|
17
|
-
private_class_method :__attribute_data!
|
18
|
-
private_class_method :__attributes_data
|
14
|
+
private_class_method :__attributes_data, :__attributes
|
15
|
+
private_class_method :__attributes_def, :__attributes_set
|
16
|
+
private_class_method :__attribute_reader, :__attribute_set
|
19
17
|
end
|
20
18
|
|
21
19
|
def base.inherited(subclass)
|
22
|
-
self.attributes_data({})
|
23
|
-
subclass.attribute(value.nil? ? name : {name => value})
|
24
|
-
end
|
25
|
-
|
20
|
+
subclass.attributes(self.attributes_data({}))
|
26
21
|
subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses')
|
27
22
|
end
|
28
23
|
end
|
@@ -3,59 +3,59 @@
|
|
3
3
|
module Micro
|
4
4
|
module Attributes
|
5
5
|
module Macros
|
6
|
-
def
|
7
|
-
@
|
6
|
+
def __attributes_data
|
7
|
+
@__attributes_data ||= {}
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
__attributes.
|
10
|
+
def __attributes
|
11
|
+
@__attributes ||= Set.new
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
14
|
+
def __attribute_reader(name)
|
15
15
|
__attributes.add(name)
|
16
16
|
attr_reader(name)
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
20
|
-
|
19
|
+
def __attribute_set(key, value, can_overwrite)
|
20
|
+
name = key.to_s
|
21
|
+
has_attribute = attribute?(name)
|
22
|
+
__attribute_reader(name) unless has_attribute
|
23
|
+
__attributes_data[name] = value if can_overwrite || !has_attribute
|
21
24
|
end
|
22
25
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
__attributes_data[name] = value if allow_to_override || !has_attribute
|
26
|
+
def __attributes_def(arg, can_overwrite)
|
27
|
+
return __attribute_set(arg, nil, can_overwrite) unless arg.is_a?(::Hash)
|
28
|
+
arg.each { |key, val| __attribute_set(key, val, can_overwrite) }
|
27
29
|
end
|
28
30
|
|
29
|
-
def
|
30
|
-
|
31
|
+
def __attributes_set(args, can_overwrite)
|
32
|
+
args.flatten.each { |arg| __attributes_def(arg, can_overwrite) }
|
33
|
+
end
|
31
34
|
|
32
|
-
|
35
|
+
def attribute?(name)
|
36
|
+
__attributes.member?(name.to_s)
|
33
37
|
end
|
34
38
|
|
35
|
-
def attribute(
|
36
|
-
|
39
|
+
def attribute(name, value=nil)
|
40
|
+
__attribute_set(name, value, false)
|
37
41
|
end
|
38
42
|
|
39
43
|
def attributes(*args)
|
40
44
|
return __attributes.to_a if args.empty?
|
41
|
-
|
42
|
-
args.flatten.each { |arg| attribute(arg) }
|
45
|
+
__attributes_set(args, can_overwrite: false)
|
43
46
|
end
|
44
47
|
|
45
48
|
def attributes_data(arg)
|
46
|
-
__attributes_data.merge(
|
47
|
-
AttributesUtils.hash_argument!(arg)
|
48
|
-
.each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
|
49
|
-
)
|
49
|
+
__attributes_data.merge(AttributesUtils.stringify_hash_keys!(arg))
|
50
50
|
end
|
51
51
|
|
52
52
|
module ForSubclasses
|
53
|
-
def attribute!(
|
54
|
-
|
53
|
+
def attribute!(name, value=nil)
|
54
|
+
__attribute_set(name, value, true)
|
55
55
|
end
|
56
56
|
|
57
57
|
def attributes!(*args)
|
58
|
-
return args
|
58
|
+
return __attributes_set(args, can_overwrite: true) unless args.empty?
|
59
59
|
raise ArgumentError, 'wrong number of arguments (given 0, expected 1 or more)'
|
60
60
|
end
|
61
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|