to_api 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/to_api.rb CHANGED
@@ -1,8 +1,10 @@
1
+ require 'active_record'
2
+
1
3
  class ActiveRecord::Base
2
4
  def to_api(*includes) #assumes all attribute types are fine
3
5
  hash = {}
4
6
  valid_includes = (self.class.reflect_on_all_associations.map(&:name).map(&:to_s) | self.valid_api_includes)
5
-
7
+
6
8
  include_hash = {}
7
9
  includes.each do |i|
8
10
  if i.kind_of?(Hash)
@@ -13,9 +15,9 @@ class ActiveRecord::Base
13
15
  include_hash[i] = []
14
16
  end
15
17
  end
16
-
18
+
17
19
  include_hash.delete_if{|k,v| !valid_includes.include?(k)}
18
-
20
+
19
21
  attributes.each do |k, v|
20
22
  attribute_includes = include_hash[k] || []
21
23
  v = v.to_api(*attribute_includes) if v.respond_to?(:to_api)
@@ -27,24 +29,24 @@ class ActiveRecord::Base
27
29
  api_obj = self.send(relation)
28
30
  hash[relation.to_s] = api_obj.respond_to?(:to_api) ? api_obj.to_api(*relation_includes) : api_obj
29
31
  end
30
-
32
+
31
33
  hash
32
34
  end
33
-
35
+
34
36
  # override in models
35
37
  def valid_api_includes
36
38
  []
37
39
  end
38
40
  end
39
41
 
40
- module Enumerable
42
+ #Sadly, Scope isn't enumerable
43
+ class ActiveRecord::NamedScope::Scope
41
44
  def to_api(*includes)
42
45
  map{|e|e.to_api(*includes)}
43
46
  end
44
47
  end
45
48
 
46
- #Sadly, Scope isn't enumerable
47
- class ActiveRecord::NamedScope::Scope
49
+ module Enumerable
48
50
  def to_api(*includes)
49
51
  map{|e|e.to_api(*includes)}
50
52
  end
@@ -81,4 +83,10 @@ class Symbol
81
83
  def to_api(*includes)
82
84
  to_s
83
85
  end
86
+ end
87
+
88
+ class NilClass
89
+ def to_api
90
+ self
91
+ end
84
92
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,4 @@
1
- require 'active_record'
2
- require File.join(File.dirname(__FILE__), "..", "lib", 'to_api')
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require 'spec'
4
+ require 'to_api'
data/spec/to_api_spec.rb CHANGED
@@ -127,6 +127,12 @@ describe '#to_api' do
127
127
  [a].to_api(['bar']).should == [:apiz]
128
128
  end
129
129
  end
130
+
131
+ describe nil do
132
+ it "returns nil" do
133
+ nil.to_api.should be_nil
134
+ end
135
+ end
130
136
 
131
137
  describe ActiveRecord::Base do
132
138
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shawn Anderson
@@ -20,23 +20,7 @@ date: 2010-12-10 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
- type: :runtime
24
- prerelease: false
25
- version_requirements: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- hash: 3
31
- segments:
32
- - 0
33
- version: "0"
34
- name: rake
35
- requirement: *id001
36
- - !ruby/object:Gem::Dependency
37
- type: :runtime
38
- prerelease: false
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
23
+ requirement: &id001 !ruby/object:Gem::Requirement
40
24
  none: false
41
25
  requirements:
42
26
  - - ">="
@@ -52,82 +36,55 @@ dependencies:
52
36
  segments:
53
37
  - 3
54
38
  version: "3"
55
- name: activerecord
56
- requirement: *id002
57
- - !ruby/object:Gem::Dependency
58
39
  type: :runtime
40
+ name: activerecord
59
41
  prerelease: false
60
- version_requirements: &id003 !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
63
- - - ">="
64
- - !ruby/object:Gem::Version
65
- hash: 3
66
- segments:
67
- - 0
68
- version: "0"
69
- name: jeweler
70
- requirement: *id003
42
+ version_requirements: *id001
71
43
  - !ruby/object:Gem::Dependency
72
- type: :development
73
- prerelease: false
74
- version_requirements: &id004 !ruby/object:Gem::Requirement
44
+ requirement: &id002 !ruby/object:Gem::Requirement
75
45
  none: false
76
46
  requirements:
77
- - - ">="
47
+ - - "="
78
48
  - !ruby/object:Gem::Version
79
- hash: 3
49
+ hash: 25
80
50
  segments:
81
- - 0
82
- version: "0"
83
- name: rspec
84
- requirement: *id004
85
- - !ruby/object:Gem::Dependency
51
+ - 1
52
+ - 3
53
+ - 1
54
+ version: 1.3.1
86
55
  type: :development
56
+ name: rspec
87
57
  prerelease: false
88
- version_requirements: &id005 !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- hash: 3
94
- segments:
95
- - 0
96
- version: "0"
97
- name: jeweler
98
- requirement: *id005
58
+ version_requirements: *id002
99
59
  - !ruby/object:Gem::Dependency
100
- type: :runtime
101
- prerelease: false
102
- version_requirements: &id006 !ruby/object:Gem::Requirement
60
+ requirement: &id003 !ruby/object:Gem::Requirement
103
61
  none: false
104
62
  requirements:
105
63
  - - ">="
106
64
  - !ruby/object:Gem::Version
107
- hash: 3
65
+ hash: 49
108
66
  segments:
109
67
  - 0
110
- version: "0"
111
- name: activerecord
112
- requirement: *id006
68
+ - 8
69
+ - 7
70
+ version: 0.8.7
71
+ type: :development
72
+ name: rake
73
+ prerelease: false
74
+ version_requirements: *id003
113
75
  description: Helper for simplifying JSON api creation.
114
76
  email: shawn42@gmail.com
