Module: Mordor::Resource
- Defined in:
- lib/mordor/resource.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#_id ⇒ Object
Returns the value of attribute _id.
Class Method Summary collapse
Instance Method Summary collapse
- #collection ⇒ Object
- #destroy ⇒ Object
- #destroyed? ⇒ Boolean
- #initialize(attributes = {}) ⇒ Object
- #new? ⇒ Boolean
- #reload ⇒ Object
- #replace_params(params = {}) ⇒ Object
- #replace_type(value) ⇒ Object
- #save ⇒ Object (also: #save!)
- #saved? ⇒ Boolean
- #to_hash ⇒ Object
- #to_json(*args) ⇒ Object
- #update ⇒ Object
- #with_collection ⇒ Object
Instance Attribute Details
#_id ⇒ Object
Returns the value of attribute _id.
3 4 5 |
# File 'lib/mordor/resource.rb', line 3 def _id @_id end |
Class Method Details
.included(base) ⇒ Object
5 6 7 |
# File 'lib/mordor/resource.rb', line 5 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#collection ⇒ Object
110 111 112 |
# File 'lib/mordor/resource.rb', line 110 def collection self.class.collection end |
#destroy ⇒ Object
132 133 134 135 136 |
# File 'lib/mordor/resource.rb', line 132 def destroy collection.remove({:_id => _id}) self.class.send(:ensure_indices) @destroyed = true end |
#destroyed? ⇒ Boolean
128 129 130 |
# File 'lib/mordor/resource.rb', line 128 def destroyed? @destroyed ||= false end |
#initialize(attributes = {}) ⇒ Object
9 10 11 12 13 |
# File 'lib/mordor/resource.rb', line 9 def initialize(attributes = {}) attributes.each do |k,v| self.send("#{k}=", v) end end |
#new? ⇒ Boolean
49 50 51 |
# File 'lib/mordor/resource.rb', line 49 def new? return self._id == nil end |
#reload ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/mordor/resource.rb', line 57 def reload return unless _id res = self.class.get(_id).to_hash.each do |k, v| self.send("#{k}=".to_sym, v) end self end |
#replace_params(params = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mordor/resource.rb', line 15 def replace_params(params = {}) result = {} return result if params.nil? or params.empty? params.each do |key, value| value = replace_type(value) key = key.to_s.gsub(/\W|\./, "_") result[key] = value end result end |
#replace_type(value) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mordor/resource.rb', line 26 def replace_type(value) case value when Hash value = replace_params(value) when Date, DateTime value = value.to_time.getlocal when Time value = value.getlocal when BigDecimal value = value.to_f when Array value = value.map do |val| replace_type(val) end when BSON::Timestamp value = replace_params({:seconds => value ? value.seconds : 0, :increment => value ? value.increment : 0}) when Integer else value = value.to_s end value end |
#save ⇒ Object Also known as: save!
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mordor/resource.rb', line 65 def save unless self._id self_hash = self.to_hash if = self.class. = self_hash.delete() if .is_a?(Hash) = BSON::Timestamp.new(["seconds"], ["increment"]) end ordered_self_hash = BSON::OrderedHash.new if .nil? || (.is_a?(String) && .empty?) ordered_self_hash[] = BSON::Timestamp.new(0, 0) else ordered_self_hash[] = end self_hash.each do |key, value| ordered_self_hash[key] = value end self_hash = ordered_self_hash end with_collection do |collection| insert_id = collection.insert(self_hash) self._id = insert_id end else insert_id = self.update end saved? end |
#saved? ⇒ Boolean
53 54 55 |
# File 'lib/mordor/resource.rb', line 53 def saved? return !new? end |
#to_hash ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/mordor/resource.rb', line 114 def to_hash attributes = self.class.instance_variable_get(:@attributes) result = {} return result unless attributes attributes.each do |attribute_name| result[attribute_name] = replace_type(self.send(attribute_name)) end result end |
#to_json(*args) ⇒ Object
124 125 126 |
# File 'lib/mordor/resource.rb', line 124 def to_json(*args) to_hash.merge(:_id => _id).to_json(*args) end |
#update ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/mordor/resource.rb', line 102 def update insert_id = nil with_collection do |collection| insert_id = collection.update({:_id => self._id}, self.to_hash) end insert_id end |
#with_collection ⇒ Object
96 97 98 99 100 |
# File 'lib/mordor/resource.rb', line 96 def with_collection self.class.with_collection do |collection| yield collection end end |