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.
112 113 114 |
# File 'lib/hugg_orm/persistence.rb', line 112 def data @data end |
#uuid ⇒ Object
Returns the value of attribute uuid.
112 113 114 |
# File 'lib/hugg_orm/persistence.rb', line 112 def uuid @uuid end |
Instance Method Details
#<=>(o) ⇒ Object
199 200 201 |
# File 'lib/hugg_orm/persistence.rb', line 199 def <=>(o) o.kind_of?(self.class) && @data == o.data ? 0 : nil end |
#created? ⇒ Boolean
179 180 181 |
# File 'lib/hugg_orm/persistence.rb', line 179 def created? @state == :created end |
#destroy ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/hugg_orm/persistence.rb', line 148 def destroy if self.class.destroy(key) @data = {} @state = :destroyed end self end |
#destroyed? ⇒ Boolean
171 172 173 |
# File 'lib/hugg_orm/persistence.rb', line 171 def destroyed? @state == :destroyed end |
#dump ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/hugg_orm/persistence.rb', line 183 def dump str = "#{self.class.name}:\n" @data.sort.each do |field, value| str << " #{field}: #{value}\n" end str end |
#fetch(*args) ⇒ Object
191 192 193 |
# File 'lib/hugg_orm/persistence.rb', line 191 def fetch(*args) @data.send(:fetch, *args) end |
#has_field?(field) ⇒ Boolean
195 196 197 |
# File 'lib/hugg_orm/persistence.rb', line 195 def has_field?(field) @data.has_key?(field) end |
#initialize(data = {}) ⇒ Object
114 115 116 117 |
# File 'lib/hugg_orm/persistence.rb', line 114 def initialize(data = {}) @data = {} update_attributes(data) unless data.empty? end |
#key ⇒ Object
119 120 121 |
# File 'lib/hugg_orm/persistence.rb', line 119 def key @data[self.class.key_field] end |
#save ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/hugg_orm/persistence.rb', line 157 def save updated_data = self.class.where("#{self.class.primary_key} = ?", [key]).update(@data, true) if updated_data != 0 @data = updated_data @state = :updated else self.class.new_query.insert(@data) @state = :created end self end |
#set_id ⇒ Object
123 124 125 126 127 |
# File 'lib/hugg_orm/persistence.rb', line 123 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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/hugg_orm/persistence.rb', line 129 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
175 176 177 |
# File 'lib/hugg_orm/persistence.rb', line 175 def updated? @state == :updated end |