koine-attributes 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -22
- data/lib/koine/attributes.rb +0 -21
- data/lib/koine/attributes/builder.rb +3 -52
- data/lib/koine/attributes/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84fd9c66ed60fd758c16146613035919a09520ea
|
4
|
+
data.tar.gz: 6ea3a4d2842457224c9d38f62611b93e8a227df3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dec0398fc288f205625ae0aa634de9a74494f29f25ecb0f718d56d6ce053f17d2b43935dfedc10364bf5fc47076c513a9cb0f4fdd7bab7dca91d5bc9ada3f7b
|
7
|
+
data.tar.gz: 296f2461691b32d0e31f6c68134ec3905c3853edcf60418708842b3a36fa8039fe05f4c6a0d6f5058c23b4a4b811c4ae1e603a8ee9481dc157773494a3ba24b3
|
data/README.md
CHANGED
@@ -2,11 +2,15 @@
|
|
2
2
|
|
3
3
|
Strong attributes for ruby ruby objects.
|
4
4
|
|
5
|
+
Yes, there are [so many alternative solutions already](#alternative-solutions)! Why then? Cause some times people need distractions at the airports.
|
6
|
+
|
5
7
|
[![Build Status](https://travis-ci.org/mjacobus/koine-attributes.svg?branch=master)](https://travis-ci.org/mjacobus/koine-attributes)
|
6
8
|
[![Coverage Status](https://coveralls.io/repos/github/mjacobus/koine-attributes/badge.svg?branch=master)](https://coveralls.io/github/mjacobus/koine-attributes?branch=master)
|
7
9
|
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mjacobus/koine-attributes/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mjacobus/koine-attributes/?branch=master)
|
8
10
|
[![Code Climate](https://codeclimate.com/github/mjacobus/koine-attributes/badges/gpa.svg)](https://codeclimate.com/github/mjacobus/koine-attributes)
|
9
11
|
[![Issue Count](https://codeclimate.com/github/mjacobus/koine-attributes/badges/issue_count.svg)](https://codeclimate.com/github/mjacobus/koine-attributes)
|
12
|
+
|
13
|
+
[![Gem Version](https://badge.fury.io/rb/koine-attributes.svg)](https://badge.fury.io/rb/koine-command_bus)
|
10
14
|
[![Dependency Status](https://gemnasium.com/badges/github.com/mjacobus/koine-attributes.svg)](https://gemnasium.com/github.com/mjacobus/koine-attributes)
|
11
15
|
|
12
16
|
## Installation
|
@@ -29,8 +33,6 @@ Or install it yourself as:
|
|
29
33
|
|
30
34
|
```ruby
|
31
35
|
class Person
|
32
|
-
include Koine::Attributes
|
33
|
-
|
34
36
|
attributes do
|
35
37
|
attribute :name, :string
|
36
38
|
attribute :birthday, :date
|
@@ -63,8 +65,6 @@ Options:
|
|
63
65
|
|
64
66
|
```ruby
|
65
67
|
class Person
|
66
|
-
include Koine::Attributes
|
67
|
-
|
68
68
|
attributes initializer: true do
|
69
69
|
attribute :name, :string
|
70
70
|
attribute :birthday, :date
|
@@ -81,8 +81,6 @@ Options:
|
|
81
81
|
|
82
82
|
```ruby
|
83
83
|
class Person
|
84
|
-
include Koine::Attributes
|
85
|
-
|
86
84
|
attributes initializer: { strict: false } do
|
87
85
|
attribute :name, :string
|
88
86
|
attribute :birthday, :date
|
@@ -139,22 +137,6 @@ product.price = { currency: 'USD', value: 100 }
|
|
139
137
|
product.price = "100 USD"
|
140
138
|
```
|
141
139
|
|
142
|
-
### Value objects
|
143
|
-
|
144
|
-
```ruby
|
145
|
-
class Location
|
146
|
-
include Koine::Attributes
|
147
|
-
|
148
|
-
attributes initializer: { freeze: true } do
|
149
|
-
attribute :lat, :float
|
150
|
-
attribute :lon, :float
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
location = Location.new(lat: 1, lon: 2)
|
155
|
-
new_location = location.with_lon(3)
|
156
|
-
```
|
157
|
-
|
158
140
|
### Standard types
|
159
141
|
|
160
142
|
```ruby
|
@@ -166,6 +148,12 @@ new_location = location.with_lon(3)
|
|
166
148
|
:time
|
167
149
|
```
|
168
150
|
|
151
|
+
### Alternative solutions
|
152
|
+
|
153
|
+
- [attributed_object](https://github.com/jgroeneveld/attributed_object)
|
154
|
+
- [virtus](https://github.com/solnic/virtus)
|
155
|
+
- [dry-struct](https://github.com/dry-rb/dry-struct)
|
156
|
+
|
169
157
|
## Development
|
170
158
|
|
171
159
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/koine/attributes.rb
CHANGED
@@ -6,8 +6,6 @@ require 'koine/attributes/adapter/base'
|
|
6
6
|
#
|
7
7
|
# @example using attributes
|
8
8
|
# class Person
|
9
|
-
# include Koine::Attributes
|
10
|
-
#
|
11
9
|
# attributes do
|
12
10
|
# attribute :name, :string
|
13
11
|
# attribute :birthday, :date
|
@@ -36,8 +34,6 @@ require 'koine/attributes/adapter/base'
|
|
36
34
|
# @example Constructor for attributes
|
37
35
|
#
|
38
36
|
# class Person
|
39
|
-
# include Koine::Attributes
|
40
|
-
#
|
41
37
|
# attributes initializer: true do
|
42
38
|
# attribute :name, :string
|
43
39
|
# attribute :birthday, :date
|
@@ -52,8 +48,6 @@ require 'koine/attributes/adapter/base'
|
|
52
48
|
# @example Constructor for attributes withouth strict mode
|
53
49
|
#
|
54
50
|
# class Person
|
55
|
-
# include Koine::Attributes
|
56
|
-
#
|
57
51
|
# attributes initializer: { strict: false } do
|
58
52
|
# attribute :name, :string
|
59
53
|
# attribute :birthday, :date
|
@@ -66,8 +60,6 @@ require 'koine/attributes/adapter/base'
|
|
66
60
|
# @example Override constructor
|
67
61
|
#
|
68
62
|
# class Person
|
69
|
-
# include Koine::Attributes
|
70
|
-
#
|
71
63
|
# attr_reader :foo
|
72
64
|
#
|
73
65
|
# attributes initializer: true do
|
@@ -85,19 +77,6 @@ require 'koine/attributes/adapter/base'
|
|
85
77
|
# person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
|
86
78
|
# person.foo # => :bar
|
87
79
|
#
|
88
|
-
# @example
|
89
|
-
# class Location
|
90
|
-
# include Koine::Attributes
|
91
|
-
#
|
92
|
-
# attributes initializer: { freeze: true } do
|
93
|
-
# attribute :lat, :flaot
|
94
|
-
# attribute :lon, :flaot
|
95
|
-
# end
|
96
|
-
# end
|
97
|
-
#
|
98
|
-
# location = Location.new(lat: 1, lon: 2)
|
99
|
-
# new_location = location.with_lon(3)
|
100
|
-
#
|
101
80
|
module Koine
|
102
81
|
module Attributes
|
103
82
|
module Adapter
|
@@ -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)
|
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, constructor_args[name])
|
56
56
|
next
|
57
57
|
end
|
58
58
|
|
@@ -64,55 +64,6 @@ 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
|
-
protected "#{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
|
116
67
|
end
|
117
68
|
end
|
118
69
|
end
|