Class: Things::Reference::Record

Inherits:
Base
  • Object
show all
Extended by:
Inheritance
Defined in:
lib/things/reference/record.rb

Direct Known Subclasses

Area, Person, Project, Tag, Todo

Instance Attribute Summary

Attributes inherited from Base

#reference

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inheritance

inheritable_attributes, inherited

Constructor Details

#initialize(props = {}) ⇒ Record

Returns a new instance of Record.



43
44
45
46
47
# File 'lib/things/reference/record.rb', line 43

def initialize(props = {})
  props.each_pair do |property, value|
    self.send("#{property}=", value)
  end
end

Class Method Details

.build(reference) ⇒ Object

build a new instance and link it to the supplied reference

Returns a object associated with a reference



105
106
107
108
109
# File 'lib/things/reference/record.rb', line 105

def self.build(reference)
  todo = self.new
  todo.reference = reference
  todo
end

.collection(name = nil) ⇒ Object



15
16
17
# File 'lib/things/reference/record.rb', line 15

def self.collection(name = nil)
  name ? @collection = name : @collection
end

.convert(references) ⇒ Object

Converts a collection of reference into a collection of objects



112
113
114
# File 'lib/things/reference/record.rb', line 112

def self.convert(references)
  references.to_a.collect { |todo| build(todo) }
end

.create(props) ⇒ Object

create a new object based on that supplied properties and saves it



98
99
100
# File 'lib/things/reference/record.rb', line 98

def self.create(props)
  new(props).save
end

.find(name_or_id) ⇒ Object

find a todo by a name or id

Returns a Things::Todo object associated with a reference



119
120
121
# File 'lib/things/reference/record.rb', line 119

def self.find(name_or_id)
  find_by_name(name_or_id) || find_by_id(name_or_id)
end

.find_by_id(id) ⇒ Object

find a todo by a id Returns a Things::Todo object associated with a reference



133
134
135
136
137
138
# File 'lib/things/reference/record.rb', line 133

def self.find_by_id(id)
  finder = Appscript.its.id_.eq(id)
  reference = Things::App.instance.send(self.collection)[finder].get rescue nil
  reference = reference.first if reference.is_a?(Array)
  build(reference) if reference
end

.find_by_name(name) ⇒ Object

find a todo by a name

Returns a Things::Todo object associated with a reference



126
127
128
129
# File 'lib/things/reference/record.rb', line 126

def self.find_by_name(name)
  reference = Things::App.instance.send(self.collection)[name].get rescue nil
  build(reference) if reference
end

.identifier(name = nil) ⇒ Object



11
12
13
# File 'lib/things/reference/record.rb', line 11

def self.identifier(name = nil)
  name ? @identifier = name : @identifier
end

.properties(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/things/reference/record.rb', line 19

def self.properties(*args)
  @_properties = [] if !@_properties
  if args
    @_properties += args
    @_properties.each do |property|
      attr_writer(property) if !instance_methods.include?(property.to_s+'=')
      if !instance_methods.include?(property.to_s )
        class_eval <<-"eval"
          def #{property}
            return @#{property} if @#{property}
            fetched = @reference.#{property}.get rescue nil
            if fetched.is_a?(Appscript::Reference)
              fetched = fetched.identify.build(fetched)
            end
            @#{property} = fetched if fetched and fetched != :missing_value
          end
        eval
      end
    end
  end
end

Instance Method Details

#deleteObject

Delete a object

This places the object in the trash



93
94
95
# File 'lib/things/reference/record.rb', line 93

def delete
  Things::App.instance.delete(self.reference) rescue false
end

#new?Boolean

Returns whether the instance if new or has already been saved

Returns:

  • (Boolean)


50
51
52
# File 'lib/things/reference/record.rb', line 50

def new?
  id_.nil?  
end

#saveObject

Save a Todo

If a todo is new, it will be created. If not, it will be updated



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/things/reference/record.rb', line 57

def save
  if new?
    properties = {}
    (self.class.properties - [:id_]).each do |property|
      if value = self.send(property)
        properties[property] = value.respond_to?(:reference) ? value.reference : value
      end
    end
    # only set name in make, then set the rest of the properties
    name = properties.delete(:name)
    self.reference = Things::App.instance.make(:new => self.class.identifier, :with_properties => { :name => name })
    properties.each_pair { |name, property| self.reference.send(name).set(property) }
  else
    # update
    properties = self.class.properties - [:id_]
    # If :area is present, push it to end the because if other  
    # properties are removed, Things removes the Area altogether
    properties.push(properties.delete(:area)) if properties.include?(:area)
    properties.each do |property|
      if value = self.send(property)
        self.reference.send(property).set(value.respond_to?(:reference) ? value.reference : value)
      else
        begin
          # Check if the original value was not empty and remove it if wasn't
          self.reference.send(property).delete if self.reference.send(property).get != :missing_value
        rescue
        end
      end
    end
  end
  self
end