115
77
  executables: []
116
78
 
117
79
  extensions: []
118
80
 
119
- extra_rdoc_files:
120
- - README
81
+ extra_rdoc_files: []
82
+
121
83
  files:
122
- - Gemfile
123
- - Gemfile.lock
124
- - README
125
- - Rakefile
126
- - VERSION
127
84
  - lib/to_api.rb
85
+ - README
128
86
  - spec/spec_helper.rb
129
87
  - spec/to_api_spec.rb
130
- - to_api.gemspec
131
88
  has_rdoc: true
132
89
  homepage: http://github.com/atomicobject/to_api
133
90
  licenses: []
data/Gemfile DELETED
@@ -1,7 +0,0 @@
1
- source "http://rubygems.org"
2
- gem "rake"
3
- gem "activerecord", ">= 2.3", "< 3"
4
- group :test do
5
- gem 'rspec', '1.3.0', :require => false
6
- end
7
- gem "jeweler"
data/Gemfile.lock DELETED
@@ -1,22 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- activerecord (2.3.5)
5
- activesupport (= 2.3.5)
6
- activesupport (2.3.5)
7
- git (1.2.5)
8
- jeweler (1.5.1)
9
- bundler (~> 1.0.0)
10
- git (>= 1.2.5)
11
- rake
12
- rake (0.8.7)
13
- rspec (1.3.0)
14
-
15
- PLATFORMS
16
- ruby
17
-
18
- DEPENDENCIES
19
- activerecord (>= 2.3, < 3)
20
- jeweler
21
- rake
22
- rspec (= 1.3.0)
data/Rakefile DELETED
@@ -1,39 +0,0 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
-
4
-
5
- begin
6
- require 'jeweler'
7
- Jeweler::Tasks.new do |gem|
8
- gem.name = "to_api"
9
- gem.rubyforge_project = "to_api"
10
- gem.summary = %Q{Helper for simplifying JSON api creation}
11
- gem.description = %Q{Helper for simplifying JSON api creation.}
12
- gem.email = "shawn42@gmail.com"
13
- gem.homepage = "http://github.com/atomicobject/to_api"
14
- gem.authors = ["Shawn Anderson","Ryan Fogle"]
15
- gem.add_development_dependency "rspec"
16
- gem.add_development_dependency "jeweler"
17
- gem.add_dependency 'activerecord'
18
- gem.test_files = FileList['{spec,test}/**/*.rb']
19
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
20
- end
21
- Jeweler::GemcutterTasks.new
22
- rescue LoadError
23
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
24
- end
25
-
26
-
27
- begin
28
- require 'spec/rake/spectask'
29
- desc "Run the code examples in spec/"
30
- Spec::Rake::SpecTask.new(:spec) do |t|
31
- t.spec_files = FileList["spec/**/*_spec.rb"]
32
- end
33
- task :default => :spec
34
-
35
- rescue LoadError
36
- puts "RSpec (or a dependency) not available. Install it with: gem install rspec"
37
- end
38
-
39
- # vim: syntax=Ruby
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.0.1
data/to_api.gemspec DELETED
@@ -1,67 +0,0 @@
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 = %q{to_api}
8
- s.version = "1.0.1"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Shawn Anderson", "Ryan Fogle"]
12
- s.date = %q{2010-12-10}
13
- s.description = %q{Helper for simplifying JSON api creation.}
14
- s.email = %q{shawn42@gmail.com}
15
- s.extra_rdoc_files = [
16
- "README"
17
- ]
18
- s.files = [
19
- "Gemfile",
20
- "Gemfile.lock",
21
- "README",
22
- "Rakefile",
23
- "VERSION",
24
- "lib/to_api.rb",
25
- "spec/spec_helper.rb",
26
- "spec/to_api_spec.rb",
27
- "to_api.gemspec"
28
- ]
29
- s.homepage = %q{http://github.com/atomicobject/to_api}
30
- s.require_paths = ["lib"]
31
- s.rubyforge_project = %q{to_api}
32
- s.rubygems_version = %q{1.3.7}
33
- s.summary = %q{Helper for simplifying JSON api creation}
34
- s.test_files = [
35
- "spec/spec_helper.rb",
36
- "spec/to_api_spec.rb"
37
- ]
38
-
39
- if s.respond_to? :specification_version then
40
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
- s.specification_version = 3
42
-
43
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
- s.add_runtime_dependency(%q<rake>, [">= 0"])
45
- s.add_runtime_dependency(%q<activerecord>, [">= 2.3", "< 3"])
46
- s.add_runtime_dependency(%q<jeweler>, [">= 0"])
47
- s.add_development_dependency(%q<rspec>, [">= 0"])
48
- s.add_development_dependency(%q<jeweler>, [">= 0"])
49
- s.add_runtime_dependency(%q<activerecord>, [">= 0"])
50
- else
51
- s.add_dependency(%q<rake>, [">= 0"])
52
- s.add_dependency(%q<activerecord>, [">= 2.3", "< 3"])
53
- s.add_dependency(%q<jeweler>, [">= 0"])
54
- s.add_dependency(%q<rspec>, [">= 0"])
55
- s.add_dependency(%q<jeweler>, [">= 0"])
56
- s.add_dependency(%q<activerecord>, [">= 0"])
57
- end
58
- else
59
- s.add_dependency(%q<rake>, [">= 0"])
60
- s.add_dependency(%q<activerecord>, [">= 2.3", "< 3"])
61
- s.add_dependency(%q<jeweler>, [">= 0"])
62
- s.add_dependency(%q<rspec>, [">= 0"])
63
- s.add_dependency(%q<jeweler>, [">= 0"])
64
- s.add_dependency(%q<activerecord>, [">= 0"])
65
- end
66
- end
67
-