presenter_object 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b2b81e23c06a961f66b04f603fd5e99dd7c9d19
4
+ data.tar.gz: 08f1c1059ad498a3c093acf8d890f2c9c8aaecee
5
+ SHA512:
6
+ metadata.gz: 6615515a4ffaadb2f993bb7c3acf06dae12060e79ca82c741839d2fb93491c8654a60fa8d3f0a1461097c732bf065612774f0170027d56210ac514128c35c2a5
7
+ data.tar.gz: 2cae90ea5004842feb15d9f83735f4e8cf19c2430e3afe9c168a1369a018c993fc8e893c4169e47f41160332399c7524eeec9699de88ed766be8753d8dd2212f
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in presenter_object.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # PresenterObject
2
+
3
+ This is a simple presenter base class you can use to create model specific presenters.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'presenter_object'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install presenter_object
20
+
21
+ ## Usage
22
+
23
+ Given a model class:
24
+
25
+ ```ruby
26
+ class User < ActiveRecord::Base
27
+ validates :first_name, presence: true # just to show one of the attributes of this model
28
+ end
29
+ ```
30
+
31
+ Create a subclass such as:
32
+
33
+ ```ruby
34
+ class UserPresenter < PresenterObject::Base
35
+ presents :user
36
+
37
+ def formatted_phone_number
38
+ I18n.l phone_number, format: :fancy
39
+ end
40
+ end
41
+ ```
42
+
43
+ Then wrap a model instance:
44
+
45
+ ```ruby
46
+ @user = UserPresenter.new User.find(params[:id])
47
+ ```
48
+
49
+ The `@user` presenter object can be used as if it were the model object. Then, just place all presentation methods in the presenter class and call them from the views.
50
+
51
+ ```ruby
52
+ @user.class # => User
53
+ @user.respond_to? :first_name # => true
54
+ @user.formatted_phone_number # => "(555) 444-3322"
55
+ ```
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DiegoSalazar/presenter_object.
66
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "presenter_object"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ # Presenter superclass. Create your own presenter by sub-classing `PresenterObject::Base`
2
+ # and declaring the name of the model it will present.
3
+ #
4
+ # e.g:
5
+ #
6
+ # class DocumentPresenter << PresenterObject::Base
7
+ # presents :document
8
+ #
9
+ # .. add instance methods here ..
10
+ # end
11
+ #
12
+ # Create a presenter instance by giving it a model instance e.g:
13
+ #
14
+ # document = DocumentPresenter.new Document.find(params[:id])
15
+ # document.sender # can call any document model methods...
16
+ # document.pretty_sender # ...as well as any presenter defined method
17
+ # document.class # is the wrapped object's class, helps it behave like models do in forms, link helpers etc.
18
+ #
19
+ class PresenterObject::Base
20
+ class << self
21
+ def presents(class_name)
22
+ @class_name = class_name.to_s.classify
23
+ presenters[@class_name] = self
24
+ end
25
+
26
+ def presented_class
27
+ @presented_class ||= @class_name.constantize
28
+ end
29
+
30
+ def presenters
31
+ @@presenters ||= {}
32
+ end
33
+ end
34
+
35
+ attr_reader :object, :class
36
+
37
+ def initialize(object)
38
+ @object, @class = object, object.class
39
+ end
40
+
41
+ def method_missing(name, *args, &block)
42
+ super unless object.respond_to? name
43
+ object.send name, *args, &block
44
+ end
45
+
46
+ def respond_to?(name, include_private = false)
47
+ object.respond_to? name, include_private or super
48
+ end
49
+ end
@@ -0,0 +1,27 @@
1
+ # Include this concern in a model class to easily wrap the model in a presenter e.g:
2
+ #
3
+ # class Document < ActiveRecord::Base
4
+ # include PresenterObject::Presentable
5
+ # end
6
+ #
7
+ # Now turn any instance into a presenter-wrapped model e.g:
8
+ #
9
+ # document = Document.find(params[:id]).presenterize
10
+ #
11
+ module PresenterObject::Presentable
12
+ extend ActiveSupport::Concern
13
+
14
+ module ClassMethods
15
+ def presenter_class
16
+ PresenterObject::Base.presenters.fetch name
17
+ end
18
+ end
19
+
20
+ def presenter_class
21
+ self.class.presenter_class
22
+ end
23
+
24
+ def presenterize
25
+ presenter_class.new self
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module PresenterObject
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "presenter_object/version"
2
+
3
+ module PresenterObject
4
+ autoload :Base, "presenter_object/base"
5
+ autoload :Presentable, "presenter_object/presentable"
6
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'presenter_object/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "presenter_object"
8
+ spec.version = PresenterObject::VERSION
9
+ spec.authors = ["Diego Salazar"]
10
+ spec.email = ["diego.salazar@kipusystems.com"]
11
+ spec.license = "MIT"
12
+ spec.summary = %q{Prsenter object base class.}
13
+ spec.description = %q{Useful for wrapping model objects with presentation methods.}
14
+ spec.homepage = "https://github.com/DiegoSalazar/presenter_object"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.12"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.5"
32
+ spec.add_development_dependency "activesupport", "~> 4.2"
33
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: presenter_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Diego Salazar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ description: Useful for wrapping model objects with presentation methods.
70
+ email:
71
+ - diego.salazar@kipusystems.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/presenter_object.rb
84
+ - lib/presenter_object/base.rb
85
+ - lib/presenter_object/presentable.rb
86
+ - lib/presenter_object/version.rb
87
+ - presenter_object.gemspec
88
+ homepage: https://github.com/DiegoSalazar/presenter_object
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ allowed_push_host: https://rubygems.org
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Prsenter object base class.
113
+ test_files: []