Class: DataMapper::CouchResource::View

Inherits:
Object
  • Object
show all
Defined in:
lib/couchdb_adapter/view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name) ⇒ View

Returns a new instance of View.



12
13
14
15
16
17
# File 'lib/couchdb_adapter/view.rb', line 12

def initialize(model, name)
  @model = model
  @name = name

  create_getter
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/couchdb_adapter/view.rb', line 10

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/couchdb_adapter/view.rb', line 10

def name
  @name
end

Instance Method Details

#create_getterObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/couchdb_adapter/view.rb', line 19

def create_getter
  @model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def self.#{@name}(*args)
      options = {}
      if args.size == 1 && !args.first.is_a?(Hash)
        options[:key] = args.shift
      else
        options = args.pop
      end
      query = Query.new(repository, self)
      query.view_options = options || {}
      query.view = '#{@name}'
      if options.is_a?(Hash) && options.has_key?(:repository)
        repository(options.delete(:repository)).read_many(query)
      else
        repository.read_many(query)
      end
    end
  RUBY
end