u-attributes 0.13.0 → 0.14.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 +72 -7
- data/lib/micro/attributes/features.rb +1 -1
- data/lib/micro/attributes/version.rb +1 -1
- data/lib/micro/attributes.rb +6 -2
- data/{run-tests.sh → test.sh} +0 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1f5001cf28666ddb39188b98a736ef80375e7ff91367aa3a84183e524b698e2
|
4
|
+
data.tar.gz: c260e6a0e12dd6d9edf251318b6667b3b13b7cd9d6964f8ff5defa80679b9a67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3855b41867f2b635d2687766baf6c6bcdc15c5b7a149879b3640ea327071e45f564b1438a66dfbd7f11a90b6a7f9b6c7265b2b8d6732ad71b1a2203839113d14
|
7
|
+
data.tar.gz: 3e322ab6d47de45fe6120969ae876be1fa936d6a639cbc7346ce70b5f400ad801d8a7b83640b864d80dd7f008327d9bc9d47af286a4feea59300a07aa3a459c2
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,29 @@
|
|
1
|
-
|
1
|
+
[](https://travis-ci.com/serradura/u-attributes) [](https://codeclimate.com/github/serradura/u-attributes/maintainability)
|
2
|
+
|
3
|
+
μ-attributes (Micro::Attributes)
|
4
|
+
================================
|
2
5
|
|
3
6
|
This gem allows defining read-only attributes, that is, your objects will have only getters to access their attributes data.
|
4
7
|
|
8
|
+
## Table of contents
|
9
|
+
- [μ-attributes (Micro::Attributes)](#%CE%BC-attributes-MicroAttributes)
|
10
|
+
- [Table of contents](#Table-of-contents)
|
11
|
+
- [Installation](#Installation)
|
12
|
+
- [Usage](#Usage)
|
13
|
+
- [How to require?](#How-to-require)
|
14
|
+
- [How to define attributes?](#How-to-define-attributes)
|
15
|
+
- [How to define multiple attributes?](#How-to-define-multiple-attributes)
|
16
|
+
- [How to define attributes with a constructor to assign them?](#How-to-define-attributes-with-a-constructor-to-assign-them)
|
17
|
+
- [How to query the attributes?](#How-to-query-the-attributes)
|
18
|
+
- [Built-in extensions](#Built-in-extensions)
|
19
|
+
- [ActiveModel::Validations extension](#ActiveModelValidations-extension)
|
20
|
+
- [Diff extension](#Diff-extension)
|
21
|
+
- [Initialize extension](#Initialize-extension)
|
22
|
+
- [Development](#Development)
|
23
|
+
- [Contributing](#Contributing)
|
24
|
+
- [License](#License)
|
25
|
+
- [Code of Conduct](#Code-of-Conduct)
|
26
|
+
|
5
27
|
## Installation
|
6
28
|
|
7
29
|
Add this line to your application's Gemfile:
|
@@ -268,7 +290,7 @@ p Person.new(name: 'John').attributes # {"age"=>nil, "name"=>"John"}
|
|
268
290
|
|
269
291
|
You can use the method `Micro::Attributes.features()` or `Micro::Attributes.with()` to combine and require only the features that better fit your needs.
|
270
292
|
|
271
|
-
|
293
|
+
Note: If you desire require only one feature, use the `Micro::Attributes.feature()` method.
|
272
294
|
|
273
295
|
```ruby
|
274
296
|
#----------------------------------#
|
@@ -278,7 +300,7 @@ You can use the method `Micro::Attributes.features()` or `Micro::Attributes.with
|
|
278
300
|
# Loading specific features
|
279
301
|
|
280
302
|
class Job
|
281
|
-
include Micro::Attributes.
|
303
|
+
include Micro::Attributes.feature(:diff)
|
282
304
|
|
283
305
|
attribute :id
|
284
306
|
attribute :state, 'sleeping'
|
@@ -331,8 +353,6 @@ end
|
|
331
353
|
|
332
354
|
If your application uses ActiveModel as a dependency (like a regular Rails app). You will be enabled to use the `actimodel_validations` extension.
|
333
355
|
|
334
|
-
#### Usage
|
335
|
-
|
336
356
|
```ruby
|
337
357
|
class Job
|
338
358
|
# include Micro::Attributes.with(:initialize, :activemodel_validations)
|
@@ -355,8 +375,6 @@ p job.state # "sleeping"
|
|
355
375
|
|
356
376
|
Provides a way to track changes in your object attributes.
|
357
377
|
|
358
|
-
#### Usage
|
359
|
-
|
360
378
|
```ruby
|
361
379
|
require 'securerandom'
|
362
380
|
|
@@ -403,6 +421,53 @@ p job_changes.changed?(:state, from: 'sleeping', to: 'running') # true
|
|
403
421
|
p job_changes.differences # {"state"=> {"from" => "sleeping", "to" => "running"}}
|
404
422
|
```
|
405
423
|
|
424
|
+
### Initialize extension
|
425
|
+
|
426
|
+
1. Creates a constructor to assign the attributes.
|
427
|
+
2. Adds methods to build new instances when some data was assigned.
|
428
|
+
|
429
|
+
```ruby
|
430
|
+
class Job
|
431
|
+
# include Micro::Attributes.features(:initialize)
|
432
|
+
# include Micro::Attributes.with(:initialize)
|
433
|
+
# include Micro::Attributes.feature(:initialize)
|
434
|
+
include Micro::Attributes.to_initialize
|
435
|
+
|
436
|
+
attributes :id, :state
|
437
|
+
end
|
438
|
+
|
439
|
+
job = Job.new(id: 1, state: 'sleeping')
|
440
|
+
|
441
|
+
p job.id # 1
|
442
|
+
p job.state # "sleeping"
|
443
|
+
|
444
|
+
##############################################
|
445
|
+
# Assigning new values to get a new instance #
|
446
|
+
##############################################
|
447
|
+
|
448
|
+
#-------------------#
|
449
|
+
# #with_attribute() #
|
450
|
+
#-------------------#
|
451
|
+
|
452
|
+
new_job = job.with_attribute(:state, 'running')
|
453
|
+
|
454
|
+
puts new_job.id # 1
|
455
|
+
puts new_job.state # running
|
456
|
+
puts new_job.equal?(job) # false
|
457
|
+
|
458
|
+
#--------------------#
|
459
|
+
# #with_attributes() #
|
460
|
+
#--------------------#
|
461
|
+
#
|
462
|
+
# Use it to assign multiple attributes
|
463
|
+
|
464
|
+
other_job = job.with_attributes(id: 2, state: 'killed')
|
465
|
+
|
466
|
+
puts other_job.id # 2
|
467
|
+
puts other_job.state # killed
|
468
|
+
puts other_job.equal?(job) # false
|
469
|
+
```
|
470
|
+
|
406
471
|
## Development
|
407
472
|
|
408
473
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -25,7 +25,7 @@ module Micro
|
|
25
25
|
With::ActiveModelValidationsAndDiffAndInitialize
|
26
26
|
end
|
27
27
|
|
28
|
-
def self.
|
28
|
+
def self.with(names)
|
29
29
|
option = OPTIONS[names.map { |name| name.to_s.downcase }.sort.join(':')]
|
30
30
|
return option if option
|
31
31
|
raise ArgumentError, INVALID_FEATURES
|
data/lib/micro/attributes.rb
CHANGED
@@ -30,11 +30,15 @@ module Micro
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def self.with(*names)
|
33
|
-
Features.
|
33
|
+
Features.with(names)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.feature(name)
|
37
|
+
self.with(name)
|
34
38
|
end
|
35
39
|
|
36
40
|
def self.features(*names)
|
37
|
-
names.empty? ? Features.all : Features.
|
41
|
+
names.empty? ? Features.all : Features.with(names)
|
38
42
|
end
|
39
43
|
|
40
44
|
def attributes=(arg)
|
data/{run-tests.sh → test.sh}
RENAMED
File without changes
|
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.14.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-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -53,7 +53,7 @@ files:
|
|
53
53
|
- lib/micro/attributes/version.rb
|
54
54
|
- lib/micro/attributes/with.rb
|
55
55
|
- lib/u-attributes.rb
|
56
|
-
-
|
56
|
+
- test.sh
|
57
57
|
- u-attributes.gemspec
|
58
58
|
homepage: https://github.com/serradura/u-attributes
|
59
59
|
licenses:
|