Class: CouchRestAdapter::Base

Inherits:
CouchRest::Document
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations, ActiveSupport::Callbacks, AttributeMethod, DbConfig, DocumentManagement, QueryViews
Defined in:
lib/couch_rest_adapter.rb

Constant Summary

Constants included from DocumentManagement

DocumentManagement::UUID_DOC

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QueryViews

included

Methods included from DocumentManagement

#_set_id_and_namespace, included, #query, #set_id, #uuids

Methods included from DbConfig

included

Methods included from AttributeMethod

#attribute_methods, #base_id, #method_without_equals, #read_value, #read_write, #write_value, #writer?

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



30
31
32
33
# File 'lib/couch_rest_adapter.rb', line 30

def initialize attributes = {}
  raise NotImplementedError if abstract?
  super attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/couch_rest_adapter.rb', line 70

def method_missing method, *args, &block
  if attribute_methods.include? method.to_s
    read_write method, args.first
  elsif method.to_s =~ /^(.+)=$/
    read_write method, args.first
  else
    super
  end
end

Class Method Details

.allObject



35
36
37
# File 'lib/couch_rest_adapter.rb', line 35

def self.all
  query_view('all', default_design_doc)
end

.all_by_typeObject



39
40
41
# File 'lib/couch_rest_adapter.rb', line 39

def self.all_by_type
  query_view('by_type', default_design_doc)
end

.find(doc_id) ⇒ Object



43
44
45
# File 'lib/couch_rest_adapter.rb', line 43

def self.find doc_id
  new database.get( doc_id.namespace_me(object_name) ).to_hash
end

.method_missing(method, *args, &block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/couch_rest_adapter.rb', line 55

def self.method_missing method, *args, &block
  if method.to_s =~ /^find_by_(.+)$/
    find_by_attribute($1, args.first, default_design_doc)
  else
    super
  end
end

.respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/couch_rest_adapter.rb', line 51

def self.respond_to? method
  (method.to_s =~ /^find_by_.*$/) ? true : super
end

.use_default_databaseObject



47
48
49
# File 'lib/couch_rest_adapter.rb', line 47

def self.use_default_database
  use_database CouchRest.database(full_path)
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/couch_rest_adapter.rb', line 80

def persisted?
  true
end

#read_attribute_for_validation(key) ⇒ Object



84
85
86
# File 'lib/couch_rest_adapter.rb', line 84

def read_attribute_for_validation(key)
  self[key.to_s]
end

#saveObject



63
64
65
66
67
68
# File 'lib/couch_rest_adapter.rb', line 63

def save
  return false if invalid?
  return false unless run_callbacks(:save)
  _set_id_and_namespace
  super
end