Class: Exegesis::Document
- Inherits:
-
CouchRest::Document
- Object
- CouchRest::Document
- Exegesis::Document
- Defined in:
- lib/exegesis/document.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Class Method Summary collapse
- .default(hash = nil) ⇒ Object
- .expose(*attrs) ⇒ Object
- .inherited(subklass) ⇒ Object
- .instantiate(hash = {}) ⇒ Object
- .timestamps! ⇒ Object
- .unique_id(meth) ⇒ Object
Instance Method Summary collapse
- #document_save ⇒ Object
-
#initialize(keys = {}) ⇒ Document
constructor
A new instance of Document.
- #save ⇒ Object
- #update_attributes(attrs = {}) ⇒ Object
Constructor Details
#initialize(keys = {}) ⇒ Document
Returns a new instance of Document.
96 97 98 99 100 |
# File 'lib/exegesis/document.rb', line 96 def initialize keys={} apply_default super keys self['.kind'] ||= self.class.to_s end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
77 78 79 |
# File 'lib/exegesis/document.rb', line 77 def parent @parent end |
Class Method Details
.default(hash = nil) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/exegesis/document.rb', line 51 def self.default hash=nil if hash @default = hash else @default ||= superclass.respond_to?(:default) ? superclass.default : {} end end |
.expose(*attrs) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/exegesis/document.rb', line 12 def self.expose *attrs opts = if attrs.last.is_a?(Hash) attrs.pop else {} end [attrs].flatten.each do |attrib| attrib = "#{attrib}" if opts.has_key?(:writer) if opts[:writer] define_method("#{attrib}=") {|val| self[attrib] = opts[:writer].call(val) } end else define_method("#{attrib}=") {|val| self[attrib] = val } end if opts[:as] if opts[:as] == :reference define_method(attrib) do |*reload| reload = false if reload.empty? instance_variable_set("@#{attrib}", nil) if reload return instance_variable_get("@#{attrib}") if instance_variable_get("@#{attrib}") instance_variable_set("@#{attrib}", load_reference(self[attrib])) end else define_method(attrib) do self[attrib] = if self[attrib].is_a?(Array) self[attrib].map {|val| cast opts[:as], val }.compact else cast opts[:as], self[attrib] end end end else define_method(attrib) { self[attrib] } end end end |
.inherited(subklass) ⇒ Object
4 5 6 |
# File 'lib/exegesis/document.rb', line 4 def self.inherited subklass Exegesis.document_classes[subklass.name] = subklass end |
.instantiate(hash = {}) ⇒ Object
8 9 10 |
# File 'lib/exegesis/document.rb', line 8 def self.instantiate hash={} Exegesis.document_classes[hash['.kind']].new(hash) end |
.timestamps! ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/exegesis/document.rb', line 59 def self. define_method :set_timestamps do self['updated_at'] = Time.now self['created_at'] ||= Time.now end expose 'updated_at', :as => Time, :writer => false expose 'created_at', :as => Time, :writer => false end |
.unique_id(meth) ⇒ Object
68 69 70 71 72 |
# File 'lib/exegesis/document.rb', line 68 def self.unique_id meth define_method :set_unique_id do self['_id'] = self.send(meth) end end |
Instance Method Details
#document_save ⇒ Object
75 |
# File 'lib/exegesis/document.rb', line 75 alias_method :document_save, :save |
#save ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/exegesis/document.rb', line 79 def save raise ChildError, "cannot save if a parent is set" if parent if respond_to?(:set_timestamps) if respond_to?(:set_unique_id) && id.nil? @unique_id_attempt = 0 begin self['_id'] = set_unique_id document_save rescue RestClient::RequestFailed => e @unique_id_attempt += 1 retry end else document_save end end |
#update_attributes(attrs = {}) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/exegesis/document.rb', line 102 def update_attributes attrs={} raise ArgumentError, 'must include a matching _rev attribute' unless rev == attrs.delete('_rev') attrs.each_pair do |key, value| self.send("#{key}=", value) rescue nil attrs.delete(key) end save end |