lazy_resource 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -7,6 +7,7 @@ gemspec
7
7
  gem 'pry'
8
8
  gem 'debugger'
9
9
  gem 'rake'
10
+ gem 'twitter_cldr'
10
11
 
11
12
  gem 'rspec'
12
13
  gem 'simplecov', :require => false
data/README.md CHANGED
@@ -109,6 +109,12 @@ the version number. If you want to maintain your own version, go for it,
109
109
  but put it in a separate commit so I can ignore it when I merge the rest
110
110
  of your stuff in.
111
111
 
112
+ ## It's alpha, yo
113
+
114
+ I'm not using this in production anywhere (yet), so use at your own
115
+ risk. It's got a pretty comprehensive test suite, but I'm sure there
116
+ are at least a few bugs. If you find one, [report it](https://github.com/ahlatimer/lazy_resource/issues).
117
+
112
118
  ## Recognition
113
119
 
114
120
  Thanks to:
@@ -193,7 +193,19 @@ module LazyResource
193
193
  elsif attribute_type.include?(LazyResource::Resource)
194
194
  value = value.as_json
195
195
  elsif attribute_type == DateTime
196
- value = value.to_s
196
+ if options[:include_time_ago_in_words] && defined?(TwitterCldr)
197
+ hash[:"#{attribute_name}_in_words"] = (DateTime.now - (DateTime.now - value).to_f).localize.ago.to_s
198
+ end
199
+
200
+ if options[:strftime]
201
+ value = self.send(attribute_name).strftime(options[:strftime])
202
+ end
203
+
204
+ if options[attribute_name.to_sym] && options[attribute_name.to_sym][:strftime]
205
+ value = self.send(attribute_name).strftime(options[attribute_name.to_sym][:strftime])
206
+ end
207
+
208
+ value = value.to_s unless value.is_a?(String)
197
209
  end
198
210
 
199
211
  hash[attribute_name.to_sym] = value
@@ -1,3 +1,3 @@
1
1
  module LazyResource
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -4,6 +4,7 @@ class User
4
4
  attribute :id, Fixnum
5
5
  attribute :name, String
6
6
  attribute :created_at, DateTime
7
+ attribute :updated_at, DateTime
7
8
  attribute :post, Post
8
9
  attribute :comments, [Comment]
9
10
  end
@@ -227,6 +227,29 @@ describe LazyResource::Resource do
227
227
  user = User.load(params)
228
228
  user.as_json.should == params
229
229
  end
230
+
231
+ it 'converts dates to strings using the strftime parameter' do
232
+ params = { :id => 1, :created_at => DateTime.now }
233
+ user = User.load(params)
234
+ params[:created_at] = params[:created_at].strftime("%D")
235
+ user.as_json(:strftime => '%D').should == params
236
+ end
237
+
238
+ it 'converts dates to strings for the passed parameter' do
239
+ params = { :id => 1, :created_at => DateTime.now.to_s, :updated_at => DateTime.now }
240
+ user = User.load(params)
241
+ params[:updated_at] = params[:updated_at].strftime("%D")
242
+ user.as_json(:updated_at => { :strftime => "%D" }).should == params
243
+ end
244
+
245
+ it 'adds a _in_words for datetimes when include_time_ago_in_words option is set' do
246
+ require 'twitter_cldr'
247
+
248
+ params = { :id => 1, :created_at => DateTime.now.to_s }
249
+ user = User.load(params)
250
+ params[:created_at_in_words] = "1 second ago"
251
+ user.as_json(:include_time_ago_in_words => true).should == params
252
+ end
230
253
  end
231
254
 
232
255
  describe '.find' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -132,7 +132,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  segments:
134
134
  - 0
135
- hash: 1026372521155159703
135
+ hash: -2886530034829089377
136
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  none: false
138
138
  requirements:
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  version: '0'
142
142
  segments:
143
143
  - 0
144
- hash: 1026372521155159703
144
+ hash: -2886530034829089377
145
145
  requirements: []
146
146
  rubyforge_project:
147
147
  rubygems_version: 1.8.24