u-attributes 0.7.0 → 0.8.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 +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +28 -2
- data/lib/micro/attributes/{utils.rb → attributes_utils.rb} +1 -1
- data/lib/micro/attributes/macros.rb +3 -3
- data/lib/micro/attributes/to_initialize.rb +25 -0
- data/lib/micro/attributes/version.rb +1 -1
- data/lib/micro/attributes.rb +24 -28
- data/{micro-attributes.gemspec → u-attributes.gemspec} +0 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6458bbe7cd9f3f3ce857b69273aec70bf9697594c4f2ee0ca898c441d0d7f1f
|
4
|
+
data.tar.gz: f71576f85de2c6a03ea50401a6b2e791ed79ec393d061c5653e52076bcaee23a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c26e67d45e58f7b381e4b1a250a1d284eb05dc6455baae21cecff5ca54b5de864360c2d4f889ecab318d3b5d406ee39a1011608a1e3983dddf0106f5d849c10e
|
7
|
+
data.tar.gz: 9ff97e16e741127e9311ce0baf74deac64a24bc9d964e5f32f17d3a03e27fcbc32ded8efa3ce367caaec2032adc5327ea13bb341e7be3c8939f542283cd7725c
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -80,6 +80,32 @@ person = Person.new('name' => 'John', age: 20)
|
|
80
80
|
|
81
81
|
puts person.name # John
|
82
82
|
puts person.age # 20
|
83
|
+
|
84
|
+
#--------------#
|
85
|
+
# #attribute() #
|
86
|
+
#--------------#
|
87
|
+
#
|
88
|
+
# Use the #attribute() method with a valid attribute name to get its value
|
89
|
+
|
90
|
+
puts person.attribute(:name) # John
|
91
|
+
puts person.attribute('age') # 20
|
92
|
+
puts person.attribute('foo') # nil
|
93
|
+
|
94
|
+
#
|
95
|
+
# If you pass a block, it will be executed only if the attribute is valid.
|
96
|
+
|
97
|
+
person.attribute(:name) { |value| puts value } # John
|
98
|
+
person.attribute('age') { |value| puts value } # 20
|
99
|
+
person.attribute('foo') { |value| puts value } # !! Nothing happened, because of the attribute not exists.
|
100
|
+
|
101
|
+
#--------------#
|
102
|
+
# #attribute() #
|
103
|
+
#--------------#
|
104
|
+
#
|
105
|
+
# Works like the #attribute() method, but will raise an exception when the attribute not exist.
|
106
|
+
|
107
|
+
puts person.attribute!('foo') # NameError (undefined attribute `foo)
|
108
|
+
person.attribute!('foo') { |value| puts value } # NameError (undefined attribute `foo)
|
83
109
|
```
|
84
110
|
|
85
111
|
### How to define multiple attributes?
|
@@ -249,7 +275,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
249
275
|
|
250
276
|
## Contributing
|
251
277
|
|
252
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
278
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/serradura/u-attributes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
253
279
|
|
254
280
|
## License
|
255
281
|
|
@@ -257,4 +283,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
257
283
|
|
258
284
|
## Code of Conduct
|
259
285
|
|
260
|
-
Everyone interacting in the Micro::Attributes project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
286
|
+
Everyone interacting in the Micro::Attributes project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/serradura/u-attributes/blob/master/CODE_OF_CONDUCT.md).
|
@@ -44,8 +44,8 @@ module Micro
|
|
44
44
|
|
45
45
|
def attributes_data(arg)
|
46
46
|
__attributes_data.merge(
|
47
|
-
|
48
|
-
|
47
|
+
AttributesUtils.hash_argument!(arg)
|
48
|
+
.each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
|
49
49
|
)
|
50
50
|
end
|
51
51
|
|
@@ -59,8 +59,8 @@ module Micro
|
|
59
59
|
raise ArgumentError, 'wrong number of arguments (given 0, expected 1 or more)'
|
60
60
|
end
|
61
61
|
end
|
62
|
-
|
63
62
|
private_constant :ForSubclasses
|
64
63
|
end
|
64
|
+
private_constant :Macros
|
65
65
|
end
|
66
66
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Micro
|
4
|
+
module Attributes
|
5
|
+
module ToInitialize
|
6
|
+
def self.included(base)
|
7
|
+
base.send(:include, ::Micro::Attributes)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(arg)
|
11
|
+
self.attributes = arg
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_attribute(key, val)
|
15
|
+
self.class.new(attributes.merge(key => val))
|
16
|
+
end
|
17
|
+
|
18
|
+
def with_attributes(arg)
|
19
|
+
self.class.new(attributes.merge(arg))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private_constant :ToInitialize
|
24
|
+
end
|
25
|
+
end
|
data/lib/micro/attributes.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "micro/attributes/version"
|
4
|
-
require "micro/attributes/
|
4
|
+
require "micro/attributes/attributes_utils"
|
5
5
|
require "micro/attributes/macros"
|
6
|
+
require "micro/attributes/to_initialize"
|
6
7
|
|
7
8
|
module Micro
|
8
9
|
module Attributes
|
9
10
|
def self.included(base)
|
10
|
-
base.extend
|
11
|
+
base.extend(::Micro::Attributes.const_get(:Macros))
|
11
12
|
|
12
13
|
base.class_eval do
|
13
14
|
private_class_method :__attribute
|
@@ -22,41 +23,20 @@ module Micro
|
|
22
23
|
subclass.attribute(value.nil? ? name : {name => value})
|
23
24
|
end
|
24
25
|
|
25
|
-
subclass.extend
|
26
|
+
subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses')
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
30
|
def self.to_initialize
|
30
|
-
@to_initialize ||=
|
31
|
-
def self.included(base)
|
32
|
-
base.send(:include, Micro::Attributes)
|
33
|
-
|
34
|
-
base.class_eval(<<-RUBY)
|
35
|
-
def initialize(arg)
|
36
|
-
self.attributes = arg
|
37
|
-
end
|
38
|
-
|
39
|
-
def with_attribute(key, val)
|
40
|
-
self.class.new(attributes.merge(key => val))
|
41
|
-
end
|
42
|
-
|
43
|
-
def with_attributes(arg)
|
44
|
-
self.class.new(attributes.merge(arg))
|
45
|
-
end
|
46
|
-
RUBY
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def attribute?(name)
|
52
|
-
self.class.attribute?(name)
|
31
|
+
@to_initialize ||= ::Micro::Attributes.const_get(:ToInitialize)
|
53
32
|
end
|
54
33
|
|
55
34
|
def attributes=(arg)
|
56
|
-
self.class.attributes_data(
|
35
|
+
self.class.attributes_data(AttributesUtils.hash_argument!(arg)).each do |name, value|
|
57
36
|
instance_variable_set("@#{name}", value) if attribute?(name)
|
58
37
|
end
|
59
38
|
end
|
39
|
+
protected :attributes=
|
60
40
|
|
61
41
|
def attributes
|
62
42
|
state = self.class.attributes.each_with_object({}) do |name, memo|
|
@@ -66,6 +46,22 @@ module Micro
|
|
66
46
|
self.class.attributes_data(state)
|
67
47
|
end
|
68
48
|
|
69
|
-
|
49
|
+
def attribute?(name)
|
50
|
+
self.class.attribute?(name)
|
51
|
+
end
|
52
|
+
|
53
|
+
def attribute(name)
|
54
|
+
return unless attribute?(name)
|
55
|
+
|
56
|
+
value = public_send(name)
|
57
|
+
|
58
|
+
block_given? ? yield(value) : value
|
59
|
+
end
|
60
|
+
|
61
|
+
def attribute!(name, &block)
|
62
|
+
attribute(name) { |name| return block ? block[name] : name }
|
63
|
+
|
64
|
+
raise NameError, "undefined attribute `#{name}"
|
65
|
+
end
|
70
66
|
end
|
71
67
|
end
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
@@ -57,11 +57,12 @@ files:
|
|
57
57
|
- bin/console
|
58
58
|
- bin/setup
|
59
59
|
- lib/micro/attributes.rb
|
60
|
+
- lib/micro/attributes/attributes_utils.rb
|
60
61
|
- lib/micro/attributes/macros.rb
|
61
|
-
- lib/micro/attributes/
|
62
|
+
- lib/micro/attributes/to_initialize.rb
|
62
63
|
- lib/micro/attributes/version.rb
|
63
64
|
- lib/u-attributes.rb
|
64
|
-
-
|
65
|
+
- u-attributes.gemspec
|
65
66
|
homepage: https://github.com/serradura/u-attributes
|
66
67
|
licenses:
|
67
68
|
- MIT
|