simple-api-field-control 0.1.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: 8e687ba0705ce4cf319e8e418d694bfc7facff7c
4
+ data.tar.gz: 3fc6ca4df295129f21af751c21b5fdfad23cb89d
5
+ SHA512:
6
+ metadata.gz: fc139af6699b179e4ac2840a5ea67a098fc9864585748f7c3d0cdc01d9659279c709f34fae1b97ed03c22a198966a32d7176ab7319a6aeb62f3fce4ac694730e
7
+ data.tar.gz: 6101163b030952b2a326b4a7944259b8bb4e90d0e68d541d81adb3aea938620a97c21745f0416642cb22825f932623c8488625d87281a4c888306454efe7b95d
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format nested
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ gem "activesupport", ">= 3.0"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "activerecord", ">= 3.0"
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "shoulda", ">= 0"
11
+ gem "rdoc", "~> 3.12"
12
+ gem "bundler", "~> 1.0"
13
+ gem "jeweler", "1.8.7"
14
+ gem "git", "1.2.5"
15
+ gem "simplecov", ">= 0"
16
+ gem "sqlite3"
17
+ gem "json_spec", "~> 1.1.1"
18
+ gem "nokogiri", ">= 1.5"
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,107 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (4.0.0)
5
+ activesupport (= 4.0.0)
6
+ builder (~> 3.1.0)
7
+ activerecord (4.0.0)
8
+ activemodel (= 4.0.0)
9
+ activerecord-deprecated_finders (~> 1.0.2)
10
+ activesupport (= 4.0.0)
11
+ arel (~> 4.0.0)
12
+ activerecord-deprecated_finders (1.0.3)
13
+ activesupport (4.0.0)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ minitest (~> 4.2)
16
+ multi_json (~> 1.3)
17
+ thread_safe (~> 0.1)
18
+ tzinfo (~> 0.3.37)
19
+ addressable (2.3.5)
20
+ arel (4.0.0)
21
+ atomic (1.1.13)
22
+ builder (3.1.4)
23
+ diff-lcs (1.1.3)
24
+ faraday (0.8.8)
25
+ multipart-post (~> 1.2.0)
26
+ git (1.2.5)
27
+ github_api (0.10.1)
28
+ addressable
29
+ faraday (~> 0.8.1)
30
+ hashie (>= 1.2)
31
+ multi_json (~> 1.4)
32
+ nokogiri (~> 1.5.2)
33
+ oauth2
34
+ hashie (2.0.5)
35
+ highline (1.6.19)
36
+ httpauth (0.2.0)
37
+ i18n (0.6.5)
38
+ jeweler (1.8.7)
39
+ builder
40
+ bundler (~> 1.0)
41
+ git (>= 1.2.5)
42
+ github_api (= 0.10.1)
43
+ highline (>= 1.6.15)
44
+ nokogiri (= 1.5.10)
45
+ rake
46
+ rdoc
47
+ json (1.8.0)
48
+ json_spec (1.1.1)
49
+ multi_json (~> 1.0)
50
+ rspec (~> 2.0)
51
+ jwt (0.1.8)
52
+ multi_json (>= 1.5)
53
+ minitest (4.7.5)
54
+ multi_json (1.7.9)
55
+ multi_xml (0.5.5)
56
+ multipart-post (1.2.0)
57
+ nokogiri (1.5.10)
58
+ oauth2 (0.9.2)
59
+ faraday (~> 0.8)
60
+ httpauth (~> 0.2)
61
+ jwt (~> 0.1.4)
62
+ multi_json (~> 1.0)
63
+ multi_xml (~> 0.5)
64
+ rack (~> 1.2)
65
+ rack (1.5.2)
66
+ rake (10.1.0)
67
+ rdoc (3.12.2)
68
+ json (~> 1.4)
69
+ rspec (2.8.0)
70
+ rspec-core (~> 2.8.0)
71
+ rspec-expectations (~> 2.8.0)
72
+ rspec-mocks (~> 2.8.0)
73
+ rspec-core (2.8.0)
74
+ rspec-expectations (2.8.0)
75
+ diff-lcs (~> 1.1.2)
76
+ rspec-mocks (2.8.0)
77
+ shoulda (3.5.0)
78
+ shoulda-context (~> 1.0, >= 1.0.1)
79
+ shoulda-matchers (>= 1.4.1, < 3.0)
80
+ shoulda-context (1.1.5)
81
+ shoulda-matchers (2.3.0)
82
+ activesupport (>= 3.0.0)
83
+ simplecov (0.7.1)
84
+ multi_json (~> 1.0)
85
+ simplecov-html (~> 0.7.1)
86
+ simplecov-html (0.7.1)
87
+ sqlite3 (1.3.8)
88
+ thread_safe (0.1.2)
89
+ atomic
90
+ tzinfo (0.3.37)
91
+
92
+ PLATFORMS
93
+ ruby
94
+
95
+ DEPENDENCIES
96
+ activerecord (>= 3.0)
97
+ activesupport (>= 3.0)
98
+ bundler (~> 1.0)
99
+ git (= 1.2.5)
100
+ jeweler (= 1.8.7)
101
+ json_spec (~> 1.1.1)
102
+ nokogiri (>= 1.5)
103
+ rdoc (~> 3.12)
104
+ rspec (~> 2.8.0)
105
+ shoulda
106
+ simplecov
107
+ sqlite3
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Tim Krajcar
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # simple-api-field-control
2
+
3
+ Ever needed to simply include a few calculated fields/virtual attributes/custom methods in your serialization? This gem makes it simple and supports both JSON and XML rendering.
4
+
5
+ ## Usage
6
+
7
+ 1. Include `gem "simple-api-field-control"` in your Gemfile.
8
+ 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.
10
+
11
+
12
+ Example model file:
13
+
14
+ ```ruby
15
+ class Post < ActiveRecord::Base
16
+ include SimpleAPIFieldControl
17
+
18
+ def subject_length
19
+ self.subject.length
20
+ end
21
+
22
+ def subject_words
23
+ self.subject.split(' ').length
24
+ end
25
+
26
+ api_include :subject_length
27
+ api_include :subject_words
28
+ end
29
+ ```
30
+
31
+ Example output:
32
+
33
+ ```
34
+ p = Post.create!(subject: "Hello, people!")
35
+ => #<Post id: 2, subject: "Hello, people!">
36
+ puts p.to_json
37
+ => {"id":2,"subject":"Hello, people!","subject_length":14,"subject_words":2}
38
+ puts p.to_xml
39
+ => <?xml version="1.0" encoding="UTF-8"?>
40
+ <post>
41
+ <id type="integer">2</id>
42
+ <subject>Hello, people!</subject>
43
+ <subject-length type="NilClass">14</subject-length>
44
+ <subject-words type="NilClass">2</subject-words>
45
+ </post>
46
+ ```
47
+
48
+ ## Contributing to simple-api-field-control
49
+
50
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
51
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
52
+ * Fork the project.
53
+ * Start a feature/bugfix branch.
54
+ * Commit and push until you are happy with your contribution.
55
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
56
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
57
+
58
+ ## Copyright
59
+
60
+ Copyright (c) 2013 Tim Krajcar. See LICENSE.txt for
61
+ further details.
62
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "simple-api-field-control"
18
+ gem.homepage = "http://github.com/tkrajcar/simple-api-field-control"
19
+ gem.license = "MIT"
20
+ gem.summary = "Simple DSL for ActiveRecord objects to include virtual attributes/custom methods in to_json/to_xml."
21
+ gem.description = "Ever needed to simply include a few calculated fields/virtual attributes/custom methods in your serialization? This gem makes it simple and supports both JSON and XML rendering."
22
+ gem.email = "allegro@conmolto.org"
23
+ gem.authors = ["Tim Krajcar"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :test
35
+
36
+ require 'rdoc/task'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "simple-api-field-control #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
45
+
46
+ desc "Run tests with code coverage enabled"
47
+ task :coverage do
48
+ ENV["COVERAGE"] = "true"
49
+ Rake::Task["spec"].invoke
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,21 @@
1
+ require 'active_support/concern'
2
+
3
+ module SimpleAPIFieldControl
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :api_include_fields
8
+ end
9
+
10
+ def serializable_hash(options = nil)
11
+ options = { methods: self.class.api_include_fields }.merge(options || {})
12
+ super(options)
13
+ end
14
+
15
+ module ClassMethods
16
+ def api_include(field)
17
+ self.api_include_fields ||= []
18
+ self.api_include_fields << field
19
+ end
20
+ end
21
+ end
data/spec/Post.rb ADDED
@@ -0,0 +1,14 @@
1
+ class Post < ActiveRecord::Base
2
+ include SimpleAPIFieldControl
3
+
4
+ api_include :subject_length
5
+ api_include :subject_words
6
+
7
+ def subject_length
8
+ self.subject.length
9
+ end
10
+
11
+ def subject_words
12
+ self.subject.split(' ').length
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require 'Post'
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ :adapter => 'sqlite3',
5
+ :database => ':memory:'
6
+ )
7
+
8
+ ActiveRecord::Schema.define do
9
+ self.verbose = false
10
+ create_table :posts, :force => true do |t|
11
+ t.string :subject
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Simple API Field Control" do
4
+ before(:all) do
5
+ @post = Post.create!(subject: "Test Post")
6
+ end
7
+
8
+ it "should know its subject length" do
9
+ @post.subject_length.should eq 9
10
+ end
11
+
12
+ it "should know its count of subject words" do
13
+ @post.subject_words.should eq 2
14
+ end
15
+
16
+ it "should serialize to JSON with subject_length and subject_words included" do
17
+ json = @post.to_json
18
+ json.should have_json_path("subject_length")
19
+ json.should have_json_path("subject_words")
20
+ end
21
+
22
+ it "should serialize to XML with subject_length and subject_words included" do
23
+ #xml = @post.to_xml
24
+ doc = Nokogiri::XML::Document.parse(@post.to_xml)
25
+ doc.xpath('//subject-length').text.should eq('9')
26
+ doc.xpath('//subject-words').text.should eq('2')
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'json_spec'
5
+ require 'nokogiri'
6
+ require 'simple-api-field-control'
7
+ require 'simplecov'
8
+ require 'active_record'
9
+ require 'active-record-setup'
10
+
11
+ SimpleCov.start if ENV["COVERAGE"]
12
+
13
+ # Requires supporting files with custom matchers and macros, etc,
14
+ # in ./support/ and its subdirectories.
15
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
16
+
17
+ RSpec.configure do |config|
18
+ config.include JsonSpec::Helpers
19
+ end
metadata ADDED
@@ -0,0 +1,229 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-api-field-control
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Krajcar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '3.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: 2.8.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jeweler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 1.8.7
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.8.7
111
+ - !ruby/object:Gem::Dependency
112
+ name: git
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.2.5
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 1.2.5
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sqlite3
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: json_spec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ~>
158
+ - !ruby/object:Gem::Version
159
+ version: 1.1.1
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ~>
165
+ - !ruby/object:Gem::Version
166
+ version: 1.1.1
167
+ - !ruby/object:Gem::Dependency
168
+ name: nokogiri
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '1.5'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '1.5'
181
+ description: Ever needed to simply include a few calculated fields/virtual attributes/custom
182
+ methods in your serialization? This gem makes it simple and supports both JSON and
183
+ XML rendering.
184
+ email: allegro@conmolto.org
185
+ executables: []
186
+ extensions: []
187
+ extra_rdoc_files:
188
+ - LICENSE.txt
189
+ - README.md
190
+ files:
191
+ - .document
192
+ - .rspec
193
+ - Gemfile
194
+ - Gemfile.lock
195
+ - LICENSE.txt
196
+ - README.md
197
+ - Rakefile
198
+ - VERSION
199
+ - lib/simple-api-field-control.rb
200
+ - spec/Post.rb
201
+ - spec/active-record-setup.rb
202
+ - spec/simple-api-field-control_spec.rb
203
+ - spec/spec_helper.rb
204
+ homepage: http://github.com/tkrajcar/simple-api-field-control
205
+ licenses:
206
+ - MIT
207
+ metadata: {}
208
+ post_install_message:
209
+ rdoc_options: []
210
+ require_paths:
211
+ - lib
212
+ required_ruby_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - '>='
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ required_rubygems_version: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - '>='
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ requirements: []
223
+ rubyforge_project:
224
+ rubygems_version: 2.0.3
225
+ signing_key:
226
+ specification_version: 4
227
+ summary: Simple DSL for ActiveRecord objects to include virtual attributes/custom
228
+ methods in to_json/to_xml.
229
+ test_files: []