Module: AssayDepot::Model

Included in:
Provider, Ware
Defined in:
lib/assaydepot/model.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/assaydepot/model.rb', line 15

def self.included(base)
  base.extend ClassMethods
  base.extend Forwardable
  base.def_delegators  :private_results, 
                  :each, 
                  :[], 
                  :count, 
                  :collect, 
                  :map,
                  :tap, 
                  :<=>, 
                  :compact, 
                  :each_index, 
                  :each_with_index, 
                  :empty?, 
                  :flatten, 
                  :include?, 
                  :index, 
                  :length, 
                  :first, 
                  :last, 
                  :keep_if,
                  :reject,
                  :reverse

  attr_accessor :search_query
  attr_accessor :search_facets
  attr_accessor :search_options

  def initialize(options={})
    @search_query = options[:search_query] || ""
    @search_facets = options[:search_facets] || {}
    @search_options = options[:search_options] || {:page => 1}
  end

  def initialize_copy(source)  
    super
    @search_query = @search_query.dup
    @search_facets = @search_facets.dup
  end

  def query_time
    search_results["query_time"]
  end
  def total
    search_results["total"]
  end
  def page
    search_results["page"]
  end
  def per_page
    search_results["per_page"]
  end
  def facets
    search_results["facets"]
  end

  def page(page_num)
    options( { "page" => page_num } )
  end

  def per_page(page_size)
    options( { "per_page" => page_size } )
  end

  def find(query)
    result = self.clone
    result.search_query = search_query
    result
  end

  def where(conditions={})
    result = self.clone
    result.search_facets = self.search_facets ? self.search_facets.merge(conditions) : conditions
    result
  end

  def options(options={})
    result = self.clone
    result.search_options = self.search_options ? self.search_options.merge(options) : options
    result
  end

  def private_results
    search_results[self.class.ref_name]
  end
  def search_results
    unless @search_results
      @search_results = Client.new(:search_type => self.class.search_type).search(search_query, search_facets, search_options)
    end
    @search_results
  end
end

Instance Method Details

#facetsObject



68
69
70
# File 'lib/assaydepot/model.rb', line 68

def facets
  search_results["facets"]
end

#find(query) ⇒ Object



80
81
82
83
84
# File 'lib/assaydepot/model.rb', line 80

def find(query)
  result = self.clone
  result.search_query = search_query
  result
end

#initialize(options = {}) ⇒ Object



44
45
46
47
48
# File 'lib/assaydepot/model.rb', line 44

def initialize(options={})
  @search_query = options[:search_query] || ""
  @search_facets = options[:search_facets] || {}
  @search_options = options[:search_options] || {:page => 1}
end

#initialize_copy(source) ⇒ Object



50
51
52
53
54
# File 'lib/assaydepot/model.rb', line 50

def initialize_copy(source)  
  super
  @search_query = @search_query.dup
  @search_facets = @search_facets.dup
end

#options(options = {}) ⇒ Object



92
93
94
95
96
# File 'lib/assaydepot/model.rb', line 92

def options(options={})
  result = self.clone
  result.search_options = self.search_options ? self.search_options.merge(options) : options
  result
end

#page(page_num) ⇒ Object



62
63
64
# File 'lib/assaydepot/model.rb', line 62

def page
  search_results["page"]
end

#per_page(page_size) ⇒ Object



65
66
67
# File 'lib/assaydepot/model.rb', line 65

def per_page
  search_results["per_page"]
end

#private_resultsObject



98
99
100
# File 'lib/assaydepot/model.rb', line 98

def private_results
  search_results[self.class.ref_name]
end

#query_timeObject



56
57
58
# File 'lib/assaydepot/model.rb', line 56

def query_time
  search_results["query_time"]
end

#search_resultsObject



101
102
103
104
105
106
# File 'lib/assaydepot/model.rb', line 101

def search_results
  unless @search_results
    @search_results = Client.new(:search_type => self.class.search_type).search(search_query, search_facets, search_options)
  end
  @search_results
end

#totalObject



59
60
61
# File 'lib/assaydepot/model.rb', line 59

def total
  search_results["total"]
end

#where(conditions = {}) ⇒ Object



86
87
88
89
90
# File 'lib/assaydepot/model.rb', line 86

def where(conditions={})
  result = self.clone
  result.search_facets = self.search_facets ? self.search_facets.merge(conditions) : conditions
  result
end