faceted 0.4.20 → 0.5.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.4.20
1
+ 0.5.0
data/faceted.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "faceted"
8
- s.version = "0.4.20"
8
+ s.version = "0.5.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-17"
12
+ s.date = "2012-09-18"
13
13
  s.description = "Provides useful tools for API implementations."
14
14
  s.email = "corey@trunkclub.com"
15
15
  s.extra_rdoc_files = [
@@ -28,7 +28,9 @@ Gem::Specification.new do |s|
28
28
  "VERSION",
29
29
  "faceted.gemspec",
30
30
  "lib/faceted.rb",
31
+ "lib/faceted/collector.rb",
31
32
  "lib/faceted/presenter.rb",
33
+ "spec/collector_spec.rb",
32
34
  "spec/presenter_spec.rb",
33
35
  "spec/spec_helper.rb"
34
36
  ]
data/lib/faceted.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  module Faceted
2
2
  require 'faceted/presenter'
3
+ require 'faceted/collector'
3
4
  end
@@ -0,0 +1,59 @@
1
+ module Faceted
2
+
3
+ module Collector
4
+
5
+ require 'json'
6
+ require 'active_support/core_ext/hash'
7
+
8
+ def self.included(base)
9
+ base.extend ActiveModel::Naming
10
+ base.send(:attr_accessor, :errors)
11
+ base.send(:attr_accessor, :success)
12
+ base.extend ClassMethods
13
+ end
14
+
15
+ # Class methods ===========================================================
16
+
17
+ module ClassMethods
18
+
19
+ def collects(name, args={})
20
+ @collects = eval "#{scope}#{args[:class_name] || name.to_s.classify}"
21
+ define_method :"#{name.downcase}" do
22
+ self.objects
23
+ end
24
+ define_method :finder do
25
+ {"#{args[:find_by]}" => self.send(args[:find_by])}
26
+ end
27
+ self.send(:attr_accessor, args[:find_by])
28
+ end
29
+
30
+ def collected_class
31
+ @collects
32
+ end
33
+
34
+ def scope
35
+ parent.to_s == "Object" ? "::" : "#{parent.to_s}::"
36
+ end
37
+
38
+ end
39
+
40
+ # Instance methods =========================================================
41
+
42
+ def initialize(args={})
43
+ ! args.empty? && args.symbolize_keys.delete_if{|k,v| v.nil?}.each{|k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") && ! v.blank? }
44
+ self.errors = []
45
+ self.success = true
46
+ end
47
+
48
+ def objects
49
+ return unless self.class.collected_class
50
+ @objects ||= self.class.collected_class.where(self.finder)
51
+ end
52
+
53
+ def to_hash
54
+ self.objects.map{|o| o.to_hash}
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ class Musician # pretend that this is an AR model
4
+ attr_accessor :id, :name, :rating, :errors, :birthplace_id
5
+ def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
6
+ def attributes; {:id => self.id, :name => self.name, :rating => self.rating}; end
7
+ end
8
+
9
+ class Birthplace # another make-believe AR model
10
+ attr_accessor :id, :city, :state
11
+ def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
12
+ def attributes; {:id => self.id, :city => self.city, :state => self.state}; end
13
+ end
14
+
15
+ module MyApi
16
+
17
+ class Birthplace
18
+ include Faceted::Presenter
19
+ presents :birthplace
20
+ field :city
21
+ field :state
22
+ end
23
+
24
+ class Musician
25
+ include Faceted::Presenter
26
+ presents :musician
27
+ field :name
28
+ field :rating
29
+ field :birthplace_id
30
+ end
31
+
32
+ class Band
33
+ include Faceted::Collector
34
+ collects :musicians, :class_name => 'Musician', :find_by => :birthplace_id
35
+ end
36
+
37
+ describe Band do
38
+
39
+ before do
40
+ end
41
+
42
+ it 'creates an accessor method for its collected objects' do
43
+ Band.new.respond_to?(:musicians).should be_true
44
+ end
45
+
46
+ describe 'with associated objects' do
47
+
48
+ it 'initializes the associated objects in the correct namespace' do
49
+ band = MyApi::Band.new(:birthplace_id => 1)
50
+ MyApi::Musician.stub(:where) { [MyApi::Musician.new] }
51
+ band.musicians.first.class.name.should == "MyApi::Musician"
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ 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.4.20
4
+ version: 0.5.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-17 00:00:00.000000000 Z
13
+ date: 2012-09-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -159,7 +159,9 @@ files:
159
159
  - VERSION
160
160
  - faceted.gemspec
161
161
  - lib/faceted.rb
162
+ - lib/faceted/collector.rb
162
163
  - lib/faceted/presenter.rb
164
+ - spec/collector_spec.rb
163
165
  - spec/presenter_spec.rb
164
166
  - spec/spec_helper.rb
165
167
  homepage: http://github.com/trunkclub/faceted
@@ -177,7 +179,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
177
179
  version: '0'
178
180
  segments:
179
181
  - 0
180
- hash: 245053931040836749
182
+ hash: 3092283735648102372
181
183
  required_rubygems_version: !ruby/object:Gem::Requirement
182
184
  none: false
183
185
  requirements: