cyrax 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZjUxOTdlY2I2N2FkZmE0MDVhZWNlNjQwY2IxNTE5OTc4NGQ4MzYxMQ==
4
+ ZWUxYTQwNmI1YzlmOGRmZWE2OWIwNDMzY2QwYzc1ZWM0ODliYTI2OA==
5
5
  data.tar.gz: !binary |-
6
- MDkyNWFmZDk2Y2NhYTQ3ZTYwZWE2MDAzYzM3NmIwZGI2YTE4ZjAxYg==
6
+ YTEzMGY5MjgxYjVmYzYwODYwZmRmNWJmYWI0NWQ3MjkzNzE2YjcyMg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZThmZDE0ODA2MTc1YjRkZjRlMTQ5YTU1ODNjZmRiNDAxODc1MjhjYjA0ZDJj
10
- YjgwNmNjZjI0MTdiMGM2N2IwY2JkMjg5YWU3NGQ3ZDQ4MDE4ZWUwOTA5Njhi
11
- MWM5ODQwZGVjODU2ZThlMWZmZDMwMzZmOGI1NmI5M2JkMjZkYzQ=
9
+ MzgxYTRmODEwYzNiZjY1YWRhYTlkZDY3ZTdjNTBhY2M5ODY4MmJlOTdlNDAz
10
+ MDdkZTYyMDY4ZGQ2ZDg2NzI3MjBjMWE1MWFmNmUxMWZiZWI2ZTFlOWJhN2Yw
11
+ ZjhiOTk1NjhjOWRlMzg0MGE1NDE4YzUwNzIxMjExNTJhNDAwOGU=
12
12
  data.tar.gz: !binary |-
13
- MmEzYTFhYzAwZWZlM2E2MzZjM2Q1YjM0MWRlY2YyMmQ5NmJjYzM5MTE4ODhl
14
- ZjRmYzA0MDFmZDRiNzhlOWJiOGIwN2ZlM2Y5YmM4NWU4NDgxODE3MzIwN2Qy
15
- MjdmZWUzMTY0ODU1ODc3NGZkNjMxYjllZTgxMDJlNWM2ZThhMWE=
13
+ MWVhYjc5MzIyMjQ0OWExMTE5MTQ4ZTAxMDVmYWZhZjg2NjNkZWZhN2YzMTQ5
14
+ MDdiNGJhN2ZmOGEwNzBhZjFmMjg0OWUxMDFlMmUxMWQzMzIwMWQwYzVlNDA1
15
+ YzM0ZGNlYmYzMjViODE3MmM1NjI4ZmRiZjA0MjFjYWY3ZGMzZmE=
data/lib/cyrax.rb CHANGED
@@ -6,8 +6,10 @@ require "cyrax/extensions/has_service.rb"
6
6
  require "cyrax/extensions/has_decorator.rb"
7
7
  require "cyrax/base.rb"
8
8
  require "cyrax/base_resource.rb"
9
+ require "cyrax/base_presenter.rb"
9
10
  require "cyrax/response.rb"
10
11
  require "cyrax/callbacks.rb"
12
+ require "cyrax/collection_decorator.rb"
11
13
  require "cyrax/decorator.rb"
12
14
 
13
15
  module Cyrax
