picky-client 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,14 @@ module Picky
8
8
  def empty?
9
9
  allocations.empty?
10
10
  end
11
+ # The rendered results or AR instances if you
12
+ # have populated the results.
13
+ #
14
+ def entries limit = 20
15
+ entries = []
16
+ allocations.each { |allocation| allocation[5].each { |id| break if entries.size > limit; entries << id } }
17
+ entries
18
+ end
11
19
  # Returns the topmost limit results.
12
20
  #
13
21
  def ids limit = 20
@@ -32,6 +40,50 @@ module Picky
32
40
  def total
33
41
  @total || @total = self[:total]
34
42
  end
35
-
43
+
44
+ # Populating the results.
45
+ #
46
+ # Give it an AR class and options for the find and it
47
+ # will yield each found result for you to render.
48
+ #
49
+ # If you don't pass it a block, it will just use the AR results.
50
+ #
51
+ def populate_with klass, amount = 20, options = {}, &block
52
+ the_ids = ids amount
53
+
54
+ objects = klass.find the_ids, options
55
+
56
+ # Put together a mapping.
57
+ #
58
+ mapped_entries = objects.inject({}) do |mapped, entry|
59
+ mapped[entry.id] = entry if entry
60
+ mapped
61
+ end
62
+
63
+ # Preserves the order
64
+ #
65
+ objects = the_ids.map { |id| mapped_entries[id] }
66
+
67
+ objects.collect! &block if block_given?
68
+
69
+ replace_ids_with objects
70
+ clear_ids
71
+
72
+ objects
73
+ end
74
+
75
+ # The ids need to come in the order which the ids were returned by the ids method.
76
+ #
77
+ def replace_ids_with entries
78
+ i = 0
79
+ self.allocations.each do |allocation|
80
+ allocation[5] = allocation[4].map do |_|
81
+ e = entries[i]
82
+ i += 1
83
+ e
84
+ end
85
+ end
86
+ end
87
+
36
88
  end
37
89
  end
@@ -87,7 +87,11 @@ end
87
87
 
88
88
  # Extend hash with to_query method.
89
89
  #
90
- require 'active_support/core_ext/object/to_query'
90
+ begin
91
+ require 'active_support/core_ext/object/to_query'
92
+ rescue LoadError
93
+
94
+ end
91
95
  class Hash
92
96
  def to_query namespace = nil
93
97
  collect do |key, value|
@@ -0,0 +1,58 @@
1
+ module Picky
2
+
3
+ # This class provides a few view helpers.
4
+ #
5
+ class Helper
6
+
7
+ @@localized_interface = lambda { |options|
8
+ search_button_text = options[:button] || 'search'
9
+ no_results = options[:no_results] || 'Sorry, no results found!'
10
+ more_allocations = options[:more] || 'more'
11
+ <<-HTML
12
+ <div id="picky">
13
+ <div class="dashboard empty">
14
+ <div class="feedback">
15
+ <div class="status"></div>
16
+ <input type="text" autocorrect="off" class="query"/>
17
+ <div class="reset" title="clear"></div>
18
+ </div>
19
+ <input type="button" class="search_button" value="#{search_button_text}">
20
+ </div>
21
+ <ol class="results"></ol>
22
+ <div class="no_results">#{no_results}</div>
23
+ <div class="allocations">
24
+ <ol class="shown"></ol>
25
+ <ol class="more">#{more_allocations}</ol>
26
+ <ol class="hidden"></ol>
27
+ </div>
28
+ </div>
29
+ HTML
30
+ }
31
+
32
+ # Returns a standard search interface for easy starting.
33
+ #
34
+ # ... aka scaffolding ;)
35
+ #
36
+ # Options:
37
+ # * button: The search button text.
38
+ # * no_results: The text shown when there are no results.
39
+ # * more: The text shown when there are more than X results.
40
+ #
41
+ # Usage, in Views:
42
+ #
43
+ # = Picky::Helper.interface :button => 'Go go go!'
44
+ #
45
+ #
46
+ def self.interface options = {}
47
+ @@localized_interface[options].gsub!(/[\n]/, '').squeeze! ' '
48
+ end
49
+
50
+ # Returns a cached version if you always use a single language.
51
+ #
52
+ def self.cached_interface options = {}
53
+ @interface ||= interface.freeze
54
+ end
55
+
56
+ end
57
+
58
+ end
data/lib/picky-client.rb CHANGED
@@ -4,7 +4,8 @@ require 'rubygems'
4
4
 
5
5
  require 'active_support'
6
6
 
7
- this = File.dirname __FILE__
8
- require File.join(this, '/picky-client/engine')
9
- require File.join(this, '/picky-client/serializer')
10
- require File.join(this, '/picky-client/convenience')
7
+ dir = File.dirname __FILE__
8
+ require File.expand_path('picky-client/engine', dir)
9
+ require File.expand_path('picky-client/serializer', dir)
10
+ require File.expand_path('picky-client/convenience', dir)
11
+ require File.expand_path('picky-client/helper', dir)
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Picky::Convenience do
4
4
 
@@ -12,21 +12,60 @@ describe Picky::Convenience do
12
12
  :duration => 0.12345
13
13
  }.extend Picky::Convenience
14
14
  end
