representable-expandable 1.0.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: 237b0654dd7f31f1b8a6b0aa475606c1fce444cd
4
+ data.tar.gz: 2e7c929251b32166a6958914a08ae75d7f7bdc45
5
+ SHA512:
6
+ metadata.gz: ec969453f9e3002aa252677f6104f0b98196ed7b203de3f379a96eb964f2acda0a70bb89b0d7b8ea15113a92786e01b39fde279760e4300599a9dd197301bde8
7
+ data.tar.gz: 965305b8f5c745ca598ec6e681ad636ed5602ed53ee3d5f350e14bdb4d2b1a420ee0a5f269dfffe8c2dc4473818c9fde7e192553d99c5a62dde1f5b7b317cb14
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-19mode # JRuby in 1.9 mode
5
+ - rbx-2.1.1
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
3
+ gem 'rake'
4
+
5
+ gem 'rspec'
6
+ gem 'representable'
7
+ gem 'multi_json'
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ representable-expandable (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ declarative (0.0.5)
10
+ uber (>= 0.0.15)
11
+ diff-lcs (1.2.5)
12
+ multi_json (1.11.2)
13
+ rake (0.9.6)
14
+ representable (2.4.0)
15
+ declarative (~> 0.0.5)
16
+ uber (~> 0.0.15)
17
+ rspec (3.1.0)
18
+ rspec-core (~> 3.1.0)
19
+ rspec-expectations (~> 3.1.0)
20
+ rspec-mocks (~> 3.1.0)
21
+ rspec-core (3.1.7)
22
+ rspec-support (~> 3.1.0)
23
+ rspec-expectations (3.1.2)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.1.0)
26
+ rspec-mocks (3.1.3)
27
+ rspec-support (~> 3.1.0)
28
+ rspec-support (3.1.2)
29
+ uber (0.0.15)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ multi_json
36
+ rake
37
+ representable
38
+ representable-expandable!
39
+ rspec
40
+
41
+ BUNDLED WITH
42
+ 1.10.6
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ashkan
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Representable Expandable Extension [![Build Status](https://travis-ci.org/ashkan18/representable-expandable.svg?branch=master)](https://travis-ci.org/ashkan18/representable-expandable)
2
+
3
+ This gem is an extension on top of [Representable](https://github.com/apotonick/representable) which allows us to by default exclude properties/collections from representables and explicitly include them by setting right option on Representable.
4
+
5
+ ## Installation
6
+ Add following line to your Gemfile
7
+
8
+ ```
9
+ gem 'representable-expandable'
10
+ ```
11
+
12
+ ## Usage
13
+ You can make a collection `Expandable` by includeing `Representable::Expandable` in your `Representable` and set `expandable: true` for your expandable properties/colloections. Here is a sample:
14
+
15
+ ```ruby
16
+ module MessageRepresenter
17
+ include Roar::JSON
18
+ include Expandable
19
+
20
+ property :id
21
+ property :subject
22
+ property :body, expandable: true
23
+ collection :attachments, extend: AttachmentRepresenter, class: Attachment, expandable: true
24
+ end
25
+ ```
26
+
27
+ In the above example `attachments` are expandable which means by default they won't get rendered in `MessageRepresenter`. In order to explicitly add them to representer we need to set `expand` options of representer using `to_json` method:
28
+
29
+ ```ruby
30
+ message = Message.new(id: 'test', subject: 'test subject', body: 'test body', attachments: ['1', '2'])
31
+ message.extend(MessageRepresenter).to_json(expand: ['attachments'])
32
+ ```
33
+ Note that `expand` gets an array as input.
34
+
35
+
36
+ ##Problems?
37
+
38
+ Please create a Github issue once you find any issue or had any requests.
39
+
40
+ ##Thanks
41
+ Special thanks to [@joeyAghion](https://github.com/joeyAghion)
42
+
43
+ ##License
44
+
45
+ Please see [LICENSE](LICENSE) for licensing details.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler/gem_tasks'
3
+
4
+ Bundler.setup :default, :development
5
+
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+
9
+ RSpec::Core::RakeTask.new(:spec) do |spec|
10
+ spec.pattern = FileList['spec/**/*_spec.rb']
11
+ end
12
+
13
+ task default: [:spec]
@@ -0,0 +1,25 @@
1
+ module Representable
2
+ module Expandable
3
+ def self.included(base)
4
+ base.class_eval do
5
+ def self.collection(name, options = {}, &block)
6
+ add_expandable(name, options)
7
+ super
8
+ end
9
+
10
+ def self.property(name, options = {}, &block)
11
+ add_expandable(name, options)
12
+ super
13
+ end
14
+
15
+ def self.add_expandable(name, options = {})
16
+ if options.delete :expandable
17
+ # if collection already had if, and it with expandable if condition
18
+ existing_if = options[:if] || ->(_opts) { true }
19
+ options[:if] = ->(opts) { opts[:expand] && opts[:expand].include?(name.to_s) && existing_if.call(opts) }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Representable
2
+ module Expandable
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'representable/expandable/version'
2
+ require 'representable/expandable'
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+ require 'representable/expandable/version'
3
+ Gem::Specification.new do |s|
4
+ s.name = 'representable-expandable'
5
+ s.version = Representable::Expandable::VERSION
6
+ s.authors = ['Ashkan Nasseri']
7
+ s.email = 'ashkan.nasseri@gmail.com'
8
+ s.platform = Gem::Platform::RUBY
9
+ s.required_rubygems_version = '>= 1.3.6'
10
+ s.files = `git ls-files`.split("\n")
11
+ s.require_paths = ['lib']
12
+ s.homepage = 'https://github.com/ashkan18/representable-expandable'
13
+ s.licenses = ['MIT']
14
+ s.summary = 'Expandable extension for Representable'
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Representable::Expandable do
4
+ it 'has a version' do
5
+ expect(Representable::Expandable::VERSION).not_to be_nil
6
+ end
7
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Representable::Expandable do
4
+ before do
5
+ class Song < OpenStruct
6
+ end
7
+ module SongExpandable
8
+ include Representable::JSON
9
+ include Representable::Expandable
10
+ property :name, expandable: true
11
+ collection :genres, expandable: true
12
+ end
13
+ @song = Song.new(name: 'Summertime', genres: ['jazz', 'classic'])
14
+ end
15
+ context 'property' do
16
+ it 'does not include name when not expanded' do
17
+ response = @song.extend(SongExpandable).to_json
18
+ expect(response).not_to include 'name'
19
+ end
20
+
21
+ it 'includes name when expanded' do
22
+ response = @song.extend(SongExpandable).to_json(expand: ['name'])
23
+ expect(response).to include 'name'
24
+ end
25
+ end
26
+
27
+ context 'collection' do
28
+ it 'does not include genres when not expanded' do
29
+ response = @song.extend(SongExpandable).to_json
30
+ expect(response).not_to include 'genres'
31
+ end
32
+
33
+ it 'includes name when expanded' do
34
+ response = @song.extend(SongExpandable).to_json(expand: ['genres'])
35
+ expect(response).to include 'genres'
36
+ end
37
+ end
38
+
39
+ context 'expand list' do
40
+ it 'includes genres and names when both' do
41
+ response = @song.extend(SongExpandable).to_json(expand: ['genres', 'name'])
42
+ expect(response).to include 'genres'
43
+ expect(response).to include 'name'
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
4
+ require 'rspec'
5
+ require 'representable/json'
6
+ require 'representable_expandable'
7
+ require 'ostruct'
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: representable-expandable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ashkan Nasseri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: ashkan.nasseri@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - .gitignore
20
+ - .rspec
21
+ - .travis.yml
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/representable/expandable.rb
28
+ - lib/representable/expandable/version.rb
29
+ - lib/representable_expandable.rb
30
+ - representable_expandable.gemspec
31
+ - spec/representable/expandable/version_spec.rb
32
+ - spec/representable/expandable_spec.rb
33
+ - spec/spec_helper.rb
34
+ homepage: https://github.com/ashkan18/representable-expandable
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: 1.3.6
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.6
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Expandable extension for Representable
58
+ test_files: []