burgundy 0.0.1

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.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +84 -0
  8. data/Rakefile +6 -0
  9. data/burgundy.gemspec +23 -0
  10. data/lib/burgundy.rb +5 -0
  11. data/lib/burgundy/collection.rb +19 -0
  12. data/lib/burgundy/item.rb +14 -0
  13. data/lib/burgundy/rails.rb +60 -0
  14. data/lib/burgundy/version.rb +3 -0
  15. data/spec/burgundy_spec.rb +71 -0
  16. data/spec/dummy/README.rdoc +28 -0
  17. data/spec/dummy/Rakefile +6 -0
  18. data/spec/dummy/app/assets/images/.keep +0 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  20. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  22. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/mailers/.keep +0 -0
  25. data/spec/dummy/app/models/.keep +0 -0
  26. data/spec/dummy/app/models/concerns/.keep +0 -0
  27. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/spec/dummy/app/views/site/contact.html.erb +0 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +4 -0
  31. data/spec/dummy/bin/rake +4 -0
  32. data/spec/dummy/config.ru +4 -0
  33. data/spec/dummy/config/application.rb +14 -0
  34. data/spec/dummy/config/boot.rb +5 -0
  35. data/spec/dummy/config/database.yml +25 -0
  36. data/spec/dummy/config/environment.rb +5 -0
  37. data/spec/dummy/config/environments/development.rb +29 -0
  38. data/spec/dummy/config/environments/production.rb +80 -0
  39. data/spec/dummy/config/environments/test.rb +31 -0
  40. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  41. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  42. data/spec/dummy/config/initializers/inflections.rb +16 -0
  43. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  44. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  45. data/spec/dummy/config/initializers/session_store.rb +3 -0
  46. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/spec/dummy/config/locales/en.yml +23 -0
  48. data/spec/dummy/config/routes.rb +3 -0
  49. data/spec/dummy/lib/assets/.keep +0 -0
  50. data/spec/dummy/log/.keep +0 -0
  51. data/spec/dummy/log/test.log +45 -0
  52. data/spec/dummy/public/404.html +58 -0
  53. data/spec/dummy/public/422.html +58 -0
  54. data/spec/dummy/public/500.html +57 -0
  55. data/spec/dummy/public/favicon.ico +0 -0
  56. data/spec/spec_helper.rb +14 -0
  57. metadata +213 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b54a032f0c4d79e05ae0dd1b1fbb5155b2025b9f
