Module: Mordor::Resource::ClassMethods
- Defined in:
- lib/mordor/resource.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #attribute(name, options = {}) ⇒ Object
- #collection ⇒ Object
- #collection_name ⇒ Object
- #create(attributes = {}) ⇒ Object
- #database ⇒ Object
- #find(query, options = {}) ⇒ Object
- #find_by_day(value, options = {}) ⇒ Object
- #find_by_id(id) ⇒ Object
- #get(id) ⇒ Object
- #timestamped_attribute ⇒ Object
- #with_collection ⇒ Object
- #with_database(max_retries = 60) ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
145 146 147 |
# File 'lib/mordor/resource.rb', line 145 def all( = {}) Collection.new(self, perform_collection_find({}, )) end |
#attribute(name, options = {}) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/mordor/resource.rb', line 229 def attribute(name, = {}) @attributes ||= [] @indices ||= [] @index_types ||= {} @attributes << name unless @attributes.include?(name) if [:index] @indices << name unless @indices.include?(name) @index_types[name] = [:index_type] ? [:index_type] : Mongo::DESCENDING end if [:timestamp] raise ArgumentError.new("Only one timestamped attribute is allowed, '#{@timestamped_attribute}' is already timestamped") unless @timestamped_attribute.nil? @timestamped_attribute = name end method_name = .key?(:finder_method) ? [:finder_method] : "find_by_#{name}" class_eval <<-EOS, __FILE__, __LINE__ attr_accessor name def self.#{method_name}(value, options = {}) if value.is_a?(Hash) raise ArgumentError.new(":value missing from complex query hash") unless value.keys.include?(:value) query = {:#{name} => value.delete(:value)} query = query.merge(value) else query = {:#{name} => value} end col = perform_collection_find(query, options) Collection.new(self, col) end EOS end |
#collection ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/mordor/resource.rb', line 149 def collection collection = nil with_database do |database| collection = database.collection(self.collection_name) end collection end |
#collection_name ⇒ Object
157 158 159 160 |
# File 'lib/mordor/resource.rb', line 157 def collection_name klassname = self.to_s.downcase.gsub(/[\/|.|::]/, '_') "#{klassname}s" end |
#create(attributes = {}) ⇒ Object
139 140 141 142 143 |
# File 'lib/mordor/resource.rb', line 139 def create(attributes = {}) resource = self.new(attributes) resource.save resource end |
#database ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/mordor/resource.rb', line 173 def database unless @db if connecting_to_replica_set? client = new_replica_set_client else client = new_mongo_connection end @db = database_connection(client) end @db end |
#find(query, options = {}) ⇒ Object
208 209 210 |
# File 'lib/mordor/resource.rb', line 208 def find(query, = {}) Collection.new(self, perform_collection_find(query, )) end |
#find_by_day(value, options = {}) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/mordor/resource.rb', line 212 def find_by_day(value, = {}) if value.is_a?(Hash) raise ArgumentError.new(":value missing from complex query hash") unless value.keys.include?(:value) day = value.delete(:value) query = value.merge(day_to_query(day)) else query = day_to_query(value) end cursor = perform_collection_find(query, ) Collection.new(self, cursor) end |
#find_by_id(id) ⇒ Object
204 205 206 |
# File 'lib/mordor/resource.rb', line 204 def find_by_id(id) get(id) end |
#get(id) ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mordor/resource.rb', line 162 def get(id) if id.is_a?(String) id = BSON::ObjectId.from_string(id) end if attributes = perform_collection_find_one(:_id => id) new(attributes) else nil end end |
#timestamped_attribute ⇒ Object
225 226 227 |
# File 'lib/mordor/resource.rb', line 225 def @timestamped_attribute end |
#with_collection ⇒ Object
198 199 200 201 202 |
# File 'lib/mordor/resource.rb', line 198 def with_collection with_database do |database| yield database.collection(self.collection_name) end end |
#with_database(max_retries = 60) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mordor/resource.rb', line 186 def with_database(max_retries=60) retries = 0 begin yield self.database rescue Mongo::ConnectionFailure => ex retries += 1 raise ex if retries > max_retries sleep(0.5) retry end end |