to_api 1.0.5 → 1.1.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.
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ pkg
2
+ *.swp
3
+ *.log
4
+ doc
5
+ coverage
6
+ .DS_Store
7
+ .yardoc
8
+ tmp
9
+ tags
10
+ .bundle
11
+ Gemfile.lock
12
+ *.un~
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.md CHANGED
@@ -34,7 +34,7 @@ Now just call your to\_api method. You'll get back a hash of arrays, hashes, str
34
34
 
35
35
  ### If I have to implement to\_api, what does the gem do for me?
36
36
 
37
- The gem provides to\_api for common ruby classes, allowing simple conversion to json, xml, yaml, etc. Hash and Enumerable transform all their contents, allowing your data objects to simply call to\_api on a hash of relevant attributes.
37
+ The gem provides to\_api for common Ruby classes, allowing simple conversion to json, xml, yaml, etc. Hash and Enumerable transform all their contents, allowing your data objects to simply call to\_api on a hash of relevant attributes.
38
38
 
39
39
  Fixnum, String, DateTime, Symbol, and NilClass are also provided.
40
40
 
@@ -46,4 +46,12 @@ If ActiveRecord is present, ActiveRecord::Base#to\_api transforms and returns th
46
46
 
47
47
  ### What if I need support for other common classes?
48
48
 
