version-one 0.0.4 → 0.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.
@@ -160,6 +160,69 @@ module VersionOne
160
160
  raise
161
161
  end
162
162
 
163
+ def reply(msg)
164
+ self.class.reply_to(self, msg)
165
+ end
166
+
167
+ def self.reply_to(asset, msg)
168
+ asset_id = case asset
169
+ when Asset
170
+ asset.id
171
+ when String
172
+ asset
173
+ else
174
+ raise ArgumentError, 'Invalid asset argument'
175
+ end
176
+
177
+ raise ArgumentError, 'Cannot reply to this asset type' unless asset_id.match(/^Expression\:\d+$/)
178
+
179
+ reply = VersionOne::Expression.new(
180
+ content: msg,
181
+ in_reply_to: AssetRef.new(id: asset_id)
182
+ )
183
+
184
+ reply.save!
185
+ end
186
+
187
+ def comments(options={})
188
+ deep = options.key?(:deep) ? options[:deep] : true
189
+
190
+ query = VersionOne::Query.new('Expression').select(*%w{Author.Name Author.Email AuthoredAt Mentions InReplyTo Replies Content})
191
+ where = "Mentions='#{self.id}'"
192
+ if deep
193
+ where = "(#{where})|(InReplyTo.Mentions='#{self.id}')"
194
+ end
195
+
196
+ results = query.where(where).all
197
+
198
+ # Move reply information to the corresponding
199
+ # parent conversation
200
+ if deep
201
+ comments_by_id = results.index_by(&:id)
202
+
203
+ replies = results.select{|c| c.in_reply_to && comments_by_id.key?(c.in_reply_to.id)}
204
+ replies_by_id = replies.index_by(&:id)
205
+
206
+ results.map do |c|
207
+ if replies_by_id.key?(c.id)
208
+ nil
209
+ elsif c.replies.nil?
210
+ c
211
+ else
212
+ c.replies.each do |ref|
213
+ reply = replies_by_id[ref.id]
214
+ ref.set(reply) if reply
215
+ end
216
+ c
217
+ end
218
+ end
219
+
220
+ results.compact!
221
+ end
222
+
223
+ results
224
+ end
225
+
163
226
  def exec(op_name, options={})
164
227
  client = options[:client] || VersionOne::Client.new
165
228
  client.post(href + "?op=#{op_name}")
@@ -10,11 +10,14 @@ module VersionOne
10
10
  @asset = xml_or_asset
11
11
  @id = @asset.id
12
12
  @href = @asset.href
13
+ when Hash
14
+ @id = xml_or_asset[:id]
15
+ @href = xml_or_asset[:href]
13
16
  else
14
17
  @href = xml_or_asset.attributes['href']
15
18
  @id = xml_or_asset.attributes['idref']
16
19
  end
17
- raise ArgumentError, "Could not get id and href" unless @id && @href
20
+ raise ArgumentError, "Could not get id and href" unless @id || @href
18
21
  end
19
22
 
20
23
  def self.for(x)
@@ -25,14 +28,25 @@ module VersionOne
25
28
  @asset ||= get_asset(*fields)
26
29
  end
27
30
 
31
+ def set(asset)
32
+ case
33
+ when !asset.is_a?(VersionOne::Asset)
34
+ raise ArgumentError, "Parameter must be a VersionOne asset"
35
+ when asset.id == @id
36
+ @asset = asset
37
+ else
38
+ raise ArgumentError, "Asset does not match reference"
39
+ end
40
+ end
41
+
28
42
  def inspect
29
43
  "#<AssetRef:#{@href}>"
30
44
  end
31
45
 
32
46
  def to_xml
33
47
  xml = XML::Node.new('Asset')
34
- xml.attributes['href'] = @href
35
- xml.attributes['idref'] = @id
48
+ xml.attributes['href'] = @href if @href
49
+ xml.attributes['idref'] = @id if @id
36
50
  xml
37
51
  end
38
52
 
@@ -41,7 +41,7 @@ module VersionOne
41
41
  end
42
42
 
43
43
  def cache_store
44
- #Rails.cache
44
+ VersionOne.cache
45
45
  end
46
46
 
47
47
  def can_cache?
@@ -23,6 +23,14 @@ module VersionOne
23
23
  @@config ||= Configuration.new
24
24
  end
25
25
 
26
+ def self.cache
27
+ @@cache ||= defined?(Rails) ? Rails.cache : nil
28
+ end
29
+
30
+ def self.cache=(c)
31
+ @@cache = c
32
+ end
33
+
26
34
  def self.logger=(l)
27
35
  @@logger = l
28
36
  end
@@ -1,3 +1,3 @@
1
1
  module VersionOne
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  module VersionOne
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: version-one
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-29 00:00:00.000000000 Z
12
+ date: 2014-10-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: libxml-ruby