dymos 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dymos/model.rb +12 -2
- data/lib/dymos/persistence.rb +6 -2
- data/lib/dymos/version.rb +1 -1
- data/spec/lib/dymos/model_spec.rb +61 -26
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f5e6582dcf6a0992c22f3d13d6fcb87fec0bd0f
|
4
|
+
data.tar.gz: bc4a0800891429d8a80eeeeb08c2250f594c3709
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1053e6b53a07c7cd72357860c883e46b5d38151fb3fe0f0cca29391a36501814b21359442c21d20c64b0b1c875b37a3983020cc3cd705758104e2513883e32ce
|
7
|
+
data.tar.gz: 1204f174b48bc4a007bfa973738f5e4c06d03e7eab8e6a03e3c38dfefbd05935669ae833ee49c4407ab307e477d3a355aa947c47f8ca924c003d8d20fc04d8e7
|
data/lib/dymos/model.rb
CHANGED
@@ -5,10 +5,13 @@ module Dymos
|
|
5
5
|
class Model
|
6
6
|
include ActiveModel::Model
|
7
7
|
include ActiveModel::Dirty
|
8
|
+
include ActiveModel::Callbacks
|
8
9
|
include Dymos::Persistence
|
9
10
|
extend Dymos::Command
|
10
11
|
attr_accessor :metadata
|
11
12
|
|
13
|
+
define_model_callbacks :save
|
14
|
+
|
12
15
|
def initialize(params={})
|
13
16
|
@attributes={}
|
14
17
|
send :attributes=, params, true
|
@@ -17,7 +20,7 @@ module Dymos
|
|
17
20
|
|
18
21
|
def self.field(attr, type, default: nil, desc: nil)
|
19
22
|
fail StandardError('attribute name is invalid') if attr =~ /[\!\?]$/
|
20
|
-
fail StandardError('require "default" option') if(type == :bool && default.nil?)
|
23
|
+
fail StandardError('require "default" option') if (type == :bool && default.nil?)
|
21
24
|
|
22
25
|
@fields ||= {}
|
23
26
|
@fields[attr]={
|
@@ -25,10 +28,12 @@ module Dymos
|
|
25
28
|
default: default
|
26
29
|
}
|
27
30
|
define_attribute_methods attr
|
31
|
+
|
32
|
+
define_model_callbacks attr
|
28
33
|
define_method(attr) { |raw=false|
|
34
|
+
run_callbacks attr do
|
29
35
|
val = read_attribute(attr) || default
|
30
36
|
return val if raw || !val.present?
|
31
|
-
|
32
37
|
case type
|
33
38
|
when :bool
|
34
39
|
to_b(val)
|
@@ -39,6 +44,7 @@ module Dymos
|
|
39
44
|
else
|
40
45
|
val
|
41
46
|
end
|
47
|
+
end
|
42
48
|
|
43
49
|
}
|
44
50
|
define_method("#{attr}_type") { type }
|
@@ -51,9 +57,13 @@ module Dymos
|
|
51
57
|
!val.nil?
|
52
58
|
end
|
53
59
|
end
|
60
|
+
|
61
|
+
define_model_callbacks :"set_#{attr}"
|
54
62
|
define_method("#{attr}=") do |value, initialize=false|
|
63
|
+
run_callbacks :"set_#{attr}" do
|
55
64
|
value = value.iso8601 if self.class.fields.include?(attr) && value.is_a?(Time)
|
56
65
|
write_attribute(attr, value, initialize)
|
66
|
+
end
|
57
67
|
end
|
58
68
|
end
|
59
69
|
|
data/lib/dymos/persistence.rb
CHANGED
@@ -20,13 +20,17 @@ module Dymos
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def save(*)
|
23
|
-
|
23
|
+
run_callbacks :save do
|
24
|
+
create_or_update
|
25
|
+
end
|
24
26
|
rescue => e
|
25
27
|
false
|
26
28
|
end
|
27
29
|
|
28
30
|
def save!(*)
|
29
|
-
|
31
|
+
run_callbacks :save do
|
32
|
+
create_or_update || raise(Dymos::RecordNotSaved)
|
33
|
+
end
|
30
34
|
end
|
31
35
|
|
32
36
|
def delete
|
data/lib/dymos/version.rb
CHANGED
@@ -49,39 +49,39 @@ describe Dymos::Model do
|
|
49
49
|
|
50
50
|
client.delete_table(table_name: 'dummy') if client.list_tables[:table_names].include?('dummy')
|
51
51
|
client.create_table(
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
52
|
+
table_name: 'dummy',
|
53
|
+
attribute_definitions: [
|
54
|
+
{attribute_name: 'id', attribute_type: 'S'}
|
55
|
+
],
|
56
|
+
key_schema: [
|
57
|
+
{attribute_name: 'id', key_type: 'HASH'}
|
58
|
+
],
|
59
|
+
provisioned_throughput: {
|
60
|
+
read_capacity_units: 1,
|
61
|
+
write_capacity_units: 1,
|
62
|
+
})
|
63
63
|
client.put_item(table_name: 'dummy', item: {id: 'hoge', name: '太郎', list: Set['a', 'b', 'c']})
|
64
64
|
client.put_item(table_name: 'dummy', item: {id: 'fuga', name: '次郎'})
|
65
65
|
client.put_item(table_name: 'dummy', item: {id: 'piyo', name: '三郎'})
|
66
66
|
client.put_item(table_name: 'dummy', item: {id: 'musashi', name: '巴'}) #削除用
|
67
|
-
client.put_item(table_name: 'dummy', item: {id: 'enable_id', name: 'enable', enable:1})
|
68
|
-
client.put_item(table_name: 'dummy', item: {id: 'disable_id', name: 'disable', enable:0})
|
67
|
+
client.put_item(table_name: 'dummy', item: {id: 'enable_id', name: 'enable', enable: 1})
|
68
|
+
client.put_item(table_name: 'dummy', item: {id: 'disable_id', name: 'disable', enable: 0})
|
69
69
|
|
70
70
|
client.delete_table(table_name: 'post') if client.list_tables[:table_names].include?('post')
|
71
71
|
client.create_table(
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
72
|
+
table_name: 'post',
|
73
|
+
attribute_definitions: [
|
74
|
+
{attribute_name: 'id', attribute_type: 'S'},
|
75
|
+
{attribute_name: 'timestamp', attribute_type: 'N'},
|
76
|
+
],
|
77
|
+
key_schema: [
|
78
|
+
{attribute_name: 'id', key_type: 'HASH'},
|
79
|
+
{attribute_name: 'timestamp', key_type: 'RANGE'},
|
80
|
+
],
|
81
|
+
provisioned_throughput: {
|
82
|
+
read_capacity_units: 1,
|
83
|
+
write_capacity_units: 1,
|
84
|
+
})
|
85
85
|
end
|
86
86
|
|
87
87
|
# let(:model) { Dummy.new }
|
@@ -323,5 +323,40 @@ describe Dymos::Model do
|
|
323
323
|
end
|
324
324
|
end
|
325
325
|
end
|
326
|
+
|
327
|
+
describe :callbacks do
|
328
|
+
class DummyArticle < Dymos::Model
|
329
|
+
table :dummy
|
330
|
+
field :id, :integer
|
331
|
+
field :title, :string
|
332
|
+
field :updated_at, :time
|
333
|
+
|
334
|
+
after_set_title do
|
335
|
+
self.updated_at = Time.now
|
336
|
+
end
|
337
|
+
end
|
338
|
+
it 'タイトルが挿入されるとタイトルを2回繰り返して更新時間が自動的に挿入される' do
|
339
|
+
article = DummyArticle.new
|
340
|
+
expect(article.title).to eq(nil)
|
341
|
+
expect(article.updated_at).to eq(nil)
|
342
|
+
article.title="hoge"*2
|
343
|
+
expect(article.title).to eq("hogehoge")
|
344
|
+
expect(article.updated_at.class).to eq(Time)
|
345
|
+
end
|
346
|
+
class DummyTable < Dymos::Model
|
347
|
+
table :dummy
|
348
|
+
before_save do
|
349
|
+
fail StandardError
|
350
|
+
end
|
351
|
+
end
|
352
|
+
it "DummyTableをsaveするとfalseを返す" do
|
353
|
+
d = DummyTable.new
|
354
|
+
result=d.save
|
355
|
+
expect(result).to eq(false)
|
356
|
+
end
|
357
|
+
it "DummyTableをsave!すると例外を返す" do
|
358
|
+
expect{DummyTable.new.save!}.to raise_error
|
359
|
+
end
|
360
|
+
end
|
326
361
|
end
|
327
362
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dymos
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hoshina85
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|