koine-attributes 0.1.1 → 0.1.2
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/README.md +23 -1
- data/lib/koine/attributes/builder.rb +52 -3
- data/lib/koine/attributes/version.rb +1 -1
- data/lib/koine/attributes.rb +21 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ecad2772d64e09329e15fc0045e95bddc8a1f662
|
4
|
+
data.tar.gz: b5350607c2f3871947146bbc8c2a2e5e489018b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dc9cae66c11704eb19b45c2e36de18e58960da5c678c71b4bd8a6870d7053cf17ba68907e35ef841ca21c39c23ee9d1c98c8a771ce9c71dad0c67414bf9efe0
|
7
|
+
data.tar.gz: 760b696eceabd3bdd3592b7ba59e4f990e9cccbf5240caa591eb73f06b2fee49dddd1ce7e1891fedf4f1bf3e2d84f213e779343a2a7fb5e22de1d5e159ab6df1
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ Yes, there are [so many alternative solutions already](#alternative-solutions)!
|
|
10
10
|
[](https://codeclimate.com/github/mjacobus/koine-attributes)
|
11
11
|
[](https://codeclimate.com/github/mjacobus/koine-attributes)
|
12
12
|
|
13
|
-
[](https://badge.fury.io/rb/koine-
|
13
|
+
[](https://badge.fury.io/rb/koine-attributes)
|
14
14
|
[](https://gemnasium.com/github.com/mjacobus/koine-attributes)
|
15
15
|
|
16
16
|
## Installation
|
@@ -33,6 +33,8 @@ Or install it yourself as:
|
|
33
33
|
|
34
34
|
```ruby
|
35
35
|
class Person
|
36
|
+
include Koine::Attributes
|
37
|
+
|
36
38
|
attributes do
|
37
39
|
attribute :name, :string
|
38
40
|
attribute :birthday, :date
|
@@ -65,6 +67,8 @@ Options:
|
|
65
67
|
|
66
68
|
```ruby
|
67
69
|
class Person
|
70
|
+
include Koine::Attributes
|
71
|
+
|
68
72
|
attributes initializer: true do
|
69
73
|
attribute :name, :string
|
70
74
|
attribute :birthday, :date
|
@@ -81,6 +85,8 @@ Options:
|
|
81
85
|
|
82
86
|
```ruby
|
83
87
|
class Person
|
88
|
+
include Koine::Attributes
|
89
|
+
|
84
90
|
attributes initializer: { strict: false } do
|
85
91
|
attribute :name, :string
|
86
92
|
attribute :birthday, :date
|
@@ -137,6 +143,22 @@ product.price = { currency: 'USD', value: 100 }
|
|
137
143
|
product.price = "100 USD"
|
138
144
|
```
|
139
145
|
|
146
|
+
### Value objects
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
class Location
|
150
|
+
include Koine::Attributes
|
151
|
+
|
152
|
+
attributes initializer: { freeze: true } do
|
153
|
+
attribute :lat, :float
|
154
|
+
attribute :lon, :float
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
location = Location.new(lat: 1, lon: 2)
|
159
|
+
new_location = location.with_lon(3)
|
160
|
+
```
|
161
|
+
|
140
162
|
### Standard types
|
141
163
|
|
142
164
|
```ruby
|
@@ -35,7 +35,7 @@ module Koine
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
def build_constructor(strict: true)
|
38
|
+
def build_constructor(strict: true, freeze: false)
|
39
39
|
valid_attributes = attributes
|
40
40
|
|
41
41
|
target.class_eval do
|
@@ -49,10 +49,10 @@ module Koine
|
|
49
49
|
invalid_attributes = []
|
50
50
|
constructor_args = HashHelper.new.symbolize_keys(constructor_args)
|
51
51
|
|
52
|
-
constructor_args.each do |name,
|
52
|
+
constructor_args.each do |name, value|
|
53
53
|
if valid_attributes.include?(name)
|
54
54
|
setter = "#{name}=".to_sym
|
55
|
-
send(setter,
|
55
|
+
send(setter, value)
|
56
56
|
next
|
57
57
|
end
|
58
58
|
|
@@ -64,6 +64,55 @@ module Koine
|
|
64
64
|
attributes = invalid_attributes.join(', ')
|
65
65
|
raise ArgumentError, "Invalid attributes (#{attributes})"
|
66
66
|
end
|
67
|
+
|
68
|
+
self.freeze if freeze
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
if strict
|
73
|
+
make_setters_private
|
74
|
+
create_with_methods
|
75
|
+
create_comparator
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
def make_setters_private
|
82
|
+
attrs = attributes
|
83
|
+
|
84
|
+
target.instance_eval do
|
85
|
+
attrs.each do |attr|
|
86
|
+
private "#{attr}="
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def create_with_methods
|
92
|
+
attributes.each do |attr|
|
93
|
+
create_with_method(attr)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def create_with_method(attr)
|
98
|
+
target.class_eval do
|
99
|
+
define_method "with_#{attr}" do |*args|
|
100
|
+
dup.tap do |new_object|
|
101
|
+
new_object.send("#{attr}=", *args)
|
102
|
+
new_object.freeze
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def create_comparator
|
109
|
+
attrs = attributes
|
110
|
+
|
111
|
+
target.class_eval do
|
112
|
+
define_method :== do |that|
|
113
|
+
a = attrs.map { |attr| send(attr) }
|
114
|
+
b = attrs.map { |attr| that.send(attr) }
|
115
|
+
a == b
|
67
116
|
end
|
68
117
|
end
|
69
118
|
end
|
data/lib/koine/attributes.rb
CHANGED
@@ -6,6 +6,8 @@ require 'koine/attributes/adapter/base'
|
|
6
6
|
#
|
7
7
|
# @example using attributes
|
8
8
|
# class Person
|
9
|
+
# include Koine::Attributes
|
10
|
+
#
|
9
11
|
# attributes do
|
10
12
|
# attribute :name, :string
|
11
13
|
# attribute :birthday, :date
|
@@ -34,6 +36,8 @@ require 'koine/attributes/adapter/base'
|
|
34
36
|
# @example Constructor for attributes
|
35
37
|
#
|
36
38
|
# class Person
|
39
|
+
# include Koine::Attributes
|
40
|
+
#
|
37
41
|
# attributes initializer: true do
|
38
42
|
# attribute :name, :string
|
39
43
|
# attribute :birthday, :date
|
@@ -48,6 +52,8 @@ require 'koine/attributes/adapter/base'
|
|
48
52
|
# @example Constructor for attributes withouth strict mode
|
49
53
|
#
|
50
54
|
# class Person
|
55
|
+
# include Koine::Attributes
|
56
|
+
#
|
51
57
|
# attributes initializer: { strict: false } do
|
52
58
|
# attribute :name, :string
|
53
59
|
# attribute :birthday, :date
|
@@ -60,6 +66,8 @@ require 'koine/attributes/adapter/base'
|
|
60
66
|
# @example Override constructor
|
61
67
|
#
|
62
68
|
# class Person
|
69
|
+
# include Koine::Attributes
|
70
|
+
#
|
63
71
|
# attr_reader :foo
|
64
72
|
#
|
65
73
|
# attributes initializer: true do
|
@@ -77,6 +85,19 @@ require 'koine/attributes/adapter/base'
|
|
77
85
|
# person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
|
78
86
|
# person.foo # => :bar
|
79
87
|
#
|
88
|
+
# @example
|
89
|
+
# class Location
|
90
|
+
# include Koine::Attributes
|
91
|
+
#
|
92
|
+
# attributes initializer: { freeze: true } do
|
93
|
+
# attribute :lat, :float
|
94
|
+
# attribute :lon, :float
|
95
|
+
# end
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# location = Location.new(lat: 1, lon: 2)
|
99
|
+
# new_location = location.with_lon(3)
|
100
|
+
#
|
80
101
|
module Koine
|
81
102
|
module Attributes
|
82
103
|
module Adapter
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: koine-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcelo Jacobus
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
130
|
version: '0'
|
131
131
|
requirements: []
|
132
132
|
rubyforge_project:
|
133
|
-
rubygems_version: 2.6.
|
133
|
+
rubygems_version: 2.6.11
|
134
134
|
signing_key:
|
135
135
|
specification_version: 4
|
136
136
|
summary: Stronger setters and getters
|