15
-
16
- # describe 'replace_ids_with' do
17
- # before(:each) do
18
- # @results = Search::Results.new [
19
- # [nil, nil, nil, [1,2,3,4,5,6,7,8]],
20
- # [nil, nil, nil, [9,10,11,12,13,14,15,16]],
21
- # [nil, nil, nil, [17,18,19,20,21,22,23]]
22
- # ], [], nil, 123, true, true, 0.123, 1234
23
- # end
24
- # it 'should replace the ids' do
25
- # new_ids = (11..31).to_a # +10
26
- # @results.replace_ids_with new_ids
27
- # @results.ids.should == (11..31).to_a
28
- # end
29
- # end
15
+
16
+ describe "populate_with" do
17
+ before(:each) do
18
+ @results = {
19
+ :allocations => [[nil, nil, nil, nil, [1,2,3,4,5,6,7,8]],
20
+ [nil, nil, nil, nil, [9,10,11,12,13,14,15,16]],
21
+ [nil, nil, nil, nil, [17,18,19,20,21,22,23]]
22
+ ],
23
+ :offset => 123,
24
+ :duration => 0.123,
25
+ :count => 1234
26
+ }.extend Picky::Convenience
27
+
28
+ class ARClass
29
+ attr_reader :id
30
+ def initialize id
31
+ @id = id
32
+ end
33
+ def self.find ids, options = {}
34
+ ids.map { |id| new(id) }
35
+ end
36
+ def == other
37
+ self.id == other.id
38
+ end
39
+ end
40
+ end
41
+ it "should populate correctly even without a block" do
42
+ @results.populate_with ARClass
43
+ @results.entries.should == (1..21).map { |id| ARClass.new(id) }
44
+ end
45
+ it "should populate correctly with a render block" do
46
+ @results.populate_with(ARClass) { |ar_instance| ar_instance.id.to_s }
47
+ @results.entries.should == (1..21).map { |id| id.to_s } # "rendering" using to_s
48
+ end
49
+ end
50
+
51
+ describe 'replace_ids_with' do
52
+ before(:each) do
53
+ @results = {
54
+ :allocations => [[nil, nil, nil, nil, [1,2,3,4,5,6,7,8]],
55
+ [nil, nil, nil, nil, [9,10,11,12,13,14,15,16]],
56
+ [nil, nil, nil, nil, [17,18,19,20,21,22,23]]
57
+ ],
58
+ :offset => 123,
59
+ :duration => 0.123,
60
+ :count => 1234
61
+ }.extend Picky::Convenience
62
+ end
63
+ it 'should populate with the entries' do
64
+ new_ids = (11..31).to_a # +10
65
+ @results.replace_ids_with new_ids
66
+ @results.entries.should == (11..31).to_a
67
+ end
68
+ end
30
69
 
31
70
  describe 'clear_ids' do
32
71
  it 'should clear all ids' do
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Picky::Client do
4
4
 
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Picky::Helper do
4
+
5
+ describe "cached_interface" do
6
+ it "should return good html" do
7
+ Picky::Helper.cached_interface.should == Picky::Helper.interface
8
+ end
9
+ it "should return the cached interface" do
10
+ Picky::Helper.cached_interface.object_id.should == Picky::Helper.cached_interface.object_id
11
+ end
12
+ it "should be frozen" do
13
+ Picky::Helper.cached_interface.should be_frozen
14
+ end
15
+ end
16
+
17
+ describe "interface" do
18
+ it "should return good html" do
19
+ Picky::Helper.interface.should == "<div id=\"picky\"> <div class=\"dashboard empty\"> <div class=\"feedback\"> <div class=\"status\"></div> <input type=\"text\" autocorrect=\"off\" class=\"query\"/> <div class=\"reset\" title=\"clear\"></div> </div> <input type=\"button\" class=\"search_button\" value=\"search\"> </div> <ol class=\"results\"></ol> <div class=\"no_results\">Sorry, no results found!</div> <div class=\"allocations\"> <ol class=\"shown\"></ol> <ol class=\"more\">more</ol> <ol class=\"hidden\"></ol> </div></div>"
20
+ end
21
+ it "should return good html" do
22
+ Picky::Helper.interface(:button => 'find', :no_results => 'SORRY!', :more => 'Click for more!').should == "<div id=\"picky\"> <div class=\"dashboard empty\"> <div class=\"feedback\"> <div class=\"status\"></div> <input type=\"text\" autocorrect=\"off\" class=\"query\"/> <div class=\"reset\" title=\"clear\"></div> </div> <input type=\"button\" class=\"search_button\" value=\"find\"> </div> <ol class=\"results\"></ol> <div class=\"no_results\">SORRY!</div> <div class=\"allocations\"> <ol class=\"shown\"></ol> <ol class=\"more\">Click for more!</ol> <ol class=\"hidden\"></ol> </div></div>"
23
+ end
24
+ end
25
+
26
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Picky::Serializer do
4
4
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-30 00:00:00 +02:00
17
+ date: 2010-10-02 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -29,11 +29,13 @@ extra_rdoc_files:
29
29
  files:
30
30
  - lib/picky-client/convenience.rb
31
31
  - lib/picky-client/engine.rb
32
+ - lib/picky-client/helper.rb
32
33
  - lib/picky-client/serializer.rb
33
34
  - lib/picky-client.rb
34
35
  - README.rdoc
35
36
  - spec/picky-client/convenience_spec.rb
36
37
  - spec/picky-client/engine_spec.rb
38
+ - spec/picky-client/helper_spec.rb
37
39
  - spec/picky-client/serializer_spec.rb
38
40
  has_rdoc: true
39
41
  homepage: http://floere.github.com/picky
@@ -70,4 +72,5 @@ summary: picky Search Engine Client
70
72
  test_files:
71
73
  - spec/picky-client/convenience_spec.rb
72
74
  - spec/picky-client/engine_spec.rb
75
+ - spec/picky-client/helper_spec.rb
73
76
  - spec/picky-client/serializer_spec.rb