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
- #connection ⇒ Object
- #create(attributes = {}) ⇒ Object
- #find(query, options = {}) ⇒ Object
- #find_by_day(day, options = {}) ⇒ Object
- #find_by_id(id) ⇒ Object
- #get(id) ⇒ Object
- #timestamped_attribute ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
120 121 122 |
# File 'lib/mordor/resource.rb', line 120 def all( = {}) Collection.new(self, perform_collection_find({}, )) end |
#attribute(name, options = {}) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/mordor/resource.rb', line 178 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 ensure_index(name) 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
124 125 126 |
# File 'lib/mordor/resource.rb', line 124 def collection connection.collection(self.collection_name) end |
#collection_name ⇒ Object
128 129 130 131 |
# File 'lib/mordor/resource.rb', line 128 def collection_name klassname = self.to_s.downcase.gsub(/[\/|.|::]/, '_') "#{klassname}s" end |
#connection ⇒ Object
144 145 146 |
# File 'lib/mordor/resource.rb', line 144 def connection @connection ||= Mordor.connection end |
#create(attributes = {}) ⇒ Object
114 115 116 117 118 |
# File 'lib/mordor/resource.rb', line 114 def create(attributes = {}) resource = self.new(attributes) resource.save resource end |
#find(query, options = {}) ⇒ Object
153 154 155 |
# File 'lib/mordor/resource.rb', line 153 def find(query, = {}) Collection.new(self, perform_collection_find(query, )) end |
#find_by_day(day, options = {}) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mordor/resource.rb', line 157 def find_by_day(day, = {}) case day when DateTime start = day.to_date.to_time end_of_day = (day.to_date + 1).to_time when Date start = day.to_time end_of_day = (day + 1).to_time when Time start = day.to_datetime.to_date.to_time end_of_day = (day.to_date + 1).to_datetime.to_date.to_time end hash = {:at => {'$gte' => start, '$lt' => end_of_day}} cursor = perform_collection_find({:at => {'$gte' => start, '$lt' => end_of_day}}, ) Collection.new(self, cursor) end |
#find_by_id(id) ⇒ Object
149 150 151 |
# File 'lib/mordor/resource.rb', line 149 def find_by_id(id) get(id) end |
#get(id) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/mordor/resource.rb', line 133 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
174 175 176 |
# File 'lib/mordor/resource.rb', line 174 def @timestamped_attribute end |