active_remote 2.3.3 → 2.3.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b604884d11d706b849ed8963accf9e2d28c155eb
4
- data.tar.gz: 8ef1ff903de40d4191863bdd4c90642236deed91
3
+ metadata.gz: cb82251608b1f6b943d874e28dc49b8c1499120e
4
+ data.tar.gz: c615b870b8541d5a8ef55ad81f943bc9fb35ffee
5
5
  SHA512:
6
- metadata.gz: aa0b3bee35bd874f8e7614ebbc1a79acb0ebb64665c164afd862340f254f01a08fafca5f6272c7aa7aec2de7f836ffd0aa6191a454289e0799b023d85a213794
7
- data.tar.gz: 0cbace01e4a87ae9bc098acd4427b43353b4de2680223f39258b29c1f658f5f831742404899863cbd95127de7ba99e79a396b8353d81b0b32ae0a3d1bc2174ad
6
+ metadata.gz: 912fe0d39adefa07687a0cd900e337fccb986d26c3a5c295d93e7324c0ed5d96c61db1f76711c1e6a513a05ae78ab9f7c02de8ecf6b834bccd35a5ba2b4c1e3a
7
+ data.tar.gz: f4b58ae9a8ea1d2d0201cdd6089d512ce19e9e3e444a105873f8bb8134b1042a24c1527a1d54cca0cd5c2ae564b904c86bcd367fe75ecdc3f5ad048ab9bd9965
@@ -0,0 +1,100 @@
1
+ require "active_support/concern"
2
+ require "active_support/core_ext/object/duplicable"
3
+
4
+ module ActiveRemote
5
+ # AttributeDefaults allows defaults to be declared for your attributes
6
+ #
7
+ # Defaults are declared by passing the :default option to the attribute
8
+ # class method. If you need the default to be dynamic, pass a lambda, Proc,
9
+ # or any object that responds to #call as the value to the :default option
10
+ # and the result will calculated on initialization. These dynamic defaults
11
+ # can depend on the values of other attributes when those attributes are
12
+ # assigned using MassAssignment or BlockInitialization.
13
+ #
14
+ # @example Usage
15
+ # class Person
16
+ # include ActiveRemote::AttributeDefaults
17
+ #
18
+ # attribute :first_name, :default => "John"
19
+ # attribute :last_name, :default => "Doe"
20
+ # end
21
+ #
22
+ # person = Person.new
23
+ # person.first_name #=> "John"
24
+ # person.last_name #=> "Doe"
25
+ #
26
+ # @example Dynamic Default
27
+ # class Event
28
+ # include ActiveAttr::MassAssignment
29
+ # include ActiveRemote::AttributeDefaults
30
+ #
31
+ # attribute :start_date
32
+ # attribute :end_date, :default => lambda { start_date }
33
+ # end
34
+ #
35
+ # event = Event.new(:start_date => Date.parse("2012-01-01"))
36
+ # event.end_date.to_s #=> "2012-01-01"
37
+ #
38
+ module AttributeDefaults
39
+ extend ActiveSupport::Concern
40
+
41
+ # Applies the attribute defaults
42
+ #
43
+ # Applies all the default values to any attributes not yet set, avoiding
44
+ # any attribute setter logic, such as dirty tracking.
45
+ #
46
+ # @example Usage
47
+ # class Person
48
+ # include ActiveRemote::AttributeDefaults
49
+ #
50
+ # attribute :first_name, :default => "John"
51
+ # end
52
+ #
53
+ # person = Person.new
54
+ # person.first_name #=> "John"
55
+ #
56
+ def apply_defaults(defaults=attribute_defaults)
57
+ @attributes ||= {}
58
+ defaults.each do |name, value|
59
+ # instance variable is used here to avoid any dirty tracking in attribute setter methods
60
+ @attributes[name] = value if @attributes[name].nil?
61
+ end
62
+ end
63
+
64
+ # Calculates the attribute defaults from the attribute definitions
65
+ #
66
+ # @example Usage
67
+ # class Person
68
+ # include ActiveAttr::AttributeDefaults
69
+ #
70
+ # attribute :first_name, :default => "John"
71
+ # end
72
+ #
73
+ # Person.new.attribute_defaults #=> {"first_name"=>"John"}
74
+ #
75
+ def attribute_defaults
76
+ attributes_map { |name| _attribute_default name }
77
+ end
78
+
79
+ # Applies attribute default values
80
+ #
81
+ def initialize(*)
82
+ super
83
+ apply_defaults
84
+ end
85
+
86
+ private
87
+
88
+ # Calculates an attribute default
89
+ #
90
+ def _attribute_default(attribute_name)
91
+ default = self.class.attributes[attribute_name][:default]
92
+
93
+ case
94
+ when default.respond_to?(:call) then instance_exec(&default)
95
+ when default.duplicable? then default.dup
96
+ else default
97
+ end
98
+ end
99
+ end
100
+ end
@@ -2,6 +2,7 @@ require 'active_model/callbacks'
2
2
  require 'active_attr/model'