49
- For most classes, it only takes a couple specs and a few lines of code. Send a pull request; we'd love to take your additions.
49
+ For most classes, it only takes a couple specs and a few lines of code. Send a pull request; we'd love to take your additions.
50
+
51
+ Authors
52
+ =======
53
+
54
+ * Ryan Fogle (fogle@atomicobject.com)
55
+ * Shawn Anderson (shawn.anderson@atomicobject.com)
56
+ * © 2011 [Atomic Object](http://www.atomicobject.com/)
57
+ * More Atomic Object [open source](http://www.atomicobject.com/pages/Software+Commons) projects
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ begin
6
+ require 'spec/rake/spectask'
7
+ desc "Run the code examples in spec/"
8
+ Spec::Rake::SpecTask.new(:spec) do |t|
9
+ t.spec_files = FileList["spec/**/*_spec.rb"]
10
+ end
11
+ task :default => :spec
12
+
13
+ rescue LoadError
14
+ puts "RSpec (or a dependency) not available. Run: bundle install"
15
+ end
@@ -0,0 +1,3 @@
1
+ module ToApi
2
+ VERSION = "1.1.0"
3
+ end
data/lib/to_api.rb CHANGED
@@ -17,7 +17,7 @@ if Object.const_defined? :ActiveRecord
17
17
 
18
18
  include_hash.delete_if{|k,v| !valid_includes.include?(k.to_s)}
19
19
 
20
- attributes.each do |k, v|
20
+ to_api_attributes.each do |k, v|
21
21
  attribute_includes = include_hash[k] || []
22
22
  v = v.to_api(*attribute_includes) if v.respond_to?(:to_api)
23
23
  hash[k] = v
@@ -36,6 +36,10 @@ if Object.const_defined? :ActiveRecord
36
36
  def valid_api_includes
37
37
  []
38
38
  end
39
+
40
+ def to_api_attributes
41
+ attributes
42
+ end
39
43
  end
40
44
 
41
45
  #Sadly, Scope isn't enumerable
@@ -70,6 +74,18 @@ class DateTime
70
74
  end
71
75
  end
72
76
 
77
+ class Date
78
+ def to_api(*includes)
79
+ to_s(:db)
80
+ end
81
+ end
82
+
83
+ class Time
84
+ def to_api(*includes)
85
+ to_s(:db)
86
+ end
87
+ end
88
+
73
89
  class Hash
74
90
  def to_api(*includes)
75
91
  inject({}) do |memo, (k, v)|
data/spec/to_api_spec.rb CHANGED
@@ -7,6 +7,9 @@ class FakeRecord < ActiveRecord::Base
7
7
  named_scope :scopez, :conditions => '1=1'
8
8
  end
9
9
 
10
+ class OtherFakeRecord < FakeRecord
11
+ end
12
+
10
13
  class FakeChildRecord < FakeRecord
11
14
  end
12
15
 
@@ -83,6 +86,28 @@ describe '#to_api' do
83
86
  end
84
87
  end
85
88
 
89
+ describe Date do
90
+ it "returns db string for date" do
91
+ now = Date.parse("2001-11-28")
92
+ now.to_api.should == "2001-11-28"
93
+ end
94
+ describe "ignoring includes" do
95
+ let(:instance){ Date.today }
96
+ it_should_behave_like "ignoring includes"
97
+ end
98
+ end
99
+
100
+ describe Time do
101
+ it "returns db string for time" do
102
+ now = Time.parse("2001-11-28 04:01:59")
103
+ now.to_api.should == "2001-11-28 04:01:59"
104
+ end
105
+ describe "ingoring includes" do
106
+ let(:instance) { Time.now }
107
+ it_should_behave_like "ignoring includes"
108
+ end
109
+ end
110
+
86
111
  describe Enumerable do
87
112
  class Enumz
88
113
  attr_accessor :kid
@@ -148,6 +173,17 @@ describe '#to_api' do
148
173
  end
149
174
  end
150
175
 
176
+ describe "with to_api_attributes" do
177
+ before do
178
+ @base = OtherFakeRecord.new
179
+ @base.stub!(:to_api_attributes => {"age" => mock(:to_api => :apid_age)})
180
+ end
181
+
182
+ it "includes the to_api'd attributes" do
183
+ @base.to_api["age"].should == :apid_age
184
+ end
185
+ end
186
+
151
187
  describe "with includes" do
152
188
  before do
153
189
  @base = FakeRecord.new
data/to_api.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "to_api/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "to_api"
7
+ s.version = ToApi::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Shawn Anderson", "Ryan Fogle"]
10
+ s.email = ["shawn42@gmail.com", "github@atomicobject.com"]
11
+ s.homepage = "http://github.com/atomicobject/to_api"
12
+ s.summary = %q{Helper for simplifying JSON api creation}
13
+ s.description = %q{Helper for simplifying JSON api creation.}
14
+
15
+ s.rubyforge_project = "to_api"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "activerecord", [">= 2.3", "< 3"]
23
+ s.add_development_dependency "rspec", "= 1.3.1"
24
+ s.add_development_dependency "rake", ">= 0.8.7"
25
+ end
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: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 5
10
- version: 1.0.5
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shawn Anderson
@@ -16,10 +16,12 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-12-10 00:00:00 -05:00
19
+ date: 2011-01-11 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
+ name: activerecord
24
+ prerelease: false
23
25
  requirement: &id001 !ruby/object:Gem::Requirement
24
26
  none: false
25
27
  requirements:
@@ -37,10 +39,10 @@ dependencies:
37
39
  - 3
38
40
  version: "3"
39
41
  type: :development
40
- name: activerecord
41
- prerelease: false
42
42
  version_requirements: *id001
43
43
  - !ruby/object:Gem::Dependency
44
+ name: rspec
45
+ prerelease: false
44
46
  requirement: &id002 !ruby/object:Gem::Requirement
45
47
  none: false
46
48
  requirements:
@@ -53,10 +55,10 @@ dependencies:
53
55
  - 1
54
56
  version: 1.3.1
55
57
  type: :development
56
- name: rspec
57
- prerelease: false
58
58
  version_requirements: *id002
59
59
  - !ruby/object:Gem::Dependency
60
+ name: rake
61
+ prerelease: false
60
62
  requirement: &id003 !ruby/object:Gem::Requirement
61
63
  none: false
62
64
  requirements:
@@ -69,11 +71,11 @@ dependencies:
69
71
  - 7
70
72
  version: 0.8.7
71
73
  type: :development
72
- name: rake
73
- prerelease: false
74
74
  version_requirements: *id003
75
75
  description: Helper for simplifying JSON api creation.
76
- email: shawn42@gmail.com
76
+ email:
77
+ - shawn42@gmail.com
78
+ - github@atomicobject.com
77
79
  executables: []
78
80
 
79
81
  extensions: []
@@ -81,10 +83,15 @@ extensions: []
81
83
  extra_rdoc_files: []
82
84
 
83
85
  files:
84
- - lib/to_api.rb
86
+ - .gitignore
87
+ - Gemfile
85
88
  - README.md
89
+ - Rakefile
90
+ - lib/to_api.rb
91
+ - lib/to_api/version.rb
86
92
  - spec/spec_helper.rb
87
93
  - spec/to_api_spec.rb
94
+ - to_api.gemspec
88
95
  has_rdoc: true
89
96
  homepage: http://github.com/atomicobject/to_api
90
97
  licenses: []