Class: Things::Reference::Record
- Extended by:
- Inheritance
- Defined in:
- lib/things/reference/record.rb
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.build(reference) ⇒ Object
build a new instance and link it to the supplied reference.
- .collection(name = nil) ⇒ Object
-
.convert(references) ⇒ Object
Converts a collection of reference into a collection of objects.
-
.create(props) ⇒ Object
create a new object based on that supplied properties and saves it.
-
.find(name_or_id) ⇒ Object
find a todo by a name or id.
-
.find_by_id(id) ⇒ Object
find a todo by a id Returns a Things::Todo object associated with a reference.
-
.find_by_name(name) ⇒ Object
find a todo by a name.
- .identifier(name = nil) ⇒ Object
- .properties(*args) ⇒ Object
Instance Method Summary collapse
-
#delete ⇒ Object
Delete a object.
-
#initialize(props = {}) ⇒ Record
constructor
A new instance of Record.
-
#new? ⇒ Boolean
Returns whether the instance if new or has already been saved.
-
#save ⇒ Object
Save a Todo.
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
104 105 106 107 108 |
# File 'lib/things/reference/record.rb', line 104 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
111 112 113 |
# File 'lib/things/reference/record.rb', line 111 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
97 98 99 |
# File 'lib/things/reference/record.rb', line 97 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
118 119 120 |
# File 'lib/things/reference/record.rb', line 118 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
132 133 134 135 136 137 |
# File 'lib/things/reference/record.rb', line 132 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
125 126 127 128 |
# File 'lib/things/reference/record.rb', line 125 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
#delete ⇒ Object
Delete a object
This places the object in the trash
92 93 94 |
# File 'lib/things/reference/record.rb', line 92 def delete Things::App.instance.delete(self.reference) rescue false end |
#new? ⇒ Boolean
Returns whether the instance if new or has already been saved
50 51 52 |
# File 'lib/things/reference/record.rb', line 50 def new? id_.nil? end |
#save ⇒ Object
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 |
# 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 self.reference.send(property).delete rescue end end end end self end |