duck_record 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/duck_record/associations/association.rb +1 -1
- data/lib/duck_record/associations/builder/association.rb +2 -1
- data/lib/duck_record/associations/collection_association.rb +20 -4
- data/lib/duck_record/associations/has_one_association.rb +8 -3
- data/lib/duck_record/errors.rb +15 -0
- data/lib/duck_record/reflection.rb +5 -5
- data/lib/duck_record/version.rb +1 -1
- 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: 94d686747e67e2fd89839e277156b05e45bae20a
|
4
|
+
data.tar.gz: aedff996d41cc23adf9686041eb0817e2db29f65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59898083c6f86b92da56fb15b990c1df664647319def15335f56b414a1fe41133f2fde382eec4738f2eaa29e32cfd4f2f5cf6207559dc1f9be14e14a25abbf31
|
7
|
+
data.tar.gz: b1dd8db43eb6f791bc4c0671202ec0e36a9b59b3a28e5db43b45a1b75a5a53c45a7dcba324aa6697dcfc3160f7e4bcf2ae3985b3fa04c65cd5ecbd3a61a5edb1
|
@@ -77,7 +77,7 @@ module DuckRecord
|
|
77
77
|
unless fresh_class && record.is_a?(fresh_class)
|
78
78
|
message = "#{reflection.class_name}(##{reflection.klass.object_id}) expected, "\
|
79
79
|
"got #{record.inspect} which is an instance of #{record.class}(##{record.class.object_id})"
|
80
|
-
raise
|
80
|
+
raise DuckRecord::AssociationTypeMismatch, message
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
@@ -85,7 +85,8 @@ module DuckRecord::Associations::Builder # :nodoc:
|
|
85
85
|
|
86
86
|
def self.define_writers(mixin, name)
|
87
87
|
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
88
|
-
def #{name}=(value)
|
88
|
+
def #{name}=(value, force_write_readonly: false)
|
89
|
+
return if !force_write_readonly && self.class.readonly_attributes.include?("#{name}")
|
89
90
|
association(:#{name}).writer(value)
|
90
91
|
end
|
91
92
|
CODE
|
@@ -40,7 +40,11 @@ module DuckRecord
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def build(attributes = {}, &block)
|
43
|
-
if attributes.is_a?(
|
43
|
+
if attributes.is_a?(klass)
|
44
|
+
add_to_target(attributes) do |record|
|
45
|
+
yield(record) if block_given?
|
46
|
+
end
|
47
|
+
elsif attributes.is_a?(Array)
|
44
48
|
attributes.collect { |attr| build(attr, &block) }
|
45
49
|
else
|
46
50
|
add_to_target(build_record(attributes)) do |record|
|
@@ -53,8 +57,13 @@ module DuckRecord
|
|
53
57
|
# be chained. Since << flattens its argument list and inserts each record,
|
54
58
|
# +push+ and +concat+ behave identically.
|
55
59
|
def concat(*records)
|
56
|
-
records
|
57
|
-
|
60
|
+
records.flatten.each do |r|
|
61
|
+
begin
|
62
|
+
build(r)
|
63
|
+
rescue
|
64
|
+
raise_on_type_mismatch!(r)
|
65
|
+
end
|
66
|
+
end
|
58
67
|
end
|
59
68
|
|
60
69
|
# Removes all records from the association without calling callbacks
|
@@ -130,7 +139,14 @@ module DuckRecord
|
|
130
139
|
# Replace this collection with +other_array+. This will perform a diff
|
131
140
|
# and delete/add only records that have changed.
|
132
141
|
def replace(other_array)
|
133
|
-
|
142
|
+
delete_all
|
143
|
+
other_array.each do |item|
|
144
|
+
begin
|
145
|
+
build(item)
|
146
|
+
rescue
|
147
|
+
raise_on_type_mismatch!(r)
|
148
|
+
end
|
149
|
+
end
|
134
150
|
end
|
135
151
|
|
136
152
|
def include?(record)
|
@@ -3,9 +3,14 @@ module DuckRecord
|
|
3
3
|
module Associations
|
4
4
|
class HasOneAssociation < SingularAssociation #:nodoc:
|
5
5
|
def replace(record)
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
self.target =
|
7
|
+
if record.is_a? klass
|
8
|
+
record
|
9
|
+
elsif record.respond_to?(:to_h)
|
10
|
+
build_record(record.to_h)
|
11
|
+
end
|
12
|
+
rescue
|
13
|
+
raise_on_type_mismatch!(record)
|
9
14
|
end
|
10
15
|
end
|
11
16
|
end
|
data/lib/duck_record/errors.rb
CHANGED
@@ -22,6 +22,21 @@ module DuckRecord
|
|
22
22
|
class ConfigurationError < DuckRecordError
|
23
23
|
end
|
24
24
|
|
25
|
+
# Raised when an object assigned to an association has an incorrect type.
|
26
|
+
#
|
27
|
+
# class Ticket < ActiveRecord::Base
|
28
|
+
# has_many :patches
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# class Patch < ActiveRecord::Base
|
32
|
+
# belongs_to :ticket
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# # Comments are not patches, this assignment raises AssociationTypeMismatch.
|
36
|
+
# @ticket.patches << Comment.new(content: "Please attach tests to your patch.")
|
37
|
+
class AssociationTypeMismatch < DuckRecordError
|
38
|
+
end
|
39
|
+
|
25
40
|
# Raised when unknown attributes are supplied via mass assignment.
|
26
41
|
UnknownAttributeError = ActiveModel::UnknownAttributeError
|
27
42
|
|
@@ -149,14 +149,14 @@ module DuckRecord
|
|
149
149
|
# <tt>has_many :clients</tt> returns <tt>{}</tt>
|
150
150
|
attr_reader :options
|
151
151
|
|
152
|
-
attr_reader :
|
152
|
+
attr_reader :duck_record
|
153
153
|
|
154
154
|
attr_reader :plural_name # :nodoc:
|
155
155
|
|
156
|
-
def initialize(name, options,
|
156
|
+
def initialize(name, options, duck_record)
|
157
157
|
@name = name
|
158
158
|
@options = options
|
159
|
-
@
|
159
|
+
@duck_record = duck_record
|
160
160
|
@klass = options[:anonymous_class]
|
161
161
|
@plural_name = name.to_s.pluralize
|
162
162
|
end
|
@@ -200,12 +200,12 @@ module DuckRecord
|
|
200
200
|
end
|
201
201
|
|
202
202
|
def compute_class(name)
|
203
|
-
|
203
|
+
duck_record.send(:compute_type, name)
|
204
204
|
end
|
205
205
|
|
206
206
|
attr_accessor :parent_reflection # Reflection
|
207
207
|
|
208
|
-
def initialize(name, options,
|
208
|
+
def initialize(name, options, duck_record)
|
209
209
|
super
|
210
210
|
@constructable = calculate_constructable(macro, options)
|
211
211
|
|
data/lib/duck_record/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: duck_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jasl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -156,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
156
|
version: '0'
|
157
157
|
requirements: []
|
158
158
|
rubyforge_project:
|
159
|
-
rubygems_version: 2.6.
|
159
|
+
rubygems_version: 2.6.12
|
160
160
|
signing_key:
|
161
161
|
specification_version: 4
|
162
162
|
summary: Used for creating virtual models like ActiveType or ModelAttribute does
|