u-attributes 2.2.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.sh +33 -19
- data/.travis.yml +12 -7
- data/Gemfile +15 -10
- data/README.md +192 -43
- data/lib/micro/attributes.rb +70 -20
- data/lib/micro/attributes/diff.rb +26 -14
- data/lib/micro/attributes/features.rb +128 -75
- data/lib/micro/attributes/features/accept.rb +132 -0
- data/lib/micro/attributes/features/accept/strict.rb +26 -0
- data/lib/micro/attributes/features/activemodel_validations.rb +53 -9
- data/lib/micro/attributes/features/initialize.rb +3 -8
- data/lib/micro/attributes/features/keys_as_symbol.rb +31 -0
- data/lib/micro/attributes/macros.rb +146 -27
- data/lib/micro/attributes/utils.rb +42 -22
- data/lib/micro/attributes/version.rb +1 -1
- data/test.sh +7 -3
- data/u-attributes.gemspec +5 -6
- metadata +14 -14
- data/assets/u-attributes_logo_v1.png +0 -0
- data/lib/micro/attributes/with.rb +0 -100
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c330847c820a244e35afcd702510d791a72822677ad97a4050b37dc0a9653a00
|
4
|
+
data.tar.gz: 66f7e86e1c1579f64d39e77c54eac7fab03551dbbc8fd6ce02438fab817c0939
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d06af39a1601f4ba9496e5daec023cb13d1944f0ab2b986320a365788df2208fecf1122c73d023c669647fb3f826eca15d0f4bf132872c6b84459bd13c8037ce
|
7
|
+
data.tar.gz: 16bd3897ce372901db54506761dffbc3ba42a72e6700fabc0db83fedad6ab8eaf9bb69f1d98c26e4c0eee809311d51926431807fde187af3b80a6cdba2d73f78
|
data/.travis.sh
CHANGED
@@ -1,31 +1,45 @@
|
|
1
1
|
#!/bin/bash
|
2
2
|
|
3
|
-
|
3
|
+
RUBY_V=$(ruby -v)
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
function run_with_bundler {
|
6
|
+
rm Gemfile.lock
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
if [ ! -z "$1" ]; then
|
9
|
+
bundle_cmd="bundle _$1_"
|
10
|
+
else
|
11
|
+
bundle_cmd="bundle"
|
12
|
+
fi
|
10
13
|
|
11
|
-
|
12
|
-
|
14
|
+
eval "$2 $bundle_cmd update"
|
15
|
+
eval "$2 $bundle_cmd exec rake test"
|
16
|
+
}
|
13
17
|
|
14
|
-
|
15
|
-
|
18
|
+
function run_with_am_version_and_bundler {
|
19
|
+
run_with_bundler "$2" "ACTIVEMODEL_VERSION=$1"
|
20
|
+
}
|
16
21
|
|
17
|
-
|
18
|
-
ACTIVEMODEL_VERSION='5.0' bundle exec rake test
|
22
|
+
RUBY_2_2345="ruby 2.[2345]."
|
19
23
|
|
20
|
-
|
21
|
-
|
24
|
+
if [[ $RUBY_V =~ $RUBY_2_2345 ]]; then
|
25
|
+
run_with_bundler "$BUNDLER_V1"
|
22
26
|
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
run_with_am_version_and_bundler "3.2" "$BUNDLER_V1"
|
28
|
+
run_with_am_version_and_bundler "4.0" "$BUNDLER_V1"
|
29
|
+
run_with_am_version_and_bundler "4.1" "$BUNDLER_V1"
|
30
|
+
run_with_am_version_and_bundler "4.2" "$BUNDLER_V1"
|
31
|
+
run_with_am_version_and_bundler "5.0" "$BUNDLER_V1"
|
32
|
+
run_with_am_version_and_bundler "5.1" "$BUNDLER_V1"
|
33
|
+
run_with_am_version_and_bundler "5.2" "$BUNDLER_V1"
|
26
34
|
fi
|
27
35
|
|
28
|
-
|
29
|
-
|
30
|
-
|
36
|
+
RUBY_2_567="ruby 2.[567]."
|
37
|
+
RUBY_3_x_x="ruby 3.0."
|
38
|
+
|
39
|
+
if [[ $RUBY_V =~ $RUBY_2_567 ]] || [[ $RUBY_V =~ $RUBY_3_x_x ]]; then
|
40
|
+
gem install bundler -v ">= 2" --no-doc
|
41
|
+
|
42
|
+
run_with_bundler
|
43
|
+
run_with_am_version_and_bundler "6.0"
|
44
|
+
run_with_am_version_and_bundler "6.1"
|
31
45
|
fi
|
data/.travis.yml
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
language: ruby
|
2
2
|
|
3
|
+
cache:
|
4
|
+
bundler: true
|
5
|
+
directories:
|
6
|
+
- /home/travis/.rvm/
|
7
|
+
|
3
8
|
rvm:
|
4
9
|
- 2.2.2
|
5
10
|
- 2.3.0
|
@@ -7,22 +12,22 @@ rvm:
|
|
7
12
|
- 2.5.0
|
8
13
|
- 2.6.0
|
9
14
|
- 2.7.0
|
10
|
-
-
|
15
|
+
- 3.0.0
|
11
16
|
|
12
|
-
|
17
|
+
env:
|
18
|
+
- BUNDLER_V1="1.17.3"
|
13
19
|
|
14
20
|
before_install:
|
15
|
-
- gem
|
16
|
-
- gem install bundler -v '< 2'
|
21
|
+
- gem install bundler -v "$BUNDLER_V1"
|
17
22
|
|
18
23
|
install: bundle install --jobs=3 --retry=3
|
19
24
|
|
20
25
|
before_script:
|
21
26
|
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
22
27
|
- chmod +x ./cc-test-reporter
|
23
|
-
-
|
28
|
+
- "./cc-test-reporter before-build"
|
24
29
|
|
25
|
-
script:
|
30
|
+
script: "./.travis.sh"
|
26
31
|
|
27
32
|
after_success:
|
28
|
-
-
|
33
|
+
- "./cc-test-reporter after-build -t simplecov"
|
data/Gemfile
CHANGED
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
gem 'u-case', '~> 4.0'
|
4
4
|
|
5
|
-
activemodel_version = ENV
|
5
|
+
activemodel_version = ENV['ACTIVEMODEL_VERSION']
|
6
6
|
|
7
7
|
activemodel = case activemodel_version
|
8
8
|
when '3.2' then '3.2.22'
|
@@ -11,26 +11,31 @@ activemodel = case activemodel_version
|
|
11
11
|
when '4.2' then '4.2.11'
|
12
12
|
when '5.0' then '5.0.7'
|
13
13
|
when '5.1' then '5.1.7'
|
14
|
-
when '5.2' then '5.2.
|
15
|
-
when '6.0' then '6.0.
|
14
|
+
when '5.2' then '5.2.4'
|
15
|
+
when '6.0' then '6.0.3.4'
|
16
|
+
when '6.1' then '6.1.2'
|
16
17
|
end
|
17
18
|
|
18
|
-
if activemodel_version < '6.1'
|
19
|
-
gem 'activemodel', activemodel, require: false
|
20
|
-
gem 'activesupport', activemodel, require: false
|
21
|
-
end
|
22
|
-
|
23
19
|
simplecov_version =
|
24
20
|
case RUBY_VERSION
|
25
|
-
when /\A2.[23]/ then '
|
21
|
+
when /\A2.[23]/ then '0.17.1'
|
26
22
|
when /\A2.4/ then '~> 0.18.5'
|
27
23
|
else '~> 0.19'
|
28
24
|
end
|
29
25
|
|
30
26
|
group :test do
|
31
|
-
|
27
|
+
if activemodel_version
|
28
|
+
gem 'activesupport', activemodel, require: false
|
29
|
+
gem 'activemodel', activemodel, require: false
|
30
|
+
gem 'minitest', activemodel_version < '4.1' ? '~> 4.2' : '~> 5.0'
|
31
|
+
else
|
32
|
+
gem 'minitest', '~> 5.0'
|
33
|
+
end
|
34
|
+
|
32
35
|
gem 'simplecov', simplecov_version, require: false
|
33
36
|
end
|
34
37
|
|
38
|
+
gem 'rake', '~> 13.0'
|
39
|
+
|
35
40
|
# Specify your gem's dependencies in u-attributes.gemspec
|
36
41
|
gemspec
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
<p align="center">
|
2
2
|
<img src="./assets/u-attributes_logo_v1.png" alt='Create "immutable" objects. No setters, just getters!'>
|
3
3
|
|
4
|
-
<p align="center"><i>Create "immutable" objects
|
4
|
+
<p align="center"><i>Create "immutable" objects with no setters, just getters.</i></p>
|
5
5
|
<br>
|
6
6
|
</p>
|
7
7
|
|
8
8
|
<p align="center">
|
9
|
-
<img src="https://img.shields.io/badge/ruby
|
9
|
+
<img src="https://img.shields.io/badge/ruby->%3D%202.2.0-ruby.svg?colorA=99004d&colorB=cc0066" alt="Ruby">
|
10
10
|
|
11
11
|
<a href="https://rubygems.org/gems/u-attributes">
|
12
12
|
<img alt="Gem" src="https://img.shields.io/gem/v/u-attributes.svg?style=flat-square">
|
@@ -25,8 +25,16 @@
|
|
25
25
|
</a>
|
26
26
|
</p>
|
27
27
|
|
28
|
-
This gem allows you to define "immutable" objects,
|
29
|
-
So, if you change [[1](#with_attribute)] [[2](#with_attributes)]
|
28
|
+
This gem allows you to define "immutable" objects, when using it your objects will only have getters and no setters.
|
29
|
+
So, if you change [[1](#with_attribute)] [[2](#with_attributes)] an attribute of the object, you’ll have a new object instance. That is, you transform the object instead of modifying it.
|
30
|
+
|
31
|
+
## Documentation <!-- omit in toc -->
|
32
|
+
|
33
|
+
Version | Documentation
|
34
|
+
---------- | -------------
|
35
|
+
unreleased | https://github.com/serradura/u-case/blob/main/README.md
|
36
|
+
2.7.0 | https://github.com/serradura/u-case/blob/v2.x/README.md
|
37
|
+
1.2.0 | https://github.com/serradura/u-case/blob/v1.x/README.md
|
30
38
|
|
31
39
|
# Table of contents <!-- omit in toc -->
|
32
40
|
- [Installation](#installation)
|
@@ -47,6 +55,15 @@ So, if you change [[1](#with_attribute)] [[2](#with_attributes)] some object att
|
|
47
55
|
- [Is it possible to inherit the attributes?](#is-it-possible-to-inherit-the-attributes)
|
48
56
|
- [`.attribute!()`](#attribute)
|
49
57
|
- [How to query the attributes?](#how-to-query-the-attributes)
|
58
|
+
- [`.attributes`](#attributes)
|
59
|
+
- [`.attribute?()`](#attribute-1)
|
60
|
+
- [`#attribute?()`](#attribute-2)
|
61
|
+
- [`#attributes()`](#attributes-1)
|
62
|
+
- [`#attributes(keys_as:)`](#attributeskeys_as)
|
63
|
+
- [`#attributes(*names)`](#attributesnames)
|
64
|
+
- [`#attributes([names])`](#attributesnames-1)
|
65
|
+
- [`#attributes(with:, without:)`](#attributeswith-without)
|
66
|
+
- [`#defined_attributes`](#defined_attributes)
|
50
67
|
- [Built-in extensions](#built-in-extensions)
|
51
68
|
- [Picking specific features](#picking-specific-features)
|
52
69
|
- [`Micro::Attributes.with`](#microattributeswith)
|
@@ -58,6 +75,7 @@ So, if you change [[1](#with_attribute)] [[2](#with_attributes)] some object att
|
|
58
75
|
- [Diff extension](#diff-extension)
|
59
76
|
- [Initialize extension](#initialize-extension)
|
60
77
|
- [Strict mode](#strict-mode)
|
78
|
+
- [Keys as symbol extension](#keys-as-symbol-extension)
|
61
79
|
- [Development](#development)
|
62
80
|
- [Contributing](#contributing)
|
63
81
|
- [License](#license)
|
@@ -75,7 +93,8 @@ gem 'u-attributes'
|
|
75
93
|
|
76
94
|
| u-attributes | branch | ruby | activemodel |
|
77
95
|
| -------------- | ------- | -------- | ------------- |
|
78
|
-
|
|
96
|
+
| unreleased | main | >= 2.2.0 | >= 3.2, < 7 |
|
97
|
+
| 2.7.0 | v2.x | >= 2.2.0 | >= 3.2, < 7 |
|
79
98
|
| 1.2.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
80
99
|
|
81
100
|
> **Note**: The activemodel is an optional dependency, this module [can be enabled](#activemodelvalidation-extension) to validate the attributes.
|
@@ -419,69 +438,155 @@ beta_person.age # 0
|
|
419
438
|
|
420
439
|
## How to query the attributes?
|
421
440
|
|
441
|
+
All of the methods that will be explained can be used with any of the built-in extensions.
|
442
|
+
|
443
|
+
**PS:** We will use the class below for all of the next examples.
|
444
|
+
|
422
445
|
```ruby
|
423
446
|
class Person
|
424
447
|
include Micro::Attributes
|
425
448
|
|
426
449
|
attribute :age
|
427
|
-
attribute :
|
450
|
+
attribute :first_name, default: 'John'
|
451
|
+
attribute :last_name, default: 'Doe'
|
428
452
|
|
429
453
|
def initialize(options)
|
430
454
|
self.attributes = options
|
431
455
|
end
|
456
|
+
|
457
|
+
def name
|
458
|
+
"#{first_name} #{last_name}"
|
459
|
+
end
|
432
460
|
end
|
461
|
+
```
|
433
462
|
|
434
|
-
|
435
|
-
# .attributes() #
|
436
|
-
#---------------#
|
463
|
+
### `.attributes`
|
437
464
|
|
438
|
-
|
465
|
+
Listing all the class attributes.
|
439
466
|
|
440
|
-
|
441
|
-
#
|
442
|
-
|
467
|
+
```ruby
|
468
|
+
Person.attributes # ["age", "first_name", "last_name"]
|
469
|
+
```
|
443
470
|
|
444
|
-
|
445
|
-
Person.attribute?('name') # true
|
446
|
-
Person.attribute?('foo') # false
|
447
|
-
Person.attribute?(:foo) # false
|
471
|
+
### `.attribute?()`
|
448
472
|
|
449
|
-
|
473
|
+
Checking the existence of some attribute.
|
450
474
|
|
451
|
-
|
475
|
+
```ruby
|
476
|
+
Person.attribute?(:first_name) # true
|
477
|
+
Person.attribute?('first_name') # true
|
478
|
+
|
479
|
+
Person.attribute?('foo') # false
|
480
|
+
Person.attribute?(:foo) # false
|
481
|
+
```
|
452
482
|
|
453
|
-
|
454
|
-
# #defined_attributes #
|
455
|
-
#---------------------#
|
483
|
+
### `#attribute?()`
|
456
484
|
|
457
|
-
|
485
|
+
Checking the existence of some attribute in an instance.
|
458
486
|
|
459
|
-
|
460
|
-
|
461
|
-
#---------------#
|
487
|
+
```ruby
|
488
|
+
person = Person.new(age: 20)
|
462
489
|
|
463
490
|
person.attribute?(:name) # true
|
464
491
|
person.attribute?('name') # true
|
492
|
+
|
465
493
|
person.attribute?('foo') # false
|
466
494
|
person.attribute?(:foo) # false
|
495
|
+
```
|
496
|
+
|
497
|
+
### `#attributes()`
|
498
|
+
|
499
|
+
Fetching all the attributes with their values.
|
500
|
+
|
501
|
+
```ruby
|
502
|
+
person1 = Person.new(age: 20)
|
503
|
+
person1.attributes # {"age"=>20, "first_name"=>"John", "last_name"=>"Doe"}
|
504
|
+
|
505
|
+
person2 = Person.new(first_name: 'Rodrigo', last_name: 'Rodrigues')
|
506
|
+
person2.attributes # {"age"=>nil, "first_name"=>"Rodrigo", "last_name"=>"Rodrigues"}
|
507
|
+
```
|
508
|
+
|
509
|
+
#### `#attributes(keys_as:)`
|
510
|
+
|
511
|
+
Use the `keys_as:` option with `Symbol`/`:symbol` or `String`/`:string` to transform the attributes hash keys.
|
512
|
+
|
513
|
+
```ruby
|
514
|
+
person1 = Person.new(age: 20)
|
515
|
+
person2 = Person.new(first_name: 'Rodrigo', last_name: 'Rodrigues')
|
516
|
+
|
517
|
+
person1.attributes(keys_as: Symbol) # {:age=>20, :first_name=>"John", :last_name=>"Doe"}
|
518
|
+
person2.attributes(keys_as: String) # {"age"=>nil, "first_name"=>"Rodrigo", "last_name"=>"Rodrigues"}
|
519
|
+
|
520
|
+
person1.attributes(keys_as: :symbol) # {:age=>20, :first_name=>"John", :last_name=>"Doe"}
|
521
|
+
person2.attributes(keys_as: :string) # {"age"=>nil, "first_name"=>"Rodrigo", "last_name"=>"Rodrigues"}
|
522
|
+
```
|
523
|
+
|
524
|
+
#### `#attributes(*names)`
|
525
|
+
|
526
|
+
Slices the attributes to include only the given keys (in their types).
|
527
|
+
|
528
|
+
```ruby
|
529
|
+
person = Person.new(age: 20)
|
530
|
+
|
531
|
+
person.attributes(:age) # {:age => 20}
|
532
|
+
person.attributes(:age, :first_name) # {:age => 20, :first_name => "John"}
|
533
|
+
person.attributes('age', 'last_name') # {"age" => 20, "last_name" => "Doe"}
|
534
|
+
|
535
|
+
person.attributes(:age, 'last_name') # {:age => 20, "last_name" => "Doe"}
|
536
|
+
|
537
|
+
# You could also use the keys_as: option to ensure the same type for all of the hash keys.
|
538
|
+
|
539
|
+
person.attributes(:age, 'last_name', keys_as: Symbol) # {:age=>20, :last_name=>"Doe"}
|
540
|
+
```
|
541
|
+
|
542
|
+
#### `#attributes([names])`
|
543
|
+
|
544
|
+
As the previous example, this methods accepts a list of keys to slice the attributes.
|
467
545
|
|
468
|
-
|
469
|
-
|
470
|
-
|
546
|
+
```ruby
|
547
|
+
person = Person.new(age: 20)
|
548
|
+
|
549
|
+
person.attributes([:age]) # {:age => 20}
|
550
|
+
person.attributes([:age, :first_name]) # {:age => 20, :first_name => "John"}
|
551
|
+
person.attributes(['age', 'last_name']) # {"age" => 20, "last_name" => "Doe"}
|
552
|
+
|
553
|
+
person.attributes([:age, 'last_name']) # {:age => 20, "last_name" => "Doe"}
|
554
|
+
|
555
|
+
# You could also use the keys_as: option to ensure the same type for all of the hash keys.
|
556
|
+
|
557
|
+
person.attributes([:age, 'last_name'], keys_as: Symbol) # {:age=>20, :last_name=>"Doe"}
|
558
|
+
```
|
559
|
+
|
560
|
+
#### `#attributes(with:, without:)`
|
561
|
+
|
562
|
+
Use the `with:` option to include any method value of the instance inside of the hash, and,
|
563
|
+
you can use the `without:` option to exclude one or more attribute keys from the final hash.
|
564
|
+
|
565
|
+
```ruby
|
566
|
+
person = Person.new(age: 20)
|
471
567
|
|
472
|
-
person.attributes
|
473
|
-
|
568
|
+
person.attributes(without: :age) # {"first_name"=>"John", "last_name"=>"Doe"}
|
569
|
+
person.attributes(without: [:age, :last_name]) # {"first_name"=>"John"}
|
474
570
|
|
475
|
-
|
476
|
-
# #attributes(*names) #
|
477
|
-
#---------------------#
|
571
|
+
person.attributes(with: [:name], without: [:first_name, :last_name]) # {"age"=>20, "name"=>"John Doe"}
|
478
572
|
|
479
|
-
#
|
480
|
-
# Returns a hash containing the given keys (in their types).
|
573
|
+
# To achieves the same output of the previous example, use the attribute names to slice only them.
|
481
574
|
|
482
|
-
person.attributes(:age)
|
483
|
-
|
484
|
-
|
575
|
+
person.attributes(:age, with: [:name]) # {:age=>20, "name"=>"John Doe"}
|
576
|
+
|
577
|
+
# You could also use the keys_as: option to ensure the same type for all of the hash keys.
|
578
|
+
|
579
|
+
person.attributes(:age, with: [:name], keys_as: Symbol) # {:age=>20, :name=>"John Doe"}
|
580
|
+
```
|
581
|
+
|
582
|
+
### `#defined_attributes`
|
583
|
+
|
584
|
+
Listing all the available attributes.
|
585
|
+
|
586
|
+
```ruby
|
587
|
+
person = Person.new(age: 20)
|
588
|
+
|
589
|
+
person.defined_attributes # ["age", "first_name", "last_name"]
|
485
590
|
```
|
486
591
|
|
487
592
|
[⬆️ Back to Top](#table-of-contents-)
|
@@ -499,24 +604,30 @@ But, if you desire except one or more features, use the `Micro::Attributes.witho
|
|
499
604
|
```ruby
|
500
605
|
Micro::Attributes.with(:initialize)
|
501
606
|
|
502
|
-
Micro::Attributes.with(initialize
|
607
|
+
Micro::Attributes.with(:initialize, :keys_as_symbol)
|
608
|
+
|
609
|
+
Micro::Attributes.with(:keys_as_symbol, initialize: :strict)
|
503
610
|
|
504
611
|
Micro::Attributes.with(:diff, :initialize)
|
505
612
|
|
506
613
|
Micro::Attributes.with(:diff, initialize: :strict)
|
507
614
|
|
615
|
+
Micro::Attributes.with(:diff, :keys_as_symbol, initialize: :strict)
|
616
|
+
|
508
617
|
Micro::Attributes.with(:activemodel_validations)
|
509
618
|
|
510
619
|
Micro::Attributes.with(:activemodel_validations, :diff)
|
511
620
|
|
512
621
|
Micro::Attributes.with(:activemodel_validations, :diff, initialize: :strict)
|
622
|
+
|
623
|
+
Micro::Attributes.with(:activemodel_validations, :diff, :keys_as_symbol, initialize: :strict)
|
513
624
|
```
|
514
625
|
|
515
626
|
The method `Micro::Attributes.with()` will raise an exception if no arguments/features were declared.
|
516
627
|
|
517
628
|
```ruby
|
518
629
|
class Job
|
519
|
-
include Micro::Attributes.with() # ArgumentError (Invalid feature name! Available options: :activemodel_validations, :diff, :initialize)
|
630
|
+
include Micro::Attributes.with() # ArgumentError (Invalid feature name! Available options: :accept, :activemodel_validations, :diff, :initialize, :keys_as_symbol)
|
520
631
|
end
|
521
632
|
```
|
522
633
|
|
@@ -525,15 +636,19 @@ end
|
|
525
636
|
Picking *except* one or more features
|
526
637
|
|
527
638
|
```ruby
|
528
|
-
Micro::Attributes.without(:diff) # will load :activemodel_validations and initialize: :strict
|
639
|
+
Micro::Attributes.without(:diff) # will load :activemodel_validations, :keys_as_symbol and initialize: :strict
|
529
640
|
|
530
|
-
Micro::Attributes.without(initialize: :strict) # will load :activemodel_validations and :
|
641
|
+
Micro::Attributes.without(initialize: :strict) # will load :activemodel_validations, :diff and :keys_as_symbol
|
531
642
|
```
|
532
643
|
|
533
644
|
## Picking all the features
|
534
645
|
|
535
646
|
```ruby
|
536
647
|
Micro::Attributes.with_all_features
|
648
|
+
|
649
|
+
# This method returns the same of:
|
650
|
+
|
651
|
+
Micro::Attributes.with(:activemodel_validations, :diff, :keys_as_symbol, initialize: :strict)
|
537
652
|
```
|
538
653
|
|
539
654
|
[⬆️ Back to Top](#table-of-contents-)
|
@@ -725,6 +840,40 @@ job.state # 'sleeping'
|
|
725
840
|
|
726
841
|
[⬆️ Back to Top](#table-of-contents-)
|
727
842
|
|
843
|
+
### Keys as symbol extension
|
844
|
+
|
845
|
+
Disables the indifferent access requiring the declaration/usage of the attributes as symbols.
|
846
|
+
|
847
|
+
The advantage of this extension over the default behavior is because it avoids an unnecessary allocation in memory of strings. All the keys are transformed into strings in the indifferent access mode, but, with this extension, this typecasting will be avoided. So, it has a better performance and reduces the usage of memory/Garbage collector, but gives for you the responsibility to always use symbols to set/access the attributes.
|
848
|
+
|
849
|
+
```ruby
|
850
|
+
class Job
|
851
|
+
include Micro::Attributes.with(:initialize, :keys_as_symbol)
|
852
|
+
|
853
|
+
attribute :id
|
854
|
+
attribute :state, default: 'sleeping'
|
855
|
+
end
|
856
|
+
|
857
|
+
job = Job.new(id: 1)
|
858
|
+
|
859
|
+
job.attributes # {:id => 1, :state => "sleeping"}
|
860
|
+
|
861
|
+
job.attribute?(:id) # true
|
862
|
+
job.attribute?('id') # false
|
863
|
+
|
864
|
+
job.attribute(:id) # 1
|
865
|
+
job.attribute('id') # nil
|
866
|
+
|
867
|
+
job.attribute!(:id) # 1
|
868
|
+
job.attribute!('id') # NameError (undefined attribute `id)
|
869
|
+
```
|
870
|
+
|
871
|
+
As you could see in the previous example only symbols will work to do something with the attributes.
|
872
|
+
|
873
|
+
This extension also changes the `diff extension` making everything (arguments, outputs) working only with symbols.
|
874
|
+
|
875
|
+
[⬆️ Back to Top](#table-of-contents-)
|
876
|
+
|
728
877
|
# Development
|
729
878
|
|
730
879
|
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.
|