Module: Mongoo::Attributes::InstanceMethods

Defined in:
lib/mongoo/attributes.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#attributesObject


104
105
106
# File 'lib/mongoo/attributes.rb', line 104

def attributes
  mongohash.to_key_value
end

#get_attribute(k) ⇒ Object Also known as: get, g


46
47
48
49
50
51
# File 'lib/mongoo/attributes.rb', line 46

def get_attribute(k)
  unless known_attribute?(k)
    raise Mongoo::UnknownAttributeError, k
  end
  mongohash.dot_get(k.to_s)
end

#get_attributes(keys) ⇒ Object Also known as: gets


92
93
94
95
96
# File 'lib/mongoo/attributes.rb', line 92

def get_attributes(keys)
  found = {}
  keys.each { |k| found[k.to_s] = get_attribute(k) }
  found
end

#get_or_set_attribute(k, v) ⇒ Object Also known as: get_or_set, gs


73
74
75
# File 'lib/mongoo/attributes.rb', line 73

def get_or_set_attribute(k, v)
  get_attribute(k) || set_attribute(k, v)
end

#idObject


108
109
110
# File 'lib/mongoo/attributes.rb', line 108

def id
  get "_id"
end

#id=(val) ⇒ Object


112
113
114
# File 'lib/mongoo/attributes.rb', line 112

def id=(val)
  set "_id", val
end

#known_attribute?(k) ⇒ Boolean

Returns:

  • (Boolean)

38
39
40
# File 'lib/mongoo/attributes.rb', line 38

def known_attribute?(k)
  self.class.known_attribute?(k)
end

#read_attribute_for_validation(key) ⇒ Object


42
43
44
# File 'lib/mongoo/attributes.rb', line 42

def read_attribute_for_validation(key)
  get_attribute(key)
end

#set_attribute(k, v) ⇒ Object Also known as: set, s


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mongoo/attributes.rb', line 55

def set_attribute(k,v)
  unless known_attribute?(k)
    if self.respond_to?("#{k}=")
      self.send("#{k}=", v)
      return v
    else
      raise Mongoo::UnknownAttributeError, k
    end
  end
  unless k.to_s == "_id" || v.nil?
    field_type = self.class.attributes[k.to_s][:type]
    v = Mongoo::AttributeSanitizer.sanitize(field_type, v)
  end
  mongohash.dot_set(k.to_s,v); v
end

#set_attributes(k_v_pairs) ⇒ Object Also known as: sets


85
86
87
88
89
# File 'lib/mongoo/attributes.rb', line 85

def set_attributes(k_v_pairs)
  k_v_pairs.each do |k,v|
    set_attribute(k,v)
  end
end

#unset_attribute(k) ⇒ Object Also known as: unset, u


79
80
81
# File 'lib/mongoo/attributes.rb', line 79

def unset_attribute(k)
  mongohash.dot_delete(k); true
end

#unset_attributes(keys) ⇒ Object Also known as: unsets


99
100
101
# File 'lib/mongoo/attributes.rb', line 99

def unset_attributes(keys)
  keys.each { |k| unset_attribute(k) }; true
end