faceted 0.8.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.3
1
+ 1.0.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "faceted"
8
- s.version = "0.8.3"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Corey Ehmke", "Max Thom Stahl"]
12
- s.date = "2012-09-27"
12
+ s.date = "2012-10-03"
13
13
  s.description = "Faceted provides set of tools, patterns, and modules for use in API implementations."
14
14
  s.email = "corey@trunkclub.com"
15
15
  s.extra_rdoc_files = [
@@ -19,17 +19,19 @@ module Faceted
19
19
 
20
20
  def collects(name, args={})
21
21
  @fields = [name]
22
- @collects = eval "#{scope}#{args[:class_name] || name.to_s.classify}"
22
+ find_by = args[:find_by] ? args[:find_by] : "#{name.to_s.downcase.singularize}_id"
23
+ @collects ||= {}
24
+ @collects[name.downcase] = eval "#{scope}#{args[:class_name] || name.to_s.classify}"
23
25
  define_method :"#{name.downcase}" do
24
- objects
26
+ objects(name.downcase.to_sym)
25
27
  end
26
- define_method :finder do
27
- {"#{args[:find_by]}" => self.send(args[:find_by])}
28
+ define_method :"#{name.downcase}_finder" do
29
+ {"#{find_by}" => self.send(find_by)}
28
30
  end
29
- self.send(:attr_accessor, args[:find_by])
31
+ self.send(:attr_accessor, find_by)
30
32
  end
31
33
 
32
- def collected_class
34
+ def collected_classes
33
35
  @collects
34
36
  end
35
37
 
@@ -51,9 +53,10 @@ module Faceted
51
53
 
52
54
  private
53
55
 
54
- def objects
55
- return unless self.class.collected_class
56
- @objects ||= self.class.collected_class.where(self.finder)
56
+ def objects(klass)
57
+ return [] unless self.class.collected_classes
58
+ return [] unless self.class.collected_classes.keys.include?(klass)
59
+ self.class.collected_classes[klass].where(self.send("#{klass.to_s}_finder"))
57
60
  end
58
61
 
59
62
  end
@@ -1,17 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
- class Musician # pretend that this is an AR model
3
+ class Musician # Mock AR model
4
4
  attr_accessor :id, :name, :rating, :errors, :birthplace_id
5
5
  def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
6
6
  def attributes; {:id => self.id, :name => self.name, :rating => self.rating}; end
7
7
  end
8
8
 
9
- class Birthplace # another make-believe AR model
9
+ class Birthplace # Mock AR model
10
10
  attr_accessor :id, :city, :state
11
11
  def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
12
12
  def attributes; {:id => self.id, :city => self.city, :state => self.state}; end
13
13
  end
14
14
 
15
+ class Song # Mock AR model
16
+ attr_accessor :id, :title, :rating
17
+ def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
18
+ def attributes; {:id => self.id, :title => self.title}; end
19
+ end
20
+
15
21
  module MyApi
16
22
 
17
23
  class Birthplace
@@ -29,18 +35,24 @@ module MyApi
29
35
  field :birthplace_id
30
36
  end
31
37
 
38
+ class Song
39
+ include Faceted::Presenter
40
+ presents :song
41
+ field :title
42
+ field :rating
43
+ end
44
+
32
45
  class Band
33
46
  include Faceted::Collector
34
47
  collects :musicians, :find_by => :birthplace_id
48
+ collects :songs
35
49
  end
36
50
 
37
51
  describe Band do
38
52
 
39
- before do
40
- end
41
-
42
53
  it 'creates an accessor method for its collected objects' do
43
54
  Band.new.respond_to?(:musicians).should be_true
55
+ Band.new.respond_to?(:songs).should be_true
44
56
  end
45
57
 
46
58
  describe 'with associated objects' do
@@ -48,7 +60,9 @@ module MyApi
48
60
  it 'initializes the associated objects in the correct namespace' do
49
61
  band = MyApi::Band.new(:birthplace_id => 1)
50
62
  MyApi::Musician.stub(:where) { [MyApi::Musician.new] }
63
+ MyApi::Song.stub(:where) { [MyApi::Song.new] }
51
64
  band.musicians.first.class.name.should == "MyApi::Musician"
65
+ band.songs.first.class.name.should == "MyApi::Song"
52
66
  end
53
67
 
54
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faceted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-27 00:00:00.000000000 Z
13
+ date: 2012-10-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -181,7 +181,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
181
181
  version: '0'
182
182
  segments:
183
183
  - 0
184
- hash: -13865521977452324
184
+ hash: 1204187921055065729
185
185
  required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  none: false
187
187
  requirements: