u-attributes 0.3.0 → 0.4.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 +5 -5
- data/Gemfile.lock +1 -1
- data/README.md +25 -2
- data/lib/micro/attributes.rb +10 -13
- data/lib/micro/attributes/macros.rb +13 -19
- data/lib/micro/attributes/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 724654639b33deda5424bc5e0ac320ff1a79c34aa1aed860aa9aa18ac77830be
|
4
|
+
data.tar.gz: 44c63295b620dd096b08aefe3b6d62604a09d857ff1541f74829f0df59076937
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9ad9a87ec9c9999f311e8f655c6ac186928718ea24fffd017173c78b2611c8879d7ae2f4011f835505f1cf73decb47a9e1b7566c134b5534fe4214606b7c00b
|
7
|
+
data.tar.gz: 9eec234c824538f536e2cd329034ca4954c9770b78c8f144da0b0e87ee84fc1979e7345e9d06e48936805d1bf3ec870c3ef74e693793938c2edd83fb73655da4
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -58,6 +58,28 @@ puts person.age # 21
|
|
58
58
|
#
|
59
59
|
# person.name = 'Rodrigo'
|
60
60
|
# NoMethodError (undefined method `name=' for #<Person:0x0000... @name="John Doe", @age=21>)
|
61
|
+
|
62
|
+
####################
|
63
|
+
# self.attributes= #
|
64
|
+
####################
|
65
|
+
|
66
|
+
# This protected method is added to make easier the assignment in a constructor.
|
67
|
+
|
68
|
+
class Person
|
69
|
+
include Micro::Attributes
|
70
|
+
|
71
|
+
attribute :name
|
72
|
+
attribute :age
|
73
|
+
|
74
|
+
def initialize(options)
|
75
|
+
self.attributes = options
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
person = Person.new('name' => 'John', age: 20)
|
80
|
+
|
81
|
+
puts person.name # John
|
82
|
+
puts person.age # 20
|
61
83
|
```
|
62
84
|
|
63
85
|
### How to define multiple attributes?
|
@@ -71,8 +93,8 @@ class Person
|
|
71
93
|
|
72
94
|
attributes :name, :age
|
73
95
|
|
74
|
-
def initialize(
|
75
|
-
|
96
|
+
def initialize(options)
|
97
|
+
self.attributes = options
|
76
98
|
end
|
77
99
|
end
|
78
100
|
|
@@ -125,6 +147,7 @@ puts other_person.equal?(person) # false
|
|
125
147
|
#########################################################
|
126
148
|
# Inheritance will preserve the parent class attributes #
|
127
149
|
#########################################################
|
150
|
+
|
128
151
|
class Subclass < Person
|
129
152
|
attribute :foo
|
130
153
|
end
|
data/lib/micro/attributes.rb
CHANGED
@@ -11,17 +11,13 @@ module Micro
|
|
11
11
|
base.class_eval do
|
12
12
|
private_class_method :__attribute
|
13
13
|
private_class_method :__attributes
|
14
|
-
private_class_method :
|
14
|
+
private_class_method :__attribute_data
|
15
|
+
private_class_method :__attributes_data
|
15
16
|
end
|
16
17
|
|
17
18
|
def base.inherited(subclass)
|
18
19
|
self.attributes_data({}) do |data|
|
19
|
-
|
20
|
-
v.nil? ? m[:a] << k : m[:h][k] = v
|
21
|
-
end
|
22
|
-
|
23
|
-
subclass.attributes(values[:a])
|
24
|
-
subclass.attributes(values[:h])
|
20
|
+
data.each { |k, v| subclass.attribute(v.nil? ? k : {k => v}) }
|
25
21
|
end
|
26
22
|
end
|
27
23
|
end
|
@@ -33,7 +29,6 @@ module Micro
|
|
33
29
|
|
34
30
|
base.class_eval(<<-RUBY)
|
35
31
|
def initialize(arg)
|
36
|
-
raise ArgumentError, 'argument must be a Hash' unless arg.is_a?(Hash)
|
37
32
|
self.attributes = arg
|
38
33
|
end
|
39
34
|
|
@@ -53,8 +48,10 @@ module Micro
|
|
53
48
|
self.class.attribute?(name)
|
54
49
|
end
|
55
50
|
|
56
|
-
def attributes=(
|
57
|
-
|
51
|
+
def attributes=(arg)
|
52
|
+
raise ArgumentError, 'argument must be a Hash' unless arg.is_a?(Hash)
|
53
|
+
|
54
|
+
self.class.attributes_data(arg) do |data|
|
58
55
|
data.each do |name, value|
|
59
56
|
instance_variable_set("@#{name}", data[name]) if attribute?(name)
|
60
57
|
end
|
@@ -64,9 +61,9 @@ module Micro
|
|
64
61
|
def attributes
|
65
62
|
state =
|
66
63
|
self.class.attributes.each_with_object({}) do |name, memo|
|
67
|
-
iv_name = "@#{name}"
|
68
|
-
|
69
|
-
|
64
|
+
if instance_variable_defined?(iv_name = "@#{name}")
|
65
|
+
memo[name] = instance_variable_get(iv_name)
|
66
|
+
end
|
70
67
|
end
|
71
68
|
|
72
69
|
self.class.attributes_data(state) { |data| data }
|
@@ -3,10 +3,6 @@
|
|
3
3
|
module Micro
|
4
4
|
module Attributes
|
5
5
|
module Macros
|
6
|
-
def __attributes_defaults
|
7
|
-
@__attributes_defaults ||= {}
|
8
|
-
end
|
9
|
-
|
10
6
|
def __attributes
|
11
7
|
@__attributes ||= Set.new
|
12
8
|
end
|
@@ -24,13 +20,18 @@ module Micro
|
|
24
20
|
return true
|
25
21
|
end
|
26
22
|
|
23
|
+
def __attributes_data
|
24
|
+
@__attributes_data ||= {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def __attribute_data(name, value)
|
28
|
+
__attributes_data[name] = value if __attribute(name)
|
29
|
+
end
|
30
|
+
|
27
31
|
def attribute(arg)
|
28
|
-
return
|
32
|
+
return __attribute_data(arg.to_s, nil) unless arg.is_a?(Hash)
|
29
33
|
|
30
|
-
arg.each
|
31
|
-
name = key.to_s
|
32
|
-
__attributes_defaults[name] = value if __attribute(name)
|
33
|
-
end
|
34
|
+
arg.each { |key, value| __attribute_data(key.to_s, value) }
|
34
35
|
end
|
35
36
|
|
36
37
|
def attributes(*args)
|
@@ -40,17 +41,10 @@ module Micro
|
|
40
41
|
end
|
41
42
|
|
42
43
|
def attributes_data(arg)
|
43
|
-
|
44
|
-
memo[key.to_s] = arg[key]
|
45
|
-
end
|
46
|
-
|
47
|
-
undefineds = (self.attributes - normalized_params.keys)
|
48
|
-
nil_params =
|
49
|
-
undefineds.each_with_object({}) { |name, memo| memo[name] = nil }
|
44
|
+
normalized_arg =
|
45
|
+
arg.keys.each_with_object({}) { |key, memo| memo[key.to_s] = arg[key] }
|
50
46
|
|
51
|
-
yield(
|
52
|
-
nil_params.merge!(__attributes_defaults).merge!(normalized_params)
|
53
|
-
)
|
47
|
+
yield(__attributes_data.merge(normalized_arg))
|
54
48
|
end
|
55
49
|
end
|
56
50
|
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.4.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-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -80,8 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: '0'
|
82
82
|
requirements: []
|
83
|
-
|
84
|
-
rubygems_version: 2.4.5
|
83
|
+
rubygems_version: 3.0.3
|
85
84
|
signing_key:
|
86
85
|
specification_version: 4
|
87
86
|
summary: Define read-only attributes
|