Permalink
Browse files

Add in support in the bindings for fetching properties of history items.

This slightly changes the API for creating HistoryEntry objects from:

HistoryEntry.new('target', :history => history)

to:

HistoryEntry.new(history, 'target')

Also, now, to trigger loading of history properties, the item must be passed
the :include_properties option, e.g.:

Item.new(db, 'item1', :include_properties => true)
  • Loading branch information...
1 parent 5b56a69 commit 43e059d0ebd3e7d7076ae8e92d89172c42ff364a @scotchi scotchi committed Aug 17, 2012
View
8 Ruby/lib/directededge/historyentry.rb
@@ -25,10 +25,12 @@ module DirectedEdge
class HistoryEntry
attr_accessor :target, :history, :timestamp
- def initialize(target, options)
+ def initialize(history, target, options = {})
+ raise ArgumentError.new unless history.is_a?(History) && options.is_a?(Hash)
+ @history = history
@target = target.to_s
- @history = options[:history]
- @timestamp = options[:timestamp] ? options[:timestamp].to_i : Time.now.to_i
+ @timestamp = options['timestamp'] ? options.delete('timestamp').to_i : Time.now.to_i
+ @properties = options
end
end
end
View
5 Ruby/lib/directededge/item.rb
@@ -25,9 +25,10 @@ module DirectedEdge
class Item
attr_reader :id
- def initialize(database, id)
+ def initialize(database, id, options = {})
@database = database
@id = id.to_s
+ @options = options
@data = {
:links => LinkProxy.new(Array) { load },
:tags => ContainerProxy.new(Array) { load },
@@ -41,7 +42,7 @@ def initialize(database, id)
def load
begin
- data = XML.parse(resource.get)
+ data = XML.parse(resource[@options].get)
@exists = true
rescue RestClient::ResourceNotFound
@exists = false
View
5 Ruby/lib/directededge/xml.rb
@@ -32,7 +32,10 @@ def self.parse(text)
:preselected => Reader.list(node, '//preselected'),
:blacklisted => Reader.list(node, '//blacklisted'),
:properties => Hash[node.find('//property').map { |p| [ p['name'], p.first.to_s ] }],
- :history_entries => node.find('//history').map { |h| HistoryEntry.new(h.first.to_s, h) }
+ :history_entries => node.find('//history').map do |node|
+ history = History.new(:from => node[:from], :to => node[:to])
+ HistoryEntry.new(history, node.first.to_s, node.attributes.to_h)
+ end
}
end
View
2 Ruby/test/test_directededge.rb
@@ -476,7 +476,7 @@ def test_history_entries
assert(customer.history_entries.empty?)
- customer.history_entries.add(DirectedEdge::HistoryEntry.new(product, :history => history))
+ customer.history_entries.add(DirectedEdge::HistoryEntry.new(history, product))
customer.save
assert(customer.history_entries.size == 1)

0 comments on commit 43e059d

Please sign in to comment.