u-attributes 0.2.0 → 0.3.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 +2 -3
- data/README.md +116 -4
- data/lib/micro/attributes/macros.rb +1 -1
- data/lib/micro/attributes/version.rb +1 -1
- data/lib/micro/attributes.rb +12 -3
- data/micro-attributes.gemspec +2 -1
- metadata +6 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4ffcf4806ec9d80d37275acd60cae7b17e631eb1
|
4
|
+
data.tar.gz: 097f3021f4926e30f1fae6f013546e720cdde813
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13d1ef7f388acad2924444ffc0895efe77f4dda23cbbe7bccb1043fb438bc5489d2da02b9497f79f5e60a0d9c6734a1df53e34be7411eaeebde66fbd94168d78
|
7
|
+
data.tar.gz: 18adb33e86569aa4f0a0b6bb0c0f390bab59b895d71c79076f2e6ba158f1733b0f8df64a2926a34fe90deed491150fbe2f3553761593a70eebe4a32715505d33
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
u-attributes (0.
|
4
|
+
u-attributes (0.3.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -20,11 +20,10 @@ PLATFORMS
|
|
20
20
|
ruby
|
21
21
|
|
22
22
|
DEPENDENCIES
|
23
|
-
bundler (~> 2.0)
|
24
23
|
minitest (~> 5.0)
|
25
24
|
rake (~> 10.0)
|
26
25
|
simplecov
|
27
26
|
u-attributes!
|
28
27
|
|
29
28
|
BUNDLED WITH
|
30
|
-
|
29
|
+
1.17.3
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Micro::Attributes
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
This gem allows defining read-only attributes, that is, your objects will have only getters to access their attributes data.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,121 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
### How to require?
|
24
|
+
```ruby
|
25
|
+
# Bundler will do it automatically, but if you desire to do a manual require.
|
26
|
+
# Use one of the following options:
|
27
|
+
|
28
|
+
require 'micro/attributes'
|
29
|
+
|
30
|
+
# or
|
31
|
+
|
32
|
+
require 'u-attributes'
|
33
|
+
```
|
34
|
+
|
35
|
+
### How to define attributes?
|
36
|
+
```ruby
|
37
|
+
|
38
|
+
# By default you must to define the class constructor.
|
39
|
+
|
40
|
+
class Person
|
41
|
+
include Micro::Attributes
|
42
|
+
|
43
|
+
attribute :name
|
44
|
+
attribute :age
|
45
|
+
|
46
|
+
def initialize(name: 'John Doe', age:)
|
47
|
+
@name, @age = name, age
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
person = Person.new(age: 21)
|
52
|
+
|
53
|
+
puts person.name # John Doe
|
54
|
+
puts person.age # 21
|
55
|
+
|
56
|
+
# By design, the attributes expose only reader methods (getters).
|
57
|
+
# If you try to call a setter, you will see a NoMethodError.
|
58
|
+
#
|
59
|
+
# person.name = 'Rodrigo'
|
60
|
+
# NoMethodError (undefined method `name=' for #<Person:0x0000... @name="John Doe", @age=21>)
|
61
|
+
```
|
62
|
+
|
63
|
+
### How to define multiple attributes?
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
|
67
|
+
# Use .attributes with a list of attribute names.
|
68
|
+
|
69
|
+
class Person
|
70
|
+
include Micro::Attributes
|
71
|
+
|
72
|
+
attributes :name, :age
|
73
|
+
|
74
|
+
def initialize(name, age)
|
75
|
+
@name, @age = name, age
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
person = Person.new('Serradura', 32)
|
80
|
+
|
81
|
+
puts person.name # Serradura
|
82
|
+
puts person.age # 32
|
83
|
+
```
|
84
|
+
|
85
|
+
### How to define attributes with a constructor to assign them?
|
86
|
+
A: Use `Micro::Attributes.to_initialize`
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
class Person
|
90
|
+
include Micro::Attributes.to_initialize
|
91
|
+
|
92
|
+
attributes :age, name: 'John Doe' # Use a hash to define a default value
|
93
|
+
|
94
|
+
# attribute name: 'John Doe'
|
95
|
+
# attribute :age
|
96
|
+
end
|
97
|
+
|
98
|
+
person = Person.new(age: 18)
|
99
|
+
|
100
|
+
puts person.name # John Doe
|
101
|
+
puts person.age # 18
|
102
|
+
|
103
|
+
##############################################
|
104
|
+
# Assigning new values to get a new instance #
|
105
|
+
##############################################
|
106
|
+
|
107
|
+
another_person = person.with_attribute(:age, 21)
|
108
|
+
|
109
|
+
puts another_person.name # John Doe
|
110
|
+
puts another_person.age # 21
|
111
|
+
puts another_person.equal?(person) # false
|
112
|
+
|
113
|
+
# Use #with_attributes to assign multiple attributes
|
114
|
+
other_person = person.with_attributes(name: 'Serradura', age: 32)
|
115
|
+
|
116
|
+
puts other_person.name # Serradura
|
117
|
+
puts other_person.age # 32
|
118
|
+
puts other_person.equal?(person) # false
|
119
|
+
|
120
|
+
# If you pass a value different of a Hash, an ArgumentError will be raised.
|
121
|
+
#
|
122
|
+
# Person.new(1)
|
123
|
+
# ArgumentError (argument must be a Hash)
|
124
|
+
|
125
|
+
#########################################################
|
126
|
+
# Inheritance will preserve the parent class attributes #
|
127
|
+
#########################################################
|
128
|
+
class Subclass < Person
|
129
|
+
attribute :foo
|
130
|
+
end
|
131
|
+
|
132
|
+
instance = Subclass.new({})
|
133
|
+
|
134
|
+
puts instance.name # John Doe
|
135
|
+
puts instance.respond_to?(:age) # true
|
136
|
+
puts instance.respond_to?(:foo) # true
|
137
|
+
```
|
26
138
|
|
27
139
|
## Development
|
28
140
|
|
data/lib/micro/attributes.rb
CHANGED
@@ -32,9 +32,18 @@ module Micro
|
|
32
32
|
base.send(:include, Micro::Attributes)
|
33
33
|
|
34
34
|
base.class_eval(<<-RUBY)
|
35
|
-
def initialize(
|
36
|
-
|
37
|
-
|
35
|
+
def initialize(arg)
|
36
|
+
raise ArgumentError, 'argument must be a Hash' unless arg.is_a?(Hash)
|
37
|
+
self.attributes = arg
|
38
|
+
end
|
39
|
+
|
40
|
+
def with_attribute(key, val)
|
41
|
+
self.class.new(attributes.merge(key => val))
|
42
|
+
end
|
43
|
+
|
44
|
+
def with_attributes(arg)
|
45
|
+
self.class.new(attributes.merge(arg))
|
46
|
+
end
|
38
47
|
RUBY
|
39
48
|
end
|
40
49
|
end
|
data/micro-attributes.gemspec
CHANGED
@@ -10,6 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["rodrigo.serradura@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Define read-only attributes}
|
13
|
+
spec.description = %q{This gem allows defining read-only attributes, that is, your objects will have only getters to access their attributes data.}
|
13
14
|
spec.homepage = "https://github.com/serradura/u-attributes"
|
14
15
|
spec.license = "MIT"
|
15
16
|
|
@@ -22,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
22
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
24
|
spec.require_paths = ["lib"]
|
24
25
|
|
25
|
-
spec.
|
26
|
+
spec.required_ruby_version = '>= 2.2.0'
|
26
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
27
28
|
spec.add_development_dependency "minitest", "~> 5.0"
|
28
29
|
end
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
@@ -10,20 +10,6 @@ bindir: exe
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2019-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '2.0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '2.0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,7 +38,8 @@ dependencies:
|
|
52
38
|
- - "~>"
|
53
39
|
- !ruby/object:Gem::Version
|
54
40
|
version: '5.0'
|
55
|
-
description:
|
41
|
+
description: This gem allows defining read-only attributes, that is, your objects
|
42
|
+
will have only getters to access their attributes data.
|
56
43
|
email:
|
57
44
|
- rodrigo.serradura@gmail.com
|
58
45
|
executables: []
|
@@ -86,14 +73,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
73
|
requirements:
|
87
74
|
- - ">="
|
88
75
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
76
|
+
version: 2.2.0
|
90
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
78
|
requirements:
|
92
79
|
- - ">="
|
93
80
|
- !ruby/object:Gem::Version
|
94
81
|
version: '0'
|
95
82
|
requirements: []
|
96
|
-
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.4.5
|
97
85
|
signing_key:
|
98
86
|
specification_version: 4
|
99
87
|
summary: Define read-only attributes
|