4
+ data.tar.gz: ce63a137e7d1e7838e09360fbf2e7fc246b8cae4
5
+ SHA512:
6
+ metadata.gz: e9a475b0e0e410eb4ab66762849166fbb611e7cac091523ef15e9356c35059577c5efb9d8cf3f251db0791b90f3a2371e99e4373e509d5152f4443cff8edaeb7
7
+ data.tar.gz: 5329e76c0ff9b44bd28233bb536957aa2edf9f1f7772a957663f4bb2591b835b49914672f6bce55386e41c6475464f5cf64e14a40bd2fa6b78883b2c6f173995
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in burgundy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nando Vieira
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Burgundy
2
+
3
+ A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than 100 lines.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'burgundy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install burgundy
18
+
19
+ ## Usage
20
+
21
+ First, define your wrapping class.
22
+
23
+ ```ruby
24
+ class UserPresenter < Burgundy::Item
25
+ end
26
+ ```
27
+
28
+ Then you can instantiate it:
29
+
30
+ ```ruby
31
+ user = UserPresenter.new(User.first)
32
+ ```
33
+
34
+ The `Burgundy::Item` has access to helper and route methods. Notice that the wrapper item is accessible through the `Burgundy::Item#item` method.
35
+
36
+ ```ruby
37
+ class UserPresenter < Burgundy::Item
38
+ def profile_url
39
+ routes.profile_url(item.username)
40
+ end
41
+ end
42
+ ```
43
+
44
+ You don't have to expose attributes; everything is delegated to the wrapped item.
45
+
46
+ To wrap an entire collection, just use the `Burgundy::Collection` class.
47
+
48
+ ```ruby
49
+ class WorkshopsController < ApplicationController
50
+ def index
51
+ @workshops = Burgundy::Collection.new(
52
+ Workshop.sorted_by_name,
53
+ WorkshopPresenter
54
+ )
55
+ end
56
+ end
57
+ ```
58
+
59
+ The query will be performed only when needed, usually on the view. The collection is an enumerable object and can be passed directly to the `render` method. Each item will be wrapped by the provided class.
60
+
61
+ ```erb
62
+ <%= render @workshops %>
63
+ ```
64
+
65
+ Route URLs may require the default url options. Burgundy try to get them from the following objects:
66
+
67
+ * `Rails.configuration.action_mailer.default_url_options`
68
+ * `Rails.application.routes.default_url_options`
69
+
70
+ So you can just put this on your environment file
71
+
72
+ ```ruby
73
+ config.action_controller.default_url_options = {
74
+ host: "example.org"
75
+ }
76
+ ```
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/burgundy.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ require "./lib/burgundy/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "burgundy"
5
+ spec.version = Burgundy::VERSION
6
+ spec.authors = ["Nando Vieira"]
7
+ spec.email = ["fnando.vieira@gmail.com"]
8
+ spec.description = "A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than 100 lines."
9
+ spec.summary = spec.description
10
+ spec.homepage = "http://github.com/fnando/burgundy"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files`.split($/)
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_development_dependency "bundler", "~> 1.3"
19
+ spec.add_development_dependency "rake"
20
+ spec.add_development_dependency "rspec"
21
+ spec.add_development_dependency "rails"
22
+ spec.add_development_dependency "rspec-rails"
23
+ end
data/lib/burgundy.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "delegate"
2
+ require "burgundy/version"
3
+ require "burgundy/item"
4
+ require "burgundy/collection"
5
+ require "burgundy/rails" if defined?(Rails)
@@ -0,0 +1,19 @@
1
+ module Burgundy
2
+ class Collection
3
+ include Enumerable
4
+
5
+ def initialize(items, wrapping_class)
6
+ @items = items
7
+ @wrapping_class = wrapping_class
8
+ end
9
+
10
+ def each(&block)
11
+ to_ary.each(&block)
12
+ end
13
+
14
+ def to_ary
15
+ @cache ||= @wrapping_class.map(@items.to_a)
16
+ end
17
+ alias_method :to_a, :to_ary
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module Burgundy
2
+ class Item < SimpleDelegator
3
+ attr_reader :item
4
+
5
+ def self.map(collection)
6
+ collection.map {|item| new(item) }
7
+ end
8
+
9
+ def initialize(item)
10
+ @item = item
11
+ __setobj__(item)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,60 @@
1
+ module Burgundy
2
+ module UrlMethods
3
+ def default_url_options
4
+ action_mailer_default_url_options ||
5
+ Rails.application.routes.default_url_options
6
+ end
7
+
8
+ def action_mailer_default_url_options
9
+ Rails.configuration.action_mailer.default_url_options if Rails.configuration.respond_to?(:action_mailer)
10
+ end
11
+ end
12
+
13
+ module Helpers
14
+ extend ActiveSupport::Concern
15
+
16
+ included do
17
+ delegate :translate, :t, :localize, :l, to: :helpers
18
+ end
19
+
20
+ def helpers
21
+ ApplicationController.helpers
22
+ end
23
+ alias_method :h, :helpers
24
+ end
25
+
26
+ module RouteHelpers
27
+ extend ActiveSupport::Concern
28
+
29
+ module ClassMethods
30
+ def routes_module
31
+ @routes_module ||= Module.new do
32
+ include Rails.application.routes.url_helpers
33
+ include UrlMethods
34
+ end
35
+ end
36
+
37
+ def routes
38
+ @routes ||= Object.new.extend(routes_module)
39
+ end
40
+ end
41
+
42
+ def to_param
43
+ item.to_param
44
+ end
45
+
46
+ def eql?(obj)
47
+ obj == self || item.eql?(obj)
48
+ end
49
+
50
+ def routes
51
+ self.class.routes
52
+ end
53
+ alias_method :r, :routes
54
+ end
55
+
56
+ class Item
57
+ include Helpers
58
+ include RouteHelpers
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Burgundy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,71 @@
1
+ require "spec_helper"
2
+
3
+ describe Burgundy do
4
+ let(:wrapper) { Class.new(Burgundy::Item) }
5
+
6
+ it "delegates everything" do
7
+ item = wrapper.new("hello")
8
+ expect(item.upcase).to eql("HELLO")
9
+ end
10
+
11
+ it "wraps items" do
12
+ items = wrapper.map([1,2,3])
13
+
14
+ expect(items.first).to be_a(wrapper)
15
+ expect(items.first.to_i).to eql(1)
16
+ end
17
+
18
+ it "wraps items in collection" do
19
+ collection = Burgundy::Collection.new([1,2,3], wrapper)
20
+ expect(collection.first).to eql(1)
21
+ end
22
+
23
+ it "includes Enumerable" do
24
+ expect(Burgundy::Collection).to include(Enumerable)
25
+ end
26
+
27
+ it "responds to the routes method" do
28
+ item = wrapper.new("hello")
29
+
30
+ expect(item.respond_to?(:routes, true)).to be
31
+ expect(item.respond_to?(:r, true)).to be
32
+ end
33
+
34
+ it "responds to the helpers method" do
35
+ item = wrapper.new("hello")
36
+
37
+ expect(item.respond_to?(:helpers, true)).to be
38
+ expect(item.respond_to?(:h, true)).to be
39
+ end
40
+
41
+ it "responds to the I18n methods" do
42
+ item = wrapper.new("hello")
43
+
44
+ expect(item.respond_to?(:translate, true)).to be
45
+ expect(item.respond_to?(:t, true)).to be
46
+ expect(item.respond_to?(:localize, true)).to be
47
+ expect(item.respond_to?(:l, true)).to be
48
+ end
49
+
50
+ it "returns route using action mailer options" do
51
+ wrapper = Class.new(Burgundy::Item) do
52
+ def profile_url; routes.profile_url(username) end
53
+ end
54
+
55
+ Rails.configuration.action_mailer.default_url_options = {host: "example.org"}
56
+ item = wrapper.new OpenStruct.new(username: "johndoe")
57
+
58
+ expect(item.profile_url).to eql("http://example.org/johndoe")
59
+ end
60
+
61
+ it "returns route using action controller options" do
62
+ wrapper = Class.new(Burgundy::Item) do
63
+ def profile_url; routes.profile_url(username) end
64
+ end
65
+
66
+ Rails.application.routes.default_url_options = {host: "example.org"}
67
+ item = wrapper.new OpenStruct.new(username: "johndoe")
68
+
69
+ expect(item.profile_url).to eql("http://example.org/johndoe")
70
+ end
71
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end