Module: Mordor::Resource

Defined in:
lib/mordor/resource.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_idObject

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

#collectionObject



110
111
112
# File 'lib/mordor/resource.rb', line 110

def collection
  self.class.collection
end

#destroyObject



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

Returns:

  • (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

Returns:

  • (Boolean)


49
50
51
# File 'lib/mordor/resource.rb', line 49

def new?
  return self._id == nil
end

#reloadObject



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

#saveObject 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 timestamp_attribute = self.class.timestamped_attribute
      timestamp_value = self_hash.delete(timestamp_attribute)
      if timestamp_value.is_a?(Hash)
        timestamp_value = BSON::Timestamp.new(timestamp_value["seconds"], timestamp_value["increment"])
      end
      ordered_self_hash = BSON::OrderedHash.new
      if timestamp_value.nil? || (timestamp_value.is_a?(String) && timestamp_value.empty?)
        ordered_self_hash[timestamp_attribute] = BSON::Timestamp.new(0, 0)
      else
        ordered_self_hash[timestamp_attribute] = timestamp_value
      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

Returns:

  • (Boolean)


53
54
55
# File 'lib/mordor/resource.rb', line 53

def saved?
  return !new?
end

#to_hashObject



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

#updateObject



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_collectionObject



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