@@ -0,0 +1,29 @@
1
+ class Cyrax::BasePresenter
2
+
3
+ def present(object, options = {})
4
+ should_decorate = options[:decorate].nil? || options[:decorate]
5
+ if options[:decorable] && should_decorate
6
+ present_with_decoration(object, options)
7
+ else
8
+ object
9
+ end
10
+ end
11
+
12
+ class << self
13
+
14
+ def present(object, options = {})
15
+ self.new.present(object, options)
16
+ end
17
+
18
+ end
19
+
20
+ private
21
+
22
+ def present_with_decoration(object, options)
23
+ if options[:present] == :collection
24
+ options[:decorator_class].decorate_collection(object)
25
+ else
26
+ options[:decorator_class].decorate(object)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ class Cyrax::CollectionDecorator
2
+ attr_reader :decorator_class, :collection
3
+
4
+ def initialize(collection, options = {})
5
+ @collection = collection
6
+ @decorator_class = options[:decorator_class]
7
+ raise "Decorator class is not defined! Please define it with option :decorator_class" unless decorator_class
8
+ end
9
+
10
+ class << self
11
+ alias_method :decorate, :new
12
+ end
13
+
14
+ array_methods = Array.instance_methods - Object.instance_methods
15
+ delegate *array_methods, to: :decorated_collection
16
+
17
+ def decorated_collection
18
+ @decorated_collection ||= fetched_collection.map {|item| decorate_item(item)}
19
+ end
20
+
21
+ def fetched_collection
22
+ if collection.is_a?(ActiveRecord::Relation)
23
+ collection.to_a
24
+ elsif collection.respond_to?(:all)
25
+ collection.all
26
+ else
27
+ Array.wrap collection
28
+ end
29
+ end
30
+
31
+ def method_missing(method, *args, &block)
32
+ return super unless collection.respond_to?(method)
33
+ collection.send(method, *args, &block)
34
+ end
35
+
36
+ private
37
+
38
+ def decorate_item(item)
39
+ decorator_class.new(item)
40
+ end
41
+ end
@@ -24,30 +24,14 @@ class Cyrax::Decorator
24
24
 
25
25
  def method_missing(method, *args, &block)
26
26
  return super unless resource.respond_to?(method)
27
-
28
27
  resource.send(method, *args, &block)
29
28
  end
30
29
 
31
30
  class << self
32
- def fetch(resource)
33
- if resource.is_a?(ActiveRecord::Relation)
34
- resource.to_a
35
- elsif resource.respond_to?(:all)
36
- resource.all
37
- else
38
- resource
39
- end
40
- end
31
+ alias_method :decorate, :new
41
32
 
42
- def decorate(resource)
43
- resource = fetch(resource)
44
- if resource.is_a?(Array)
45
- resource.map do |item|
46
- self.new(item)
47
- end
48
- else
49
- self.new(resource)
50
- end
33
+ def decorate_collection(resource)
34
+ Cyrax::CollectionDecorator.decorate(resource, decorator_class: self)
51
35
  end
52
36
  end
53
37
  end
@@ -32,8 +32,8 @@ module Cyrax::Extensions
32
32
 
33
33
  def respond_with(resource, options = {})
34
34
  name = options[:name] || resource_name
35
- should_decorate = options[:decorate].nil? || options[:decorate]
36
- result = decorable? && should_decorate ? decorator_class.decorate(resource) : resource
35
+ presenter_options = options.merge(decorable: decorable?, decorator_class: decorator_class)
36
+ result = Cyrax::BasePresenter.present(resource, presenter_options)
37
37
  response = Cyrax::Response.new(name, result)
38
38
  response.message = @_message
39
39
  response.errors = @_errors
@@ -3,7 +3,7 @@ module Cyrax::Extensions
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  def collection
6
- respond_with wrapped_collection, name: collection_name
6
+ respond_with build_collection, name: collection_name, present: :collection
7
7
  end
8
8
  alias_method :read_all, :collection
9
9
 
data/lib/cyrax/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cyrax
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -41,7 +41,7 @@ module Cyrax
41
41
  describe '#collection' do
42
42
  it 'responds with decorated collection' do
43
43
  subject.should_receive(:build_collection).and_return(collection)
44
- subject.should_receive(:respond_with).with(collection, name: 'foos')
44
+ subject.should_receive(:respond_with).with(collection, name: 'foos', present: :collection)
45
45
  subject.collection
46
46
  end
47
47
  end
@@ -7,6 +7,7 @@ module Cyrax
7
7
  subject { self }
8
8
  before do
9
9
  subject.stub!(:decorable?).and_return(false)
10
+ subject.stub!(:decorator_class).and_return(nil)
10
11
  end
11
12
 
12
13
  describe '#set_message' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyrax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droidlabs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-18 00:00:00.000000000 Z
11
+ date: 2013-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -73,8 +73,10 @@ extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
75
  - lib/cyrax/base.rb
76
+ - lib/cyrax/base_presenter.rb
76
77
  - lib/cyrax/base_resource.rb
77
78
  - lib/cyrax/callbacks.rb
79
+ - lib/cyrax/collection_decorator.rb
78
80
  - lib/cyrax/decorator.rb
79
81
  - lib/cyrax/extensions/has_callbacks.rb
80
82
  - lib/cyrax/extensions/has_decorator.rb