Skip to content

Commit

Permalink
Add in support in the bindings for fetching properties of history items.
Browse files Browse the repository at this point in the history
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
scotchi committed Oct 10, 2012
1 parent aceed00 commit 83f2b2e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
8 changes: 5 additions & 3 deletions Ruby/lib/directededge/historyentry.rb
Expand Up @@ -25,10 +25,12 @@ module DirectedEdge
class HistoryEntry class HistoryEntry
attr_accessor :target, :history, :timestamp 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 @target = target.to_s
@history = options[:history] @timestamp = options['timestamp'] ? options.delete('timestamp').to_i : Time.now.to_i
@timestamp = options[:timestamp] ? options[:timestamp].to_i : Time.now.to_i @properties = options
end end
end end
end end
5 changes: 3 additions & 2 deletions Ruby/lib/directededge/item.rb
Expand Up @@ -25,9 +25,10 @@ module DirectedEdge
class Item class Item
attr_reader :id attr_reader :id


def initialize(database, id) def initialize(database, id, options = {})
@database = database @database = database
@id = id.to_s @id = id.to_s
@options = options
@data = { @data = {
:links => LinkProxy.new(Array) { load }, :links => LinkProxy.new(Array) { load },
:tags => ContainerProxy.new(Array) { load }, :tags => ContainerProxy.new(Array) { load },
Expand All @@ -41,7 +42,7 @@ def initialize(database, id)


def load def load
begin begin
data = XML.parse(resource.get) data = XML.parse(resource[@options].get)
@exists = true @exists = true
rescue RestClient::ResourceNotFound rescue RestClient::ResourceNotFound
@exists = false @exists = false
Expand Down
5 changes: 4 additions & 1 deletion Ruby/lib/directededge/xml.rb
Expand Up @@ -32,7 +32,10 @@ def self.parse(text)
:preselected => Reader.list(node, '//preselected'), :preselected => Reader.list(node, '//preselected'),
:blacklisted => Reader.list(node, '//blacklisted'), :blacklisted => Reader.list(node, '//blacklisted'),
:properties => Hash[node.find('//property').map { |p| [ p['name'], p.first.to_s ] }], :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 end


Expand Down
2 changes: 1 addition & 1 deletion Ruby/test/test_directededge.rb
Expand Up @@ -476,7 +476,7 @@ def test_history_entries


assert(customer.history_entries.empty?) 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 customer.save


assert(customer.history_entries.size == 1) assert(customer.history_entries.size == 1)
Expand Down

0 comments on commit 83f2b2e

Please sign in to comment.