Module: HuggORM::Persistence::InstanceMethods
- Defined in:
- lib/hugg_orm/persistence.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Instance Method Summary collapse
- #<=>(o) ⇒ Object
- #created? ⇒ Boolean
- #destroy ⇒ Object
- #destroyed? ⇒ Boolean
- #dump ⇒ Object
- #fetch(*args) ⇒ Object
- #has_field?(field) ⇒ Boolean
- #initialize(data = {}) ⇒ Object
- #key ⇒ Object
- #save ⇒ Object
- #set_id ⇒ Object
- #update_attributes(update_data) ⇒ Object
- #updated? ⇒ Boolean
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
129 130 131 |
# File 'lib/hugg_orm/persistence.rb', line 129 def data @data end |
#uuid ⇒ Object
Returns the value of attribute uuid.
129 130 131 |
# File 'lib/hugg_orm/persistence.rb', line 129 def uuid @uuid end |
Instance Method Details
#<=>(o) ⇒ Object
216 217 218 |
# File 'lib/hugg_orm/persistence.rb', line 216 def <=>(o) o.kind_of?(self.class) && @data == o.data ? 0 : nil end |
#created? ⇒ Boolean
196 197 198 |
# File 'lib/hugg_orm/persistence.rb', line 196 def created? @state == :created end |
#destroy ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/hugg_orm/persistence.rb', line 165 def destroy if self.class.destroy(key) @data = {} @state = :destroyed end self end |
#destroyed? ⇒ Boolean
188 189 190 |
# File 'lib/hugg_orm/persistence.rb', line 188 def destroyed? @state == :destroyed end |
#dump ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/hugg_orm/persistence.rb', line 200 def dump str = "#{self.class.name}:\n" @data.sort.each do |field, value| str << " #{field}: #{value}\n" end str end |
#fetch(*args) ⇒ Object
208 209 210 |
# File 'lib/hugg_orm/persistence.rb', line 208 def fetch(*args) @data.send(:fetch, *args) end |
#has_field?(field) ⇒ Boolean
212 213 214 |
# File 'lib/hugg_orm/persistence.rb', line 212 def has_field?(field) @data.has_key?(field) end |
#initialize(data = {}) ⇒ Object
131 132 133 134 |
# File 'lib/hugg_orm/persistence.rb', line 131 def initialize(data = {}) @data = {} update_attributes(data) unless data.empty? end |
#key ⇒ Object
136 137 138 |
# File 'lib/hugg_orm/persistence.rb', line 136 def key @data[self.class.key_field] end |
#save ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/hugg_orm/persistence.rb', line 174 def save updated_data = self.class.where("#{self.class.primary_key} = ?", [key]).update(@data, true) unless updated_data.empty? @data = updated_data.first @state = :updated else self.class.new_query.insert(@data) @state = :created end self end |
#set_id ⇒ Object
140 141 142 143 144 |
# File 'lib/hugg_orm/persistence.rb', line 140 def set_id # Set id based on key field, but only if id isn't already set and # the key method has been overridden and returns data. @data[self.class.key_field] = key if @data[self.class.key_field].nil? && key end |
#update_attributes(update_data) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/hugg_orm/persistence.rb', line 146 def update_attributes(update_data) # Remove fields not set to be included unless self.class.include_fields.empty? update_data = self.class.include_fields.each_with_object({}) do |key, hash| hash[key] = update_data[key] if update_data.has_key? key end end # Update all fields unless they have been excluded update_data.each do |field, value| @data[field] = value unless self.class.exclude_fields.include?(field) end # Set id based on key method. set_id self end |
#updated? ⇒ Boolean
192 193 194 |
# File 'lib/hugg_orm/persistence.rb', line 192 def updated? @state == :updated end |