parse-stack 1.0.4 → 1.0.5

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: 7b2f443ea3100a01208cc85a71fdc0b6ec2724c0
4
- data.tar.gz: 253a1845c48fe6a9ffbdd728660efbab38bea5a1
3
+ metadata.gz: 93fb8b8b1962db48f30eb4e1ae2f7172b8377ed6
4
+ data.tar.gz: 8dbf85b25286a790c48d88dbc52228bbf8a66838
5
5
  SHA512:
6
- metadata.gz: e083d6466f4dc40f2d207c192966027f6a0adf8e928183f129c6be81d569c6ed51a9eb4b5ff4e909ba46465d74fa818bd24025a52ef756c1b14e0235986d40d5
7
- data.tar.gz: cfc551f79913f5fe87f29e642fcf22bdd51652b2b15db71650cd3727b0e62103e767e29e128b246dea5b5b4febd9eb28cff4e3ff639ec7e2441ae25928b936c9
6
+ metadata.gz: 3768e70a0e368697101e6a6d12e178cae15593950441876999b4e01377aa14ebb721be405d910651ffa399b5e6d760fa7f7a47b268665b760e5b47f4704f1b59
7
+ data.tar.gz: 56cb920091be91a880acd7667732f7d90f88fcd6dc174a7bd0bd5048b336a5538713f1117e0a5149beec612c13802669b1b28452bf4a9279a666d34f2f1dbe5e
data/Changes.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Parse-Stack Changes
2
2
 
3
+ 1.0.5
4
+ -----------
5
+ - Defaults are applied on object instantiation.
6
+ - When applying default values, dirty tracking is called.
7
+
3
8
  1.0.4
4
9
  -----------
5
10
  - Fixes minor issue when storing and retrieving objects from the cache.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- parse-stack (1.0.4)
4
+ parse-stack (1.0.5)
5
5
  active_model_serializers (>= 0.9, < 1)
6
6
  activemodel (>= 4, < 5)
7
7
  activesupport (>= 4, < 5)
@@ -61,6 +61,10 @@ module Parse
61
61
  @attributes ||= BASE.dup
62
62
  end
63
63
 
64
+ def defaults_list
65
+ @defaults_list ||= []
66
+ end
67
+
64
68
  # property :songs, :array
65
69
  # property :my_date, :date, field: "myRemoteCOLUMNName"
66
70
  # property :my_int, :integer, required: true, default: ->{ rand(10) }
@@ -126,9 +130,8 @@ module Parse
126
130
  validates_presence_of key
127
131
  end
128
132
 
129
- # get the default value if provided (or Proc)
130
- default_value = opts[:default]
131
133
  symbolize_value = opts[:symbolize]
134
+
132
135
  #only support symbolization of string data types
133
136
  if symbolize_value && data_type != :string
134
137
  raise 'Symbolization is only supported on :string data types.'
@@ -137,6 +140,18 @@ module Parse
137
140
  # Here is the where the 'magic' begins. For each property defined, we will
138
141
  # generate special setters and getters that will take advantage of ActiveModel
139
142
  # helpers.
143
+ # get the default value if provided (or Proc)
144
+ default_value = opts[:default]
145
+ if default_value.present?
146
+ defaults_list.push(key) if default_value.present?
147
+
148
+ define_method("#{key}_default") do
149
+ # If the default object provided is a Proc, then run the proc, otherwise
150
+ # we'll assume it's just a plain literal value
151
+ default_value.is_a?(Proc) ? default_value.call(self) : default_value
152
+ end
153
+
154
+ end
140
155
 
141
156
  # We define a getter with the key
142
157
  define_method(key) do
@@ -158,12 +173,12 @@ module Parse
158
173
 
159
174
  # if value is nil (even after fetching), then lets see if the developer
160
175
  # set a default value for this attribute.
161
- if value.nil? && default_value.present?
162
- # If the default object provided is a Proc, then run the proc, otherwise
163
- # we'll assume it's just a plain literal value
164
- value = default_value.is_a?(Proc) ? default_value.call : default_value
176
+ if value.nil? && respond_to?("#{key}_default")
177
+
178
+ value = send("#{key}_default")
165
179
  # lets set the variable with the updated value
166
180
  instance_variable_set ivar, value
181
+ send "#{key}_will_change!"
167
182
  end
168
183
 
169
184
  # if the value is a String (like an iso8601 date) and the data type of
@@ -171,6 +186,7 @@ module Parse
171
186
  if value.is_a?(String) && data_type == :date
172
187
  value = Parse::Date.parse value
173
188
  instance_variable_set ivar, value
189
+ send "#{key}_will_change!"
174
190
  end
175
191
  # finally return the value
176
192
  symbolize_value && value.respond_to?(:to_sym) ? value.to_sym : value
@@ -142,9 +142,16 @@ module Parse
142
142
  self.acl = self.class.acl({}, owner: self) || Parse::ACL.new(owner: self)
143
143
  end
144
144
  clear_changes! if @id.present? #then it was an import
145
+ apply_defaults!
145
146
  # do not call super since it is Pointer subclass
146
147
  end
147
148
 
149
+ def apply_defaults!
150
+ self.class.defaults_list.each do |key|
151
+ send(key) # should call set default proc/values if nil
152
+ end
153
+ end
154
+
148
155
  def self.pointer(id)
149
156
  return nil if id.nil?
150
157
  Parse::Pointer.new self.parse_class, id
@@ -1,5 +1,5 @@
1
1
  module Parse
2
2
  module Stack
3
- VERSION = "1.0.4"
3
+ VERSION = "1.0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parse-stack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Persaud
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-02-12 00:00:00.000000000 Z
12
+ date: 2016-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel