Class: Rubyfocus::Item

Inherits:
Object
  • Object
show all
Includes:
ConditionalExec, IDRef
Defined in:
lib/rubyfocus/items/item.rb

Overview

The Rubyfocus Item represents an item found in an Omnifocus XML file, and thus also any Omnifocus entity.

The Rubyfocus Item has a parent “document” as well as a series of properties determined by the XML file proper. You can pass an XML document at creation or leave it blank. You can always apply XML later using Item#apply_xml or Item#<<

By separating these two methods, we can also patch the object with another XML node, e.g. through an update. This is important!

Direct Known Subclasses

NamedItem, Setting

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConditionalExec

#conditional_set

Methods included from IDRef

included

Constructor Details

#initialize(document = nil, n = nil) ⇒ Item

Returns a new instance of Item.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubyfocus/items/item.rb', line 15

def initialize(document=nil, n=nil)
  self.document = document

  case n
  when Nokogiri::XML::Element
    apply_xml(n)
  when Hash
    n.each do |k,v|
      setter = "#{k}="
      send(setter,v) if respond_to?(setter)
    end
  end
end

Instance Attribute Details

#addedObject

Returns the value of attribute added.



13
14
15
# File 'lib/rubyfocus/items/item.rb', line 13

def added
  @added
end

#documentObject

Returns the value of attribute document.



13
14
15
# File 'lib/rubyfocus/items/item.rb', line 13

def document
  @document
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/rubyfocus/items/item.rb', line 13

def id
  @id
end

#modifiedObject

Returns the value of attribute modified.



13
14
15
# File 'lib/rubyfocus/items/item.rb', line 13

def modified
  @modified
end

Instance Method Details

#apply_xml(n) ⇒ Object Also known as: <<



29
30
31
32
33
# File 'lib/rubyfocus/items/item.rb', line 29

def apply_xml(n)
  self.id ||= n["id"] # This should not change once set!
  conditional_set(:added,    n.at_xpath("xmlns:added"))   { |e| Time.parse(e) }
  conditional_set(:modified,   n.at_xpath("xmlns:modified"))  { |e| Time.parse(e) }
end

#inspectObject


Inspection method



39
40
41
42
# File 'lib/rubyfocus/items/item.rb', line 39

def inspect
  msgs = inspect_properties.select{ |sym| self.respond_to?(sym) && !self.send(sym).nil? }
  "#<#{self.class} " + msgs.map{ |e| %|#{e}=#{self.send(e).inspect}| }.join(" ") + ">"
end

#to_serialObject



44
45
46
# File 'lib/rubyfocus/items/item.rb', line 44

def to_serial
  inspect_properties.each_with_object({}){ |s,hsh| hsh[s] = self.send(s) }
end