u-attributes 0.5.0 → 0.6.0

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: f28992fcd550ab8d700f8d722af72a486c84cdc5cb5d8fd526dd653d0f88ffb9
4
- data.tar.gz: 9896e8b25b7abcb11503bd0f11f91ddcb4f0bcfcbbe6a5467928a791da7f8d6f
3
+ metadata.gz: ddf72936cbd627a8261e6db0b11a200cdb75e94dc502866dca65b36817d42d32
4
+ data.tar.gz: 2c6c8f8a5343dd21ccea4dbab2ce44e75d0ea7d3012d862b75f24cd27edaed08
5
5
  SHA512:
6
- metadata.gz: a77934dec03187c8b45ff2c9e1577472dc7b2a7633cae3edec6c2a9d093acb35262f10e625ffb8ac086d949f41f8a0b1d46f1e57df585b03d0f9b51b72a2ff53
7
- data.tar.gz: bd186e19af413b6bf26943fe4b527f285725b947226a65bc11ac35b5909883515e09267a11bbbe812e941d3aebb586c4535d406d0f7c2f6079fa42f1346ea82d
6
+ metadata.gz: 1285076cd8133141b1d711c31bd334e27bca6e1d9942d20e26e390db8b9c646d47b327b3e3107e6e71fc97012d3b8c63c830c6f9fce7d04c0b823efb8da6e00e
7
+ data.tar.gz: 0c529556494c63cec393199f74492b0a8a75be8fe48d61abbb4b4c35964e7d68621bcf6b42e09a367bbd6db3cb9ca01006f56399e8e5e1e7ad48ea842b69b977
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-attributes (0.5.0)
4
+ u-attributes (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Micro::Attributes
1
+ # μ-attributes (Micro::Attributes)
2
2
 
3
3
  This gem allows defining read-only attributes, that is, your objects will have only getters to access their attributes data.
4
4
 
@@ -7,7 +7,7 @@ This gem allows defining read-only attributes, that is, your objects will have o
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'micro-attributes'
10
+ gem 'u-attributes'
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -16,7 +16,7 @@ And then execute:
16
16
 
17
17
  Or install it yourself as:
18
18
 
19
- $ gem install micro-attributes
19
+ $ gem install u-attributes
20
20
 
21
21
  ## Usage
22
22
 
@@ -59,9 +59,9 @@ puts person.age # 21
59
59
  # person.name = 'Rodrigo'
60
60
  # NoMethodError (undefined method `name=' for #<Person:0x0000... @name="John Doe", @age=21>)
61
61
 
62
- ####################
62
+ #------------------#
63
63
  # self.attributes= #
64
- ####################
64
+ #------------------#
65
65
 
66
66
  # This protected method is added to make easier the assignment in a constructor.
67
67
 
@@ -126,13 +126,22 @@ puts person.age # 18
126
126
  # Assigning new values to get a new instance #
127
127
  ##############################################
128
128
 
129
+ #-------------------#
130
+ # #with_attribute() #
131
+ #-------------------#
132
+
129
133
  another_person = person.with_attribute(:age, 21)
130
134
 
131
135
  puts another_person.name # John Doe
132
136
  puts another_person.age # 21
133
137
  puts another_person.equal?(person) # false
134
138
 
135
- # Use #with_attributes to assign multiple attributes
139
+ #--------------------#
140
+ # #with_attributes() #
141
+ #--------------------#
142
+ #
143
+ # Use it to assign multiple attributes
144
+
136
145
  other_person = person.with_attributes(name: 'Serradura', age: 32)
137
146
 
138
147
  puts other_person.name # Serradura
@@ -157,6 +166,77 @@ instance = Subclass.new({})
157
166
  puts instance.name # John Doe
158
167
  puts instance.respond_to?(:age) # true
159
168
  puts instance.respond_to?(:foo) # true
169
+
170
+ ##############################################################
171
+ # Inheritance allows to redefine the attributes default data #
172
+ ##############################################################
173
+
174
+ class AnotherSubclass < Person
175
+ attribute! name: 'Alfa'
176
+ end
177
+
178
+ alfa_person = AnotherSubclass.new({})
179
+
180
+ p alfa_person.name # "Alfa"
181
+ p alfa_person.age # nil
182
+
183
+ class SubSubclass < Subclass
184
+ attributes! name: 'Beta', age: 0
185
+ end
186
+
187
+ beta_person = SubSubclass.new({})
188
+
189
+ p beta_person.name # "Beta"
190
+ p beta_person.age # 0
191
+ ```
192
+
193
+ ### How to query the attributes?
194
+
195
+ ```ruby
196
+ class Person
197
+ include Micro::Attributes
198
+
199
+ attributes :age, name: 'John Doe'
200
+
201
+ def initialize(options)
202
+ self.attributes = options
203
+ end
204
+ end
205
+
206
+ #---------------#
207
+ # .attributes() #
208
+ #---------------#
209
+
210
+ p Person.attributes # ["name", "age"]
211
+
212
+ #---------------#
213
+ # .attribute?() #
214
+ #---------------#
215
+
216
+ puts Person.attribute?(:name) # true
217
+ puts Person.attribute?('name') # true
218
+ puts Person.attribute?('foo') # false
219
+ puts Person.attribute?(:foo) # false
220
+
221
+ # ---
222
+
223
+ person = Person.new(age: 20)
224
+
225
+ #---------------#
226
+ # #attribute?() #
227
+ #---------------#
228
+
229
+ puts person.attribute?(:name) # true
230
+ puts person.attribute?('name') # true
231
+ puts person.attribute?('foo') # false
232
+ puts person.attribute?(:foo) # false
233
+
234
+ #---------------#
235
+ # #attributes() #
236
+ #---------------#
237
+
238
+ p person.attributes # {"age"=>20, "name"=>"John Doe"}
239
+ p Person.new(name: 'John').attributes # {"age"=>nil, "name"=>"John"}
160
240
  ```
161
241
 
162
242
  ## Development
@@ -24,14 +24,22 @@ module Micro
24
24
  @__attributes_data ||= {}
25
25
  end
26
26
 
27
- def __attribute_data(name, value)
28
- __attributes_data[name] = value if __attribute(name)
27
+ def __attribute_data(name, value, allow_to_override)
28
+ __attributes_data[name] = value if allow_to_override || __attribute(name)
29
+ end
30
+
31
+ def __attribute_data!(arg, allow_to_override:)
32
+ return __attribute_data(arg.to_s, nil, allow_to_override) unless arg.is_a?(Hash)
33
+
34
+ arg.each { |key, value| __attribute_data(key.to_s, value, allow_to_override) }
29
35
  end
30
36
 
31
37
  def attribute(arg)
32
- return __attribute_data(arg.to_s, nil) unless arg.is_a?(Hash)
38
+ __attribute_data!(arg, allow_to_override: false)
39
+ end
33
40
 
34
- arg.each { |key, value| __attribute_data(key.to_s, value) }
41
+ def attribute!(arg)
42
+ __attribute_data!(arg, allow_to_override: true)
35
43
  end
36
44
 
37
45
  def attributes(*args)
@@ -40,6 +48,10 @@ module Micro
40
48
  args.flatten.each { |arg| attribute(arg) }
41
49
  end
42
50
 
51
+ def attributes!(*args)
52
+ args.flatten.each { |arg| attribute!(arg) }
53
+ end
54
+
43
55
  def attributes_data(arg)
44
56
  __attributes_data.merge(
45
57
  Utils.hash_argument!(arg)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Attributes
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
@@ -13,6 +13,7 @@ module Micro
13
13
  private_class_method :__attribute
14
14
  private_class_method :__attributes
15
15
  private_class_method :__attribute_data
16
+ private_class_method :__attribute_data!
16
17
  private_class_method :__attributes_data
17
18
  end
18
19
 
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.5.0
4
+ version: 0.6.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-04 00:00:00.000000000 Z
11
+ date: 2019-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  requirements: []
84
- rubygems_version: 3.0.3
84
+ rubygems_version: 3.0.1
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: Define read-only attributes