faceted 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -71,6 +71,42 @@ You can also explicitly declare the class of the association:
71
71
 
72
72
  field :genre_id, :class_name => 'MusicalGenre'
73
73
 
74
+ Presenters from Existing Models
75
+ ----
76
+ In your controllers, you will typically be using one of three methods to instantiate a presenter: `new`, `materialize`, or `from`.
77
+
78
+ ### new
79
+
80
+ This method is used to retrieve and instantiate a persisted model based on an `id`:
81
+
82
+ m = Musician.create(:name => 'Bauhaus', :genre => 'Goth') m.id
83
+ => 213
84
+
85
+ presenter = MyApi::Musician.new(:id => 213)
86
+ presenter.name
87
+ => "Bauhaus"
88
+
89
+ ### materialize
90
+
91
+ Have an array of objects that you need translated into presenters? No problem. Use the `materialize` class method on the presenter class:
92
+
93
+ musicians = [
94
+ ::Musician.new(:name => 'Love and Rockets'),
95
+ ::Musician.new(:name => 'The Pixies')
96
+ ]
97
+ presenters = MyApi::Musician.materialize(musicians)
98
+ presenters.first.name
99
+ => 'Love and Rockets'
100
+
101
+ ### from
102
+
103
+ If you have an single instance of a persisted model already loaded, or if you're presenting a class that does not get read from a database (e.g. an object from an API response), you can use the `from` class method to materialize a single presenter object:
104
+
105
+ musician_from_json_response = ::Musician.new(:name => 'Dust and a Shadow')
106
+ presenter = MyApi::Musician.from(musician_from_json_response)
107
+ presenter.name
108
+ => 'Dust and a Shadow'
109
+
74
110
  Collectors
75
111
  ----------
76
112
  Collectors are simply models that collect multiple instances of another model. An example:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.0
1
+ 1.5.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "faceted"
8
- s.version = "1.4.0"
8
+ s.version = "1.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 = "2013-09-13"
12
+ s.date = "2013-09-16"
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 = [
@@ -44,7 +44,11 @@ module Faceted
44
44
  @fields ||= [:id, :excludes]
45
45
  end
46
46
 
47
- def materialize(objects=[], args={})
47
+ def from(object, args={})
48
+ materialize([object], args).first
49
+ end
50
+
51
+ def materialize(objects=[], args={})
48
52
  objects.compact.inject([]) do |a, object|
49
53
  interface = self.new(args)
50
54
  interface.send(:object=, object)
@@ -70,6 +70,20 @@ module MyApi
70
70
 
71
71
  end
72
72
 
73
+ describe 'initialized with an instantiated object' do
74
+
75
+ let(:musician_presenter) { MyApi::Musician.from(@ar_musician) }
76
+
77
+ it 'accepts an object' do
78
+ musician_presenter.send(:object).should == @ar_musician
79
+ end
80
+
81
+ it 'initializes with the attributes of the object' do
82
+ musician_presenter.name.should == 'Johnny Cash'
83
+ end
84
+
85
+ end
86
+
73
87
  describe 'initialized with a presented object' do
74
88
 
75
89
  describe 'inherits values from its AR counterpart' do
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: 1.4.0
4
+ version: 1.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: 2013-09-13 00:00:00.000000000 Z
13
+ date: 2013-09-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -214,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
214
214
  version: '0'
215
215
  segments:
216
216
  - 0
217
- hash: 4498478211701341188
217
+ hash: -1783250443854962767
218
218
  required_rubygems_version: !ruby/object:Gem::Requirement
219
219
  none: false
220
220
  requirements: