extendible 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +1 -0
- data/extendible.gemspec +24 -0
- data/lib/extendible/version.rb +3 -0
- data/lib/extendible.rb +51 -0
- data/spec/extendible_spec.rb +5 -0
- data/spec/spec_helper.rb +4 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0083e6b235bb54b3ea7514edc43f243f01a9f34e
|
4
|
+
data.tar.gz: 3fd7a8872549382ed62109cf908ce30c057cf5a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e99dffdda5cf1f80320cc781541d2cd2cb17db1579290b04e70b72029447344e026408d4d03c18e9985942aa2aef220cf7779944aed772a3bd32dc967654d5c2
|
7
|
+
data.tar.gz: 726fe2a50080d2ae9023ae6bbca7c79998284dbd74f0731b8b4e783ad096c61967236bbf6fb917b2230a365f63d9250364899aaf4dfc39fa39540a9f23c520be
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Robert Falkén
|
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,31 @@
|
|
1
|
+
# Extendible
|
2
|
+
|
3
|
+
Make referenced objects in your API only include ID attribute by default, and extend them using URL parameters.
|
4
|
+
|
5
|
+
e.g. `https://resource-endpoint?extend=author.name,author.email` to extend your referenced author with its name and email attributes.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'extendible'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install extendible
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/extendible.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'extendible/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "extendible"
|
8
|
+
spec.version = Extendible::VERSION
|
9
|
+
spec.authors = ["Robert Falkén"]
|
10
|
+
spec.email = ["f@lken.se"]
|
11
|
+
spec.description = %q{Makes ActiveModel::Serializer objects extendible by query parameters}
|
12
|
+
spec.summary = %q{Serializer extension}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/extendible.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Extendible
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval { extend ClassMethods }
|
5
|
+
end
|
6
|
+
|
7
|
+
def extended_objects
|
8
|
+
return @extended_objects unless @extended_objects.nil?
|
9
|
+
# empty array if no params[:extend] is provided
|
10
|
+
return [] if scope[:extend].nil?
|
11
|
+
all = {}
|
12
|
+
# iterate comma separated list of objects to extend
|
13
|
+
scope[:extend].split(',').map(&:strip).each do |object_name|
|
14
|
+
if object_name.include?('.') # dot found, split into object and attribute
|
15
|
+
obj, attr = object_name.split('.')
|
16
|
+
if all.has_key?(obj.to_sym) # object already has attributes specified, add attribute to array
|
17
|
+
all[obj.to_sym] << attr
|
18
|
+
else # first attribute for array, initialize array with attribute
|
19
|
+
all[obj.to_sym] = [attr]
|
20
|
+
end
|
21
|
+
else # no dot (.) found, extend with all attributes
|
22
|
+
all[object_name.to_sym] = nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
@extended_objects = all
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def extendible(*children)
|
31
|
+
# iterate through all obects that are supposed to be extendible
|
32
|
+
children.each do |child_sym|
|
33
|
+
attributes child_sym
|
34
|
+
define_method child_sym do
|
35
|
+
# nil if child is nonexistent
|
36
|
+
return nil if object.try(child_sym).nil?
|
37
|
+
if extended_objects.has_key?(child_sym) # params[:extend] includes child, extend attributes
|
38
|
+
if extended_objects[child_sym].nil? # no attributes are specified, extend with all
|
39
|
+
object.try(child_sym)
|
40
|
+
else # only extend with attributes provided with dot notation
|
41
|
+
object.try(child_sym).attributes.extract!(*extended_objects[child_sym].map(&:to_s))
|
42
|
+
end
|
43
|
+
else # child is not in params[:extend], only include id
|
44
|
+
object.try(child_sym).attributes.extract!('id')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: extendible
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Falkén
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-27 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Makes ActiveModel::Serializer objects extendible by query parameters
|
56
|
+
email:
|
57
|
+
- f@lken.se
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- extendible.gemspec
|
68
|
+
- lib/extendible.rb
|
69
|
+
- lib/extendible/version.rb
|
70
|
+
- spec/extendible_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
homepage: ''
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Serializer extension
|
96
|
+
test_files:
|
97
|
+
- spec/extendible_spec.rb
|
98
|
+
- spec/spec_helper.rb
|