3
3
 
4
4
  require 'active_remote/association'
5
+ require 'active_remote/attribute_defaults'
5
6
  require 'active_remote/attributes'
6
7
  require 'active_remote/bulk'
7
8
  require 'active_remote/config'
@@ -23,7 +24,9 @@ module ActiveRemote
23
24
  extend ActiveModel::Callbacks
24
25
 
25
26
  include ActiveAttr::BasicModel
27
+ include ActiveAttr::Attributes
26
28
  include ActiveAttr::BlockInitialization
29
+ include ActiveAttr::ChainableInitialization
27
30
  include ActiveAttr::Logger
28
31
  include ActiveAttr::MassAssignment
29
32
  include ActiveAttr::AttributeDefaults
@@ -31,6 +34,7 @@ module ActiveRemote
31
34
  include ActiveAttr::Serialization
32
35
 
33
36
  include Association
37
+ include AttributeDefaults
34
38
  include Attributes
35
39
  include Bulk
36
40
  include DSL
@@ -1,3 +1,3 @@
1
1
  module ActiveRemote
2
- VERSION = "2.3.3"
2
+ VERSION = "2.3.4"
3
3
  end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ describe "attribute defailts" do
4
+ let(:test_class) { ::DefaultAuthor }
5
+
6
+ describe "string" do
7
+ it "defaults to a string" do
8
+ record = test_class.new
9
+ expect(record.name).to eq("John Doe")
10
+ end
11
+ end
12
+
13
+ describe "lambda" do
14
+ it "calls the lambda" do
15
+ record = test_class.new
16
+ expect(record.guid).to eq(100)
17
+ end
18
+ end
19
+
20
+ describe "array" do
21
+ it "defaults to an array" do
22
+ record = test_class.new
23
+ expect(record.books).to eq([])
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,6 @@
1
1
  require 'support/models/message_with_options'
2
2
  require 'support/models/author'
3
+ require 'support/models/default_author'
3
4
  require 'support/models/category'
4
5
  require 'support/models/post'
5
6
  require 'support/models/tag'
@@ -0,0 +1,12 @@
1
+ require 'support/protobuf/author.pb'
2
+
3
+ ##
4
+ # Define a generic class that inherits from active remote base
5
+ #
6
+ class DefaultAuthor < ::ActiveRemote::Base
7
+ service_class ::Generic::Remote::AuthorService
8
+
9
+ attribute :guid, :default => lambda { 100 }
10
+ attribute :name, :default => "John Doe"
11
+ attribute :books, :default => []
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_remote
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.3
4
+ version: 2.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hutchison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-20 00:00:00.000000000 Z
11
+ date: 2016-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_attr
@@ -168,6 +168,7 @@ files:
168
168
  - active_remote.gemspec
169
169
  - lib/active_remote.rb
170
170
  - lib/active_remote/association.rb
171
+ - lib/active_remote/attribute_defaults.rb
171
172
  - lib/active_remote/attributes.rb
172
173
  - lib/active_remote/base.rb
173
174
  - lib/active_remote/bulk.rb
@@ -205,6 +206,7 @@ files:
205
206
  - lib/active_remote/version.rb
206
207
  - spec/core_ext/date_time_spec.rb
207
208
  - spec/lib/active_remote/association_spec.rb
209
+ - spec/lib/active_remote/attribute_defaults_spec.rb
208
210
  - spec/lib/active_remote/base_spec.rb
209
211
  - spec/lib/active_remote/bulk_spec.rb
210
212
  - spec/lib/active_remote/dirty_spec.rb
@@ -232,6 +234,7 @@ files:
232
234
  - spec/support/models.rb
233
235
  - spec/support/models/author.rb
234
236
  - spec/support/models/category.rb
237
+ - spec/support/models/default_author.rb
235
238
  - spec/support/models/message_with_options.rb
236
239
  - spec/support/models/post.rb
237
240
  - spec/support/models/tag.rb
@@ -269,6 +272,7 @@ summary: Active Record for your platform
269
272
  test_files:
270
273
  - spec/core_ext/date_time_spec.rb
271
274
  - spec/lib/active_remote/association_spec.rb
275
+ - spec/lib/active_remote/attribute_defaults_spec.rb
272
276
  - spec/lib/active_remote/base_spec.rb
273
277
  - spec/lib/active_remote/bulk_spec.rb
274
278
  - spec/lib/active_remote/dirty_spec.rb
@@ -296,6 +300,7 @@ test_files:
296
300
  - spec/support/models.rb
297
301
  - spec/support/models/author.rb
298
302
  - spec/support/models/category.rb
303
+ - spec/support/models/default_author.rb
299
304
  - spec/support/models/message_with_options.rb
300
305
  - spec/support/models/post.rb
301
306
  - spec/support/models/tag.rb