Module: CouchRest::CastedModel

Defined in:
lib/couchrest/casted_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
# File 'lib/couchrest/casted_model.rb', line 4

def self.included(base)
  base.send(:include, ::CouchRest::Mixins::Callbacks)
  base.send(:include, ::CouchRest::Mixins::Properties)
  base.send(:attr_accessor, :casted_by)
end

Instance Method Details

#[](key) ⇒ Object



23
24
25
# File 'lib/couchrest/casted_model.rb', line 23

def [] key
  super(key.to_s)
end

#[]=(key, value) ⇒ Object



19
20
21
# File 'lib/couchrest/casted_model.rb', line 19

def []= key, value
  super(key.to_s, value)
end

#base_docObject

Gets a reference to the top level extended document that a model is saved inside of



29
30
31
32
# File 'lib/couchrest/casted_model.rb', line 29

def base_doc
  return nil unless @casted_by
  @casted_by.base_doc
end

#initialize(keys = {}) ⇒ Object

Raises:

  • (StandardError)


10
11
12
13
14
15
16
17
# File 'lib/couchrest/casted_model.rb', line 10

def initialize(keys={})
  raise StandardError unless self.is_a? Hash
  apply_all_property_defaults # defined in CouchRest::Mixins::Properties
  super()
  keys.each do |k,v|
    write_attribute(k.to_s, v)
  end if keys
end

#new?Boolean Also known as: new_record?

False if the casted model has already been saved in the containing document

Returns:

  • (Boolean)


36
37
38
# File 'lib/couchrest/casted_model.rb', line 36

def new?
  @casted_by.nil? ? true : @casted_by.new?
end

#update_attributes_without_saving(hash) ⇒ Object Also known as: attributes=

Sets the attributes from a hash



42
43
44
45
46
47
48
49
# File 'lib/couchrest/casted_model.rb', line 42

def update_attributes_without_saving(hash)
  hash.each do |k, v|
    raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=")
  end      
  hash.each do |k, v|
    self.send("#{k}=",v)
  end
end