simple-api-field-control 0.1.0 → 0.2.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 +4 -4
- data/.travis.yml +4 -0
- data/Gemfile +5 -1
- data/Gemfile.lock +1 -0
- data/README.md +21 -9
- data/Rakefile +3 -3
- data/VERSION +1 -1
- data/lib/simple-api-field-control.rb +7 -1
- data/simple-api-field-control.gemspec +87 -0
- data/spec/Post.rb +9 -0
- data/spec/active-record-setup.rb +2 -0
- data/spec/simple-api-field-control_spec.rb +24 -10
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 439ff260bc4fe9699898e09688a52959297e1033
|
4
|
+
data.tar.gz: 1f3313fdaa62fd02705c237cfd16bc7576ae47f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f24980accd66b1465fd64e29d56d8b90ccf0fa7bbf55919c301ce87b1276b289dd4f1706dce4f50b8bc80767d0598edde3d03a1f7db55099c8889d546304a5a0
|
7
|
+
data.tar.gz: 846dc43ae0bc55af4b7d2d535bfa79ac913bed42ebea93df59560bfe285789a2f5e5969ef5cb7c64f8fd43fd4b811b14f508fc3ae57e2047565defca87bd29b4
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -4,7 +4,7 @@ gem "activesupport", ">= 3.0"
|
|
4
4
|
|
5
5
|
# Add dependencies to develop your gem here.
|
6
6
|
# Include everything needed to run rake, tests, features, etc.
|
7
|
-
group :development do
|
7
|
+
group :development, :test do
|
8
8
|
gem "activerecord", ">= 3.0"
|
9
9
|
gem "rspec", "~> 2.8.0"
|
10
10
|
gem "shoulda", ">= 0"
|
@@ -17,3 +17,7 @@ group :development do
|
|
17
17
|
gem "json_spec", "~> 1.1.1"
|
18
18
|
gem "nokogiri", ">= 1.5"
|
19
19
|
end
|
20
|
+
|
21
|
+
group :test do
|
22
|
+
gem "rake", "10.1.0"
|
23
|
+
end
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
# simple-api-field-control
|
2
2
|
|
3
|
-
|
3
|
+
[](http://badge.fury.io/rb/simple-api-field-control)
|
4
|
+
[](https://travis-ci.org/tkrajcar/simple-api-field-control)
|
5
|
+
|
6
|
+
Ever needed to simply include or exclude a few calculated fields/virtual attributes/custom methods in your serialization? This gem makes it simple and supports both JSON and XML rendering.
|
4
7
|
|
5
8
|
## Usage
|
6
9
|
|
7
10
|
1. Include `gem "simple-api-field-control"` in your Gemfile.
|
8
11
|
2. In your model class where you want to use this module, add `include SimpleAPIFieldControl`.
|
9
|
-
3. Add one or more `api_include :name_of_method` lines to your model.
|
12
|
+
3. Add one or more `api_include :name_of_method` or `api_exclude :name_of_method` lines to your model.
|
10
13
|
|
11
14
|
|
12
15
|
Example model file:
|
@@ -15,6 +18,18 @@ Example model file:
|
|
15
18
|
class Post < ActiveRecord::Base
|
16
19
|
include SimpleAPIFieldControl
|
17
20
|
|
21
|
+
# schema:
|
22
|
+
# create_table :posts, :force => true do |t|
|
23
|
+
# t.string :subject
|
24
|
+
# t.string :api_key
|
25
|
+
# t.string :other_secret
|
26
|
+
# end
|
27
|
+
|
28
|
+
api_include :subject_length
|
29
|
+
api_include :subject_words
|
30
|
+
api_exclude :api_key
|
31
|
+
api_exclude :other_secret
|
32
|
+
|
18
33
|
def subject_length
|
19
34
|
self.subject.length
|
20
35
|
end
|
@@ -22,23 +37,20 @@ class Post < ActiveRecord::Base
|
|
22
37
|
def subject_words
|
23
38
|
self.subject.split(' ').length
|
24
39
|
end
|
25
|
-
|
26
|
-
api_include :subject_length
|
27
|
-
api_include :subject_words
|
28
40
|
end
|
29
41
|
```
|
30
42
|
|
31
43
|
Example output:
|
32
44
|
|
33
45
|
```
|
34
|
-
p = Post.create!(subject: "Hello, people!")
|
35
|
-
=> #<Post id:
|
46
|
+
p = Post.create!(subject: "Hello, people!", api_key: "foo", other_secret: "bar")
|
47
|
+
=> #<Post id: 1, subject: "Hello, people!", api_key: "foo", other_secret: "bar">
|
36
48
|
puts p.to_json
|
37
|
-
|
49
|
+
{"id":1,"subject":"Hello, people!","subject_length":14,"subject_words":2}
|
38
50
|
puts p.to_xml
|
39
51
|
=> <?xml version="1.0" encoding="UTF-8"?>
|
40
52
|
<post>
|
41
|
-
<id type="integer">
|
53
|
+
<id type="integer">1</id>
|
42
54
|
<subject>Hello, people!</subject>
|
43
55
|
<subject-length type="NilClass">14</subject-length>
|
44
56
|
<subject-words type="NilClass">2</subject-words>
|
data/Rakefile
CHANGED
@@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
|
|
17
17
|
gem.name = "simple-api-field-control"
|
18
18
|
gem.homepage = "http://github.com/tkrajcar/simple-api-field-control"
|
19
19
|
gem.license = "MIT"
|
20
|
-
gem.summary = "
|
21
|
-
gem.description = "
|
20
|
+
gem.summary = "A simple DSL for ActiveRecord objects to include/exclude virtual attributes/custom methods in to_json/to_xml."
|
21
|
+
gem.description = "A simple DSL for ActiveRecord objects to include/exclude virtual attributes/custom methods in to_json/to_xml."
|
22
22
|
gem.email = "allegro@conmolto.org"
|
23
23
|
gem.authors = ["Tim Krajcar"]
|
24
24
|
# dependencies defined in Gemfile
|
@@ -31,7 +31,7 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
31
31
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
32
|
end
|
33
33
|
|
34
|
-
task :default => :
|
34
|
+
task :default => :spec
|
35
35
|
|
36
36
|
require 'rdoc/task'
|
37
37
|
Rake::RDocTask.new do |rdoc|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -5,10 +5,11 @@ module SimpleAPIFieldControl
|
|
5
5
|
|
6
6
|
included do
|
7
7
|
class_attribute :api_include_fields
|
8
|
+
class_attribute :api_exclude_fields
|
8
9
|
end
|
9
10
|
|
10
11
|
def serializable_hash(options = nil)
|
11
|
-
options = { methods: self.class.api_include_fields }.merge(options || {})
|
12
|
+
options = { methods: self.class.api_include_fields, except: api_exclude_fields }.merge(options || {})
|
12
13
|
super(options)
|
13
14
|
end
|
14
15
|
|
@@ -17,5 +18,10 @@ module SimpleAPIFieldControl
|
|
17
18
|
self.api_include_fields ||= []
|
18
19
|
self.api_include_fields << field
|
19
20
|
end
|
21
|
+
|
22
|
+
def api_exclude(field)
|
23
|
+
self.api_exclude_fields ||= []
|
24
|
+
self.api_exclude_fields << field
|
25
|
+
end
|
20
26
|
end
|
21
27
|
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "simple-api-field-control"
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Tim Krajcar"]
|
12
|
+
s.date = "2013-08-19"
|
13
|
+
s.description = "A simple DSL for ActiveRecord objects to include/exclude virtual attributes/custom methods in to_json/to_xml."
|
14
|
+
s.email = "allegro@conmolto.org"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
".travis.yml",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/simple-api-field-control.rb",
|
30
|
+
"simple-api-field-control.gemspec",
|
31
|
+
"spec/Post.rb",
|
32
|
+
"spec/active-record-setup.rb",
|
33
|
+
"spec/simple-api-field-control_spec.rb",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = "http://github.com/tkrajcar/simple-api-field-control"
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = "2.0.3"
|
40
|
+
s.summary = "A simple DSL for ActiveRecord objects to include/exclude virtual attributes/custom methods in to_json/to_xml."
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
s.specification_version = 4
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0"])
|
47
|
+
s.add_development_dependency(%q<activerecord>, [">= 3.0"])
|
48
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
49
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
50
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
51
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
52
|
+
s.add_development_dependency(%q<jeweler>, ["= 1.8.7"])
|
53
|
+
s.add_development_dependency(%q<git>, ["= 1.2.5"])
|
54
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
56
|
+
s.add_development_dependency(%q<json_spec>, ["~> 1.1.1"])
|
57
|
+
s.add_development_dependency(%q<nokogiri>, [">= 1.5"])
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<activesupport>, [">= 3.0"])
|
60
|
+
s.add_dependency(%q<activerecord>, [">= 3.0"])
|
61
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
62
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
63
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
64
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
65
|
+
s.add_dependency(%q<jeweler>, ["= 1.8.7"])
|
66
|
+
s.add_dependency(%q<git>, ["= 1.2.5"])
|
67
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
68
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
69
|
+
s.add_dependency(%q<json_spec>, ["~> 1.1.1"])
|
70
|
+
s.add_dependency(%q<nokogiri>, [">= 1.5"])
|
71
|
+
end
|
72
|
+
else
|
73
|
+
s.add_dependency(%q<activesupport>, [">= 3.0"])
|
74
|
+
s.add_dependency(%q<activerecord>, [">= 3.0"])
|
75
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
76
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
77
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
78
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
79
|
+
s.add_dependency(%q<jeweler>, ["= 1.8.7"])
|
80
|
+
s.add_dependency(%q<git>, ["= 1.2.5"])
|
81
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
82
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
83
|
+
s.add_dependency(%q<json_spec>, ["~> 1.1.1"])
|
84
|
+
s.add_dependency(%q<nokogiri>, [">= 1.5"])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
data/spec/Post.rb
CHANGED
@@ -1,8 +1,17 @@
|
|
1
1
|
class Post < ActiveRecord::Base
|
2
2
|
include SimpleAPIFieldControl
|
3
3
|
|
4
|
+
# schema:
|
5
|
+
# create_table :posts, :force => true do |t|
|
6
|
+
# t.string :subject
|
7
|
+
# t.string :api_key
|
8
|
+
# t.string :other_secret
|
9
|
+
# end
|
10
|
+
|
4
11
|
api_include :subject_length
|
5
12
|
api_include :subject_words
|
13
|
+
api_exclude :api_key
|
14
|
+
api_exclude :other_secret
|
6
15
|
|
7
16
|
def subject_length
|
8
17
|
self.subject.length
|
data/spec/active-record-setup.rb
CHANGED
@@ -2,7 +2,10 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "Simple API Field Control" do
|
4
4
|
before(:all) do
|
5
|
-
@post = Post.create!(subject: "Test Post")
|
5
|
+
@post = Post.create!(subject: "Test Post", api_key: "foo", other_secret: "bar")
|
6
|
+
@json = @post.to_json
|
7
|
+
@xml = Nokogiri::XML::Document.parse(@post.to_xml)
|
8
|
+
|
6
9
|
end
|
7
10
|
|
8
11
|
it "should know its subject length" do
|
@@ -13,16 +16,27 @@ describe "Simple API Field Control" do
|
|
13
16
|
@post.subject_words.should eq 2
|
14
17
|
end
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
19
|
+
context "JSON serialization" do
|
20
|
+
it "should occur with subject_length and subject_words" do
|
21
|
+
@json.should have_json_path("subject_length")
|
22
|
+
@json.should have_json_path("subject_words")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should occur without api_key or other_secret" do
|
26
|
+
@json.should_not have_json_path("api_key")
|
27
|
+
@json.should_not have_json_path("other_secret")
|
28
|
+
end
|
20
29
|
end
|
21
30
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
31
|
+
context "XML serialization" do
|
32
|
+
it "should occur with subject_length and subject_words" do
|
33
|
+
@xml.xpath('//subject-length').text.should eq('9')
|
34
|
+
@xml.xpath('//subject-words').text.should eq('2')
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should occur without api_key or other_secret" do
|
38
|
+
@xml.xpath('//api-key').text.should eq("")
|
39
|
+
@xml.xpath('//other-secret').text.should eq("")
|
40
|
+
end
|
27
41
|
end
|
28
42
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-api-field-control
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Krajcar
|
@@ -178,9 +178,8 @@ dependencies:
|
|
178
178
|
- - '>='
|
179
179
|
- !ruby/object:Gem::Version
|
180
180
|
version: '1.5'
|
181
|
-
description:
|
182
|
-
methods in
|
183
|
-
XML rendering.
|
181
|
+
description: A simple DSL for ActiveRecord objects to include/exclude virtual attributes/custom
|
182
|
+
methods in to_json/to_xml.
|
184
183
|
email: allegro@conmolto.org
|
185
184
|
executables: []
|
186
185
|
extensions: []
|
@@ -190,6 +189,7 @@ extra_rdoc_files:
|
|
190
189
|
files:
|
191
190
|
- .document
|
192
191
|
- .rspec
|
192
|
+
- .travis.yml
|
193
193
|
- Gemfile
|
194
194
|
- Gemfile.lock
|
195
195
|
- LICENSE.txt
|
@@ -197,6 +197,7 @@ files:
|
|
197
197
|
- Rakefile
|
198
198
|
- VERSION
|
199
199
|
- lib/simple-api-field-control.rb
|
200
|
+
- simple-api-field-control.gemspec
|
200
201
|
- spec/Post.rb
|
201
202
|
- spec/active-record-setup.rb
|
202
203
|
- spec/simple-api-field-control_spec.rb
|
@@ -224,6 +225,6 @@ rubyforge_project:
|
|
224
225
|
rubygems_version: 2.0.3
|
225
226
|
signing_key:
|
226
227
|
specification_version: 4
|
227
|
-
summary:
|
228
|
+
summary: A simple DSL for ActiveRecord objects to include/exclude virtual attributes/custom
|
228
229
|
methods in to_json/to_xml.
|
229
230
|
test_files: []
|