representable_resources 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ class RepresenterGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path("../templates", __FILE__)
3
+
4
+ def create_representer_file
5
+ template "representer.rb", "app/representers/#{file_name}_representer.rb"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ class <%= class_name %>Representer < RepresentableResources::Base
4
+
5
+ # property :id
6
+ # property :name
7
+ # collection :my_collection, MyRepresentableResource
8
+
9
+ end
@@ -0,0 +1,6 @@
1
+ require 'representable_resources/base'
2
+
3
+ module RepresentableResources
4
+ class Base < Representable::Base
5
+ end
6
+ end
@@ -0,0 +1,55 @@
1
+
2
+ 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
+ class Base
21
+ extend Representable
22
+
23
+ def initialize(object)
24
+ @object = object
25
+ 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
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module RepresentableResources
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: representable_resources
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lucas Allan Amorim
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! 'Rubygem to create representable resources of your models. This gem
31
+ is kind of class proxy to define what fields you want show in your representable
32
+ resource. '
33
+ email:
34
+ - lucas.allan@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/generators/representer_generator.rb
40
+ - lib/generators/templates/representer.rb
41
+ - lib/representable_resources/base.rb
42
+ - lib/representable_resources/version.rb
43
+ - lib/representable_resources.rb
44
+ homepage: ''
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.22
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Rubygem to create representable resources of your models.
68
+ test_files: []