representable_resources 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,55 +1,15 @@
1
+ require 'representable/resource'
2
+ require 'representable/collection'
3
+ require 'errors/invalid_resource_error'
1
4
 
2
5
  module Representable
3
-
4
- def property(property_attr)
5
- self.representable_fields << property_attr
6
- end
7
-
8
- def representable_fields
9
- @representable_fields ||= []
10
- end
11
-
12
- def collection(collection, klass)
13
- self.representable_collections << [ klass, collection]
14
- end
15
-
16
- def representable_collections
17
- @representable_collections ||= []
18
- end
19
-
20
6
  class Base
21
- extend Representable
7
+ extend Representable::Resource
8
+ include Representable::Collection
22
9
 
23
10
  def initialize(object)
24
11
  @object = object
25
12
  end
26
-
27
- def to_hash
28
- property_hash = {}
29
- self.class.representable_fields.each do |field|
30
- field = field.to_sym
31
- property_hash.merge!(field => @object.send(field))
32
- end
33
- property_hash.merge!(:target_class => @object.class.to_s)
34
- get_collections.each do |collection|
35
- property_hash.merge!(collection)
36
- end
37
- property_hash
38
- end
39
-
40
- protected
41
-
42
- def get_collections
43
- collections = []
44
- self.class.representable_collections.each do |r|
45
- objects = []
46
- @object.send(r[1]).each do |obj|
47
- objects << r[0].new(obj).to_hash
48
- end
49
- collections << {r[1] => objects}
50
- end
51
- collections
52
- end
53
13
 
54
14
  end
55
15
  end
@@ -0,0 +1,2 @@
1
+ class InvalidResourceError < Exception
2
+ end
@@ -0,0 +1,43 @@
1
+ module Representable
2
+ module Collection
3
+
4
+ def to_hash
5
+ @property_hash ||= build_hash
6
+ end
7
+ protected
8
+
9
+ def build_hash
10
+ property_hash = {}
11
+ if @object
12
+ self.class.representable_fields.each do |field|
13
+ field = field.to_sym
14
+ property_hash.merge!(field => object_for(field))
15
+ end
16
+ property_hash.merge!(:target_class => @object.class.to_s)
17
+ add_collections_to!(property_hash)
18
+ add_resources_to!(property_hash)
19
+ end
20
+ property_hash
21
+ end
22
+
23
+ def add_collections_to!(hash)
24
+ self.class.representable_collections.each do |r|
25
+ objects = []
26
+ object_for(r[1]).each do |obj|
27
+ objects << r[0].new(obj).to_hash
28
+ end
29
+ hash.merge!(r[1] => objects)
30
+ end
31
+ end
32
+
33
+ def add_resources_to!(hash)
34
+ self.class.representable_resources.each do |r|
35
+ hash.merge!(r[1] => r[0].new(object_for(r[1])).to_hash)
36
+ end
37
+ end
38
+
39
+ def object_for(property)
40
+ @object.send(property) if @object
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ module Representable
2
+ module Resource
3
+ def property(property_attr)
4
+ self.representable_fields << property_attr
5
+ end
6
+
7
+ def representable_fields
8
+ @representable_fields ||= []
9
+ end
10
+
11
+ def collection(collection, klass)
12
+ self.representable_collections << [klass, collection]
13
+ end
14
+
15
+ def resource(resource_attr, klass)
16
+ self.representable_resources << [klass, resource_attr]
17
+ end
18
+
19
+ def representable_collections
20
+ @representable_collections ||= []
21
+ end
22
+
23
+ def representable_resources
24
+ @representable_resources ||= []
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module RepresentableResources
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representable_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -39,6 +39,9 @@ files:
39
39
  - lib/generators/representer_generator.rb
40
40
  - lib/generators/templates/representer.rb
41
41
  - lib/representable_resources/base.rb
42
+ - lib/representable_resources/errors/invalid_resource_error.rb
43
+ - lib/representable_resources/representable/collection.rb
44
+ - lib/representable_resources/representable/resource.rb
42
45
  - lib/representable_resources/version.rb
43
46
  - lib/representable_resources.rb
44
47
  homepage: ''