geckorate 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3b4a0eb4dd2abcd1c8e09b079521270efa61374f07bbacf748fff6b8b53a0efe
4
+ data.tar.gz: 13240fe99a8bf10d132ff5c99053fe21e301a608559f5c074ab3a1538a258eb4
5
+ SHA512:
6
+ metadata.gz: 36631b16505625736a025546b8f6965c528a710f40be461b41627838eb97778bf2229ad50ca2ce8a94b6b3730fc83fad4062db808f8802f99644820609d32227
7
+ data.tar.gz: adeeb69a7ab6c6c7d687b83aa4d67fa4bcda7e5e1bc22f758fbf6346dc6f0dac291bd617860294890b53611e7f4e7ba3b3f1ea0f23cc0b6a9086dce4ffbe100d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in geckorate.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ahmed Saleh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # Geckorate
2
+
3
+ A dead simple decorator for Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'geckorate'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install geckorate
20
+
21
+ ## Usage
22
+
23
+ ### A Rails example
24
+
25
+ Define your decorator as such:
26
+
27
+ ```ruby
28
+ # app/decorators/post_decorator.rb
29
+
30
+ class PostDecorator < Geckorate::Decorator
31
+ def decorate
32
+ {
33
+ title: title,
34
+ username: user.name
35
+ }.to_json
36
+ end
37
+ end
38
+ ```
39
+
40
+ and initialize it in your controller and use it for your view:
41
+ ```ruby
42
+ # app/controllers/posts_contrller.rb
43
+
44
+ class PostsController < ApplicationController
45
+ def show
46
+ post = Post.find(params[:id])
47
+ decorated_post = PostDecorator.new(post).decorate
48
+
49
+ render json: { post: decorated_post }
50
+ end
51
+ end
52
+ ```
53
+
54
+ ### For collections
55
+
56
+ There's a method `decorate_collection(collection)` for decorating collections.
57
+ Example: `PostDecorator.decorate_collection(Post.all)` returns an array of
58
+ decorated Post objects.
59
+
60
+ ### With paginated collections
61
+
62
+ Currently, there are two methods for pagination; one for Kaminari
63
+ `decorate_kaminari_collection(collection)` and another for will_paginate
64
+ `decorate_will_paginate_collection(collection)`. The returned hash from both
65
+ methods looks like this:
66
+
67
+ ```ruby
68
+ {
69
+ page: 1,
70
+ per_page: 25,
71
+ total: 100,
72
+ records: [{...}, {...}]
73
+ }
74
+ ```
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
79
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
80
+ prompt that will allow you to experiment.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`. To
83
+ release a new version, update the version number in `version.rb`, and then run
84
+ `bundle exec rake release`, which will create a git tag for the version, push
85
+ git commits and tags, and push the `.gem` file to
86
+ [rubygems.org](https://rubygems.org).
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at
91
+ https://github.com/aonemd/geckorate.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the
96
+ [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "geckorate"
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(__FILE__)
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
data/geckorate.gemspec ADDED
@@ -0,0 +1,25 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "geckorate/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "geckorate"
8
+ spec.version = Geckorate::VERSION
9
+ spec.authors = ["Ahmed Saleh"]
10
+ spec.email = ["aonemdsaleh@gmail.com"]
11
+
12
+ spec.summary = %q{A dead simple decorator for Ruby}
13
+ spec.description = %q{A dead simple decorator for Ruby}
14
+ spec.homepage = "https://github.com/aonemd/geckorate"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ end
@@ -0,0 +1,3 @@
1
+ module Geckorate
2
+ VERSION = "0.1.0"
3
+ end
data/lib/geckorate.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "geckorate/version"
2
+
3
+ module Geckorate
4
+ class Decorator < SimpleDelegator
5
+ def decorate; end
6
+
7
+ class << self
8
+ def decorate_collection(collection)
9
+ collection.map do |item|
10
+ klass = item.class
11
+ full_klass_name = klass.to_s.concat('Decorator')
12
+ decorator_klass = Class.const_get(full_klass_name)
13
+ decorator_klass.new(item).decorate
14
+ end
15
+ end
16
+
17
+ def decorate_kaminari_collection(collection)
18
+ {
19
+ page: collection.current_page,
20
+ per_page: collection.current_per_page,
21
+ total: collection.total_count,
22
+ records: decorate_collection(collection)
23
+ }
24
+ end
25
+
26
+ def decorate_will_paginate_collection(collection)
27
+ {
28
+ page: collection.current_page,
29
+ per_page: collection.per_page,
30
+ total: collection.total_entries,
31
+ records: decorate_collection(collection)
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geckorate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ahmed Saleh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-22 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: A dead simple decorator for Ruby
56
+ email:
57
+ - aonemdsaleh@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - geckorate.gemspec
71
+ - lib/geckorate.rb
72
+ - lib/geckorate/version.rb
73
+ homepage: https://github.com/aonemd/geckorate
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.7.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A dead simple decorator for Ruby
97
+ test_files: []