Module: Relaxo::Model::Document
- Includes:
- Comparable
- Defined in:
- lib/relaxo/model/document.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Equality is done only on id to improve performance.
- #==(other) ⇒ Object
-
#after_create ⇒ Object
Set any default values:.
- #after_delete ⇒ Object
- #after_fetch ⇒ Object
- #after_save ⇒ Object
- #before_delete ⇒ Object
-
#before_save ⇒ Object
Update any calculations:.
- #changed?(key) ⇒ Boolean
- #delete(dataset) ⇒ Object
-
#dup(dataset = @dataset) ⇒ Object
Make a copy of the record, as if calling create.
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #inspect ⇒ Object
- #new_record? ⇒ Boolean
- #paths ⇒ Object
- #persisted? ⇒ Boolean
-
#save(dataset) ⇒ Object
Save the model object.
- #save!(dataset) ⇒ Object
-
#to_s ⇒ Object
The canonical path to the object in the data store, assuming there is some unique way to identify the object.
- #type ⇒ Object
- #valid_type? ⇒ Boolean
Class Method Details
.included(child) ⇒ Object
50 51 52 53 |
# File 'lib/relaxo/model/document.rb', line 50 def self.included(child) child.send(:include, Component) child.send(:extend, ClassMethods) end |
Instance Method Details
#<=>(other) ⇒ Object
Equality is done only on id to improve performance.
227 228 229 |
# File 'lib/relaxo/model/document.rb', line 227 def <=> other self.id <=> other.id if other end |
#==(other) ⇒ Object
235 236 237 |
# File 'lib/relaxo/model/document.rb', line 235 def == other self.attributes == other.attributes if other end |
#after_create ⇒ Object
Set any default values:
223 224 |
# File 'lib/relaxo/model/document.rb', line 223 def after_create end |
#after_delete ⇒ Object
203 204 |
# File 'lib/relaxo/model/document.rb', line 203 def after_delete end |
#after_fetch ⇒ Object
218 219 220 |
# File 'lib/relaxo/model/document.rb', line 218 def after_fetch raise TypeError.new(self) unless valid_type? end |
#after_save ⇒ Object
119 120 |
# File 'lib/relaxo/model/document.rb', line 119 def after_save end |
#before_delete ⇒ Object
200 201 |
# File 'lib/relaxo/model/document.rb', line 200 def before_delete end |
#before_save ⇒ Object
Update any calculations:
116 117 |
# File 'lib/relaxo/model/document.rb', line 116 def before_save end |
#changed?(key) ⇒ Boolean
103 104 105 |
# File 'lib/relaxo/model/document.rb', line 103 def changed? key @changed.include? key.to_s end |
#delete(dataset) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/relaxo/model/document.rb', line 206 def delete(dataset) before_delete @changed.clear paths.each do |path| dataset.delete(path) end after_delete end |
#dup(dataset = @dataset) ⇒ Object
Make a copy of the record, as if calling create.
136 137 138 139 140 141 142 143 |
# File 'lib/relaxo/model/document.rb', line 136 def dup(dataset = @dataset) # Splat already calls dup internally I guess. clone = self.class.new(dataset, nil, @changed.dup, **@attributes) clone.after_create return clone end |
#empty? ⇒ Boolean
243 244 245 |
# File 'lib/relaxo/model/document.rb', line 243 def empty? @attributes.empty? end |
#eql?(other) ⇒ Boolean
231 232 233 |
# File 'lib/relaxo/model/document.rb', line 231 def eql? other self.id.eql?(other.id) if other end |
#hash ⇒ Object
239 240 241 |
# File 'lib/relaxo/model/document.rb', line 239 def hash self.id.hash end |
#inspect ⇒ Object
131 132 133 |
# File 'lib/relaxo/model/document.rb', line 131 def inspect "\#<#{self.class}:#{self.id} #{self.attributes.inspect}>" end |
#new_record? ⇒ Boolean
95 96 97 |
# File 'lib/relaxo/model/document.rb', line 95 def new_record? !persisted? end |
#paths ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/relaxo/model/document.rb', line 145 def paths return to_enum(:paths) unless block_given? self.class.keys.each do |name, key| # @attributes is not modified until we call self.dump (which flattens @attributes into @changes). When we generate paths, we want to ensure these are done based on the non-mutable state of the object. yield key.object_path(self, @attributes) end end |
#persisted? ⇒ Boolean
99 100 101 |
# File 'lib/relaxo/model/document.rb', line 99 def persisted? @object != nil end |
#save(dataset) ⇒ Object
Save the model object.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/relaxo/model/document.rb', line 155 def save(dataset) return if persisted? and @changed.empty? before_save if errors = self.validate return errors end existing_paths = persisted? ? paths.to_a : [] # Write data, check if any actual changes made: object = dataset.append(self.dump) return if object == @object existing_paths.each do |path| dataset.delete(path) end paths do |path| if dataset.exist?(path) raise KeyError, "Dataset already contains path: #{path}, when inserting #{@attributes.inspect}" end dataset.write(path, object) end @dataset = dataset @object = object after_save return true end |
#save!(dataset) ⇒ Object
190 191 192 193 194 195 196 197 198 |
# File 'lib/relaxo/model/document.rb', line 190 def save!(dataset) result = self.save(dataset) if result != true raise ValidationErrors.new(result) end return self end |
#to_s ⇒ Object
The canonical path to the object in the data store, assuming there is some unique way to identify the object.
123 124 125 126 127 128 129 |
# File 'lib/relaxo/model/document.rb', line 123 def to_s if primary_key = self.class.primary_key primary_key.object_path(self) else super end end |
#type ⇒ Object
107 108 109 |
# File 'lib/relaxo/model/document.rb', line 107 def type @attributes[:type] end |
#valid_type? ⇒ Boolean
111 112 113 |
# File 'lib/relaxo/model/document.rb', line 111 def valid_type? self.type == self.class.type end |