embedded 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 +5 -5
- data/README.md +15 -12
- data/Rakefile +6 -29
- data/lib/embedded/model.rb +5 -3
- data/lib/embedded/version.rb +1 -1
- metadata +68 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c7c55c2185e856455dc2901abd80f43fd4dc5a274c72dd3ad9681bf3f82484e8
|
4
|
+
data.tar.gz: 349aa11195724eb96a93f7933a44b947138458d9c6641dc99a78c3b5b8819b86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e3d49cdb279870a5228ff6e0fdc825abb1e2051d7ee6d56c29b7ac36367d81a2773efd5f88fed86d22dd713bb2cd7842704e92fcf267a21ab131717a6a29038
|
7
|
+
data.tar.gz: 2518c1636bb977e9c595401524ef80341160d66948cededd21257a5f0cf7cb037cb4ec1027a6984da3ce8dfaab441fabd735ea58e5a3a173d9443964fcdd6fbb
|
data/README.md
CHANGED
@@ -2,15 +2,18 @@
|
|
2
2
|
|
3
3
|
Embedded is a small rails engine to correctly persist Value Objects in Active Record Object columns
|
4
4
|
|
5
|
+
## Code Status
|
6
|
+
[![Build Status](https://travis-ci.org/jvillarejo/embedded.svg?branch=master)](https://travis-ci.org/jvillarejo/embedded)
|
7
|
+
|
5
8
|
## Motivation
|
6
9
|
|
7
|
-
There objects in every domain that
|
10
|
+
There are objects in every domain that don't have an identity by themselves but in which their equality depends on the values of their attributes.
|
8
11
|
|
9
12
|
Example: prices, any magnitude, a color, a polygon.
|
10
13
|
|
11
|
-
Defining a value
|
14
|
+
Defining a value object lets you extract common behavior from your current bloated active record objects.
|
12
15
|
|
13
|
-
Every time
|
16
|
+
Every time I did this, I had to define a getter and a setter for the value object, and map those to the columns of the object that gets persisted, so I thought that it would be better to define those value object attributes in a declarative way and let the plugin do the magic behind.
|
14
17
|
|
15
18
|
For more info about value objects check this links:
|
16
19
|
|
@@ -48,7 +51,7 @@ end
|
|
48
51
|
|
49
52
|
## Usage
|
50
53
|
|
51
|
-
Let's say you have a Reservation in your active record model and that
|
54
|
+
Let's say you have a Reservation in your active record model and that it has a start_time and an end_time. And that you want to calculate the duration in hours of the period.
|
52
55
|
|
53
56
|
```ruby
|
54
57
|
class Reservation < ApplicationRecord
|
@@ -83,7 +86,7 @@ Let's say you have a Reservation in your active record model and that reservatio
|
|
83
86
|
# => 3
|
84
87
|
```
|
85
88
|
|
86
|
-
Now you are starting to see the problem. That behavior belongs to a TimeInterval object that has start_time
|
89
|
+
Now you are starting to see the problem. That behavior belongs to a TimeInterval object that has a start_time an end_time and let's you calculate all the durations and intervals you want.
|
87
90
|
|
88
91
|
So with embedded in hand we can do this.
|
89
92
|
|
@@ -137,7 +140,7 @@ Now you can pass available time to shop constructor and check the duration direc
|
|
137
140
|
shop.available_time.hours
|
138
141
|
# => 3
|
139
142
|
```
|
140
|
-
Also you can persist the reservation and when fetching it back
|
143
|
+
Also you can persist the reservation, and when fetching it back from the db its scheduled_time will be a TimeInterval
|
141
144
|
|
142
145
|
```ruby
|
143
146
|
t = TimeInterval.new(start_time: Time.now, end_time: 3.hours.ago)
|
@@ -153,7 +156,7 @@ Also you can persist the reservation and when fetching it back of the db it will
|
|
153
156
|
|
154
157
|
Your table columns have to be named in a specific way so they are mapped correctly, for example:
|
155
158
|
|
156
|
-
Reservation attribute name is scheduled_time and
|
159
|
+
If Reservation attribute name is scheduled_time and its TimeInterval has start_time and end_time attributes, your column names should be defined as followed:
|
157
160
|
|
158
161
|
```ruby
|
159
162
|
class CreateReservations < ActiveRecord::Migration
|
@@ -168,7 +171,7 @@ class CreateReservations < ActiveRecord::Migration
|
|
168
171
|
end
|
169
172
|
```
|
170
173
|
|
171
|
-
Shop attribute name is available time and
|
174
|
+
Shop attribute name is available time, and its TimeInterval has start_time and end_time attributes. Your column names here must be like this:
|
172
175
|
|
173
176
|
```ruby
|
174
177
|
class CreateShops < ActiveRecord::Migration
|
@@ -195,7 +198,7 @@ bubble_price = Price.new(currency: 'USD', amount: BigDecimal.new('5257'))
|
|
195
198
|
my_intelligent_investment = SellOrder.create(price: price, created_at: Time.new(2017,10,18))
|
196
199
|
```
|
197
200
|
|
198
|
-
And we want to check the orders for a specific price we can do this
|
201
|
+
And if we want to check the orders for a specific price we can do it like this:
|
199
202
|
|
200
203
|
```ruby
|
201
204
|
price = Price.new(currency: 'BTC', amount: BigDecimal.new('2.5'))
|
@@ -204,9 +207,9 @@ gambles = BuyOrder.embedded.where(price: price).to_a
|
|
204
207
|
# => [#<Order id: 1, price_currency: "BTC", price_amount: #<BigDecimal:555e61776630,'0.25E1',18(36)>, created_at: "2017-03-17 17:11:00", updated_at: "2017-10-18 17:11:00">]
|
205
208
|
```
|
206
209
|
|
207
|
-
In order to search with
|
210
|
+
In order to search with value objects you should use embedded method. This decision was made because I didn't want to monkey patch the activerecord method 'where'.
|
208
211
|
|
209
|
-
|
212
|
+
This way the embedded method returns another scope in which the method 'where' is overridden. If you want to query by the column attributes you can still use the default 'where' method.
|
210
213
|
|
211
214
|
```ruby
|
212
215
|
jpm_orders = BuyOrder.where(price_currency: 'BTC')
|
@@ -215,7 +218,7 @@ jpm_orders.find_each {|o| o.trader.fire! }
|
|
215
218
|
|
216
219
|
## Contributing
|
217
220
|
|
218
|
-
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
221
|
+
Everyone is encouraged to help to improve this project. Here are a few ways you can help:
|
219
222
|
|
220
223
|
- [Report bugs](https://github.com/jvillarejo/embedded/issues)
|
221
224
|
- Fix bugs and [submit pull requests](https://github.com/jvillarejo/embedded/pulls)
|
data/Rakefile
CHANGED
@@ -1,33 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
rescue LoadError
|
4
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
-
end
|
6
|
-
|
7
|
-
require 'rdoc/task'
|
8
|
-
|
9
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir = 'rdoc'
|
11
|
-
rdoc.title = 'Embedded'
|
12
|
-
rdoc.options << '--line-numbers'
|
13
|
-
rdoc.rdoc_files.include('README.md')
|
14
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
require 'bundler/gem_tasks'
|
23
|
-
|
24
|
-
require 'rake/testtask'
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
25
3
|
|
26
4
|
Rake::TestTask.new(:test) do |t|
|
27
|
-
t.libs <<
|
28
|
-
t.
|
29
|
-
t.
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
30
8
|
end
|
31
9
|
|
32
|
-
|
33
|
-
task default: :test
|
10
|
+
task :default => :test
|
data/lib/embedded/model.rb
CHANGED
@@ -10,9 +10,11 @@ module Embedded
|
|
10
10
|
Embedded::Scope.new(self.all,embedded_attributes)
|
11
11
|
end
|
12
12
|
|
13
|
+
def embedded_attributes
|
14
|
+
@embedded_attributes ||= {}
|
15
|
+
end
|
16
|
+
|
13
17
|
def embeds(embeddable_attr, options = {})
|
14
|
-
cattr_accessor :embedded_attributes
|
15
|
-
self.embedded_attributes ||= {}
|
16
18
|
self.embedded_attributes[embeddable_attr] = options
|
17
19
|
|
18
20
|
attributes = options[:attrs]
|
@@ -28,7 +30,7 @@ module Embedded
|
|
28
30
|
|
29
31
|
self.send(:define_method, :"#{embeddable_attr}=") do |v|
|
30
32
|
columns.each do |k,a|
|
31
|
-
|
33
|
+
write_attribute(k, v.send(a))
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
data/lib/embedded/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,77 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embedded
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jvillarejo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.2.0
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
29
|
+
version: '4.2'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 4.2.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.13'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.13'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: minitest
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '5.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '5.0'
|
27
75
|
- !ruby/object:Gem::Dependency
|
28
76
|
name: sqlite3
|
29
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +86,20 @@ dependencies:
|
|
38
86
|
- - "~>"
|
39
87
|
- !ruby/object:Gem::Version
|
40
88
|
version: '1.3'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: byebug
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '10.0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '10.0'
|
41
103
|
description: Rails plugin that makes value objects embedded into activerecord objects
|
42
104
|
email:
|
43
105
|
- arzivian87@gmail.com
|
@@ -72,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
134
|
version: '0'
|
73
135
|
requirements: []
|
74
136
|
rubyforge_project:
|
75
|
-
rubygems_version: 2.
|
137
|
+
rubygems_version: 2.7.3
|
76
138
|
signing_key:
|
77
139
|
specification_version: 4
|
78
140
|
summary: Use value objects with activerecord objects
|