formatted_length 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +3 -0
  4. data/Gemfile +4 -0
  5. data/README.md +1 -18
  6. data/Rakefile +4 -36
  7. data/formatted_length.gemspec +30 -0
  8. data/lib/formatted_length.rb +4 -4
  9. data/lib/formatted_length/formatted_length.rb +21 -0
  10. data/lib/formatted_length/model_additions.rb +12 -0
  11. data/lib/formatted_length/railtie.rb +9 -0
  12. data/lib/formatted_length/version.rb +1 -1
  13. data/spec/formatted_length/formatted_length_spec.rb +36 -0
  14. data/spec/formatted_length/model_additions_spec.rb +39 -0
  15. data/spec/spec_helper.rb +2 -0
  16. metadata +51 -115
  17. data/lib/formatted_length/acts_as_formatted_length.rb +0 -38
  18. data/test/acts_as_formatted_length_test.rb +0 -61
  19. data/test/dummy/Rakefile +0 -7
  20. data/test/dummy/app/assets/javascripts/application.js +0 -9
  21. data/test/dummy/app/assets/stylesheets/application.css +0 -7
  22. data/test/dummy/app/controllers/application_controller.rb +0 -3
  23. data/test/dummy/app/helpers/application_helper.rb +0 -2
  24. data/test/dummy/app/models/model_with_default_length_field.rb +0 -3
  25. data/test/dummy/app/models/model_with_length_field_and_setter.rb +0 -3
  26. data/test/dummy/app/models/model_with_non_default_length_field.rb +0 -3
  27. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  28. data/test/dummy/config.ru +0 -4
  29. data/test/dummy/config/application.rb +0 -45
  30. data/test/dummy/config/boot.rb +0 -10
  31. data/test/dummy/config/database.yml +0 -25
  32. data/test/dummy/config/environment.rb +0 -5
  33. data/test/dummy/config/environments/development.rb +0 -30
  34. data/test/dummy/config/environments/production.rb +0 -60
  35. data/test/dummy/config/environments/test.rb +0 -42
  36. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  37. data/test/dummy/config/initializers/inflections.rb +0 -10
  38. data/test/dummy/config/initializers/mime_types.rb +0 -5
  39. data/test/dummy/config/initializers/secret_token.rb +0 -7
  40. data/test/dummy/config/initializers/session_store.rb +0 -8
  41. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  42. data/test/dummy/config/locales/en.yml +0 -5
  43. data/test/dummy/config/routes.rb +0 -58
  44. data/test/dummy/db/development.sqlite3 +0 -0
  45. data/test/dummy/db/migrate/20111026123348_create_model_with_default_length_fields.rb +0 -9
  46. data/test/dummy/db/migrate/20111026123424_create_model_with_non_default_length_fields.rb +0 -9
  47. data/test/dummy/db/migrate/20111026144756_create_model_with_length_field_and_setters.rb +0 -9
  48. data/test/dummy/db/schema.rb +0 -34
  49. data/test/dummy/db/test.sqlite3 +0 -0
  50. data/test/dummy/log/development.log +0 -33
  51. data/test/dummy/log/test.log +0 -2
  52. data/test/dummy/public/404.html +0 -26
  53. data/test/dummy/public/422.html +0 -26
  54. data/test/dummy/public/500.html +0 -26
  55. data/test/dummy/public/favicon.ico +0 -0
  56. data/test/dummy/script/rails +0 -6
  57. data/test/dummy/test/fixtures/model_with_default_length_fields.yml +0 -7
  58. data/test/dummy/test/fixtures/model_with_length_field_and_setters.yml +0 -7
  59. data/test/dummy/test/fixtures/model_with_non_default_length_fields.yml +0 -7
  60. data/test/dummy/test/unit/model_with_default_length_field_test.rb +0 -7
  61. data/test/dummy/test/unit/model_with_length_field_and_setter_test.rb +0 -7
  62. data/test/dummy/test/unit/model_with_non_default_length_field_test.rb +0 -7
  63. data/test/formatted_length_test.rb +0 -7
  64. data/test/test_helper.rb +0 -10
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format Fuubar
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in formatted_length.gemspec
4
+ gemspec
data/README.md CHANGED
@@ -26,25 +26,8 @@ acts_as_formatted_length :foobar
26
26
  end
27
27
 
28
28
  #now usage
29
- example = YourClass.create(length: 70)
29
+ example = YourClass.create(foobar: 70)
30
30
  puts example.formatted_length # prints out 01:10
31
- ```
32
-
33
- If you require a setter, pass :setter => true.
34
- The created setter accpets strings in following manner:
35
- * If is integer (no ':' in string), the value is treated as length in seconds.
36
- * Else, value is deformatted into correct amount of seconds (i.e. "1:01:06" becomes 3666)
37
-
38
- ```ruby
39
- #sample class definition
40
- class YourClass
41
- #implementation etc...
42
- acts_as_formatted_length :foobar, setter: true
43
- end
44
-
45
- #now usage
46
- example = YourClass.create(length: 70)
47
- example.formatted_length # = 01:10
48
31
  example.formatted_length = "1:01:06"
49
32
  puts example.foobar # prints out 3666
50
33
  ```
data/Rakefile CHANGED
@@ -1,37 +1,5 @@
1
- #!/usr/bin/env rake
2
- begin
3
- require 'bundler/setup'
4
- rescue LoadError
5
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
- end
7
- begin
8
- require 'rdoc/task'
9
- rescue LoadError
10
- require 'rdoc/rdoc'
11
- require 'rake/rdoctask'
12
- RDoc::Task = Rake::RDocTask
13
- end
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
14
3
 
15
- RDoc::Task.new(:rdoc) do |rdoc|
16
- rdoc.rdoc_dir = 'rdoc'
17
- rdoc.title = 'FormattedLength'
18
- rdoc.options << '--line-numbers'
19
- rdoc.rdoc_files.include('README.rdoc')
20
- rdoc.rdoc_files.include('lib/**/*.rb')
21
- end
22
-
23
-
24
-
25
- Bundler::GemHelper.install_tasks
26
-
27
- require 'rake/testtask'
28
-
29
- Rake::TestTask.new(:test) do |t|
30
- t.libs << 'lib'
31
- t.libs << 'test'
32
- t.pattern = 'test/**/*_test.rb'
33
- t.verbose = false
34
- end
35
-
36
-
37
- task :default => :test
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ # Maintain your gem's version:
5
+ require "formatted_length/version"
6
+
7
+ # Describe your gem and declare its dependencies:
8
+ Gem::Specification.new do |s|
9
+ s.name = "formatted_length"
10
+ s.version = FormattedLength::VERSION
11
+ s.authors = ["Meron Bareket"]
12
+ s.email = ["mrnbrkt@gmail.com"]
13
+ s.homepage = "http://github.com/mrnbrkt"
14
+ s.summary = "Adds ability to present integer amount of seconds as text"
15
+ s.description = "Adds formatted_length function to classes that acts_like_formatted_length, converting a length represented in seconds to a string"
16
+
17
+ s.rubyforge_project = "formatted_length"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+
24
+ # specify any dependencies here; for example:
25
+ s.add_development_dependency "rake"
26
+ s.add_development_dependency "rspec"
27
+ s.add_development_dependency "supermodel"
28
+ s.add_development_dependency "fuubar"
29
+ # s.add_runtime_dependency "rest-client"
30
+ end
@@ -1,4 +1,4 @@
1
- require 'formatted_length/acts_as_formatted_length'
2
-
3
- module FormattedLength
4
- end
1
+ require "formatted_length/version"
2
+ require "formatted_length/formatted_length"
3
+ require "formatted_length/model_additions"
4
+ require "formatted_length/railtie" if defined? Rails
@@ -0,0 +1,21 @@
1
+ module FormattedLength
2
+ def self.format_to_s(value)
3
+ value = value || 0
4
+ hours = value / 3600
5
+ minutes = (value / 60) % 60
6
+ seconds = value % 60
7
+ result = "%02d" % minutes + ":%02d" % seconds
8
+ result = "#{hours}:#{result}" if hours > 0
9
+ result
10
+ end
11
+
12
+ def self.format_to_i(value)
13
+ result = 0
14
+ unless value.nil?
15
+ if (match = /((\d+):)?(\d+):(\d+)/.match(value))
16
+ result = match[2].to_i * 3600 + match[3].to_i * 60 + match[4].to_i
17
+ end
18
+ end
19
+ result
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module FormattedLength
2
+ module ModelAdditions
3
+ def acts_as_formatted_length(field = :length)
4
+ define_method :formatted_length do
5
+ FormattedLength.format_to_s(send(field))
6
+ end
7
+ define_method :formatted_length= do |value|
8
+ send("#{field}=".to_sym, FormattedLength.format_to_i(value))
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module FormattedLength
2
+ class Railtie < Rails::Railtie
3
+ initializer 'formatted_length.model_additions' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend ModelAdditions
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module FormattedLength
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ examples = [
3
+ {seconds: 5, as_string: "00:05"},
4
+ {seconds: 65, as_string: "01:05"},
5
+ {seconds: 555, as_string: "09:15"},
6
+ {seconds: 1234, as_string: "20:34"},
7
+ {seconds: 3599, as_string: "59:59"},
8
+ {seconds: 3600, as_string: "1:00:00"},
9
+ {seconds: 0, as_string: "00:00"}
10
+ ]
11
+ describe FormattedLength do
12
+ describe ".format_to_s" do
13
+ it "converts length in seconds to string representation" do
14
+ examples.each do |hash|
15
+ FormattedLength.format_to_s(hash[:seconds]).should == hash[:as_string]
16
+ end
17
+ end
18
+
19
+ it "converts nil to 00:00" do
20
+ FormattedLength.format_to_s(nil).should == "00:00"
21
+ end
22
+ end
23
+
24
+ describe ".format_to_i" do
25
+ it "converts string representation of length to seconds" do
26
+ examples.each do |hash|
27
+ FormattedLength.format_to_i(hash[:as_string]).should == hash[:seconds]
28
+ end
29
+ end
30
+
31
+ it "handles empty string and nil" do
32
+ FormattedLength.format_to_i(nil).should == 0
33
+ FormattedLength.format_to_i("").should == 0
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ class ClassFormattedWithDefaultField < SuperModel::Base
4
+ extend FormattedLength::ModelAdditions
5
+ acts_as_formatted_length
6
+ attr_accessor :length
7
+ end
8
+
9
+ class ClassFormattedWithNonDefaultField < SuperModel::Base
10
+ extend FormattedLength::ModelAdditions
11
+ acts_as_formatted_length :how_long
12
+ attr_accessor :how_long
13
+ end
14
+
15
+ describe FormattedLength::ModelAdditions do
16
+ describe "#formatted_length" do
17
+ it "converts integer value from length by default" do
18
+ ClassFormattedWithDefaultField.create!(length: 30).formatted_length.should == "00:30"
19
+ ClassFormattedWithDefaultField.create!().formatted_length.should == "00:00"
20
+ end
21
+
22
+ it "converts integer value from specific field if defined" do
23
+ ClassFormattedWithNonDefaultField.create!(how_long: 30).formatted_length.should == "00:30"
24
+ ClassFormattedWithNonDefaultField.create!().formatted_length.should == "00:00"
25
+ end
26
+ end
27
+
28
+ describe "#formatted_length=" do
29
+ it "converts string value and saves it in default field" do
30
+ ClassFormattedWithDefaultField.create!(formatted_length: "1:00:30").length.should == 3630
31
+ ClassFormattedWithDefaultField.create!(formatted_length: "00:30").length.should == 30
32
+ end
33
+
34
+ it "converts string value and saves it in selected field" do
35
+ ClassFormattedWithNonDefaultField.create!(formatted_length: "1:00:30").how_long.should == 3630
36
+ ClassFormattedWithNonDefaultField.create!(formatted_length: "00:30").how_long.should == 30
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,2 @@
1
+ require 'formatted_length'
2
+ require 'supermodel'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatted_length
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,19 +12,41 @@ cert_chain: []
12
12
  date: 2012-02-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rails
16
- requirement: &70239312327800 !ruby/object:Gem::Requirement
15
+ name: rake
16
+ requirement: &70259048386660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 3.1.0
22
- type: :runtime
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70259048386660
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70259048386220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70259048386220
36
+ - !ruby/object:Gem::Dependency
37
+ name: supermodel
38
+ requirement: &70259048385760 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
23
45
  prerelease: false
24
- version_requirements: *70239312327800
46
+ version_requirements: *70259048385760
25
47
  - !ruby/object:Gem::Dependency
26
- name: sqlite3
27
- requirement: &70239312326880 !ruby/object:Gem::Requirement
48
+ name: fuubar
49
+ requirement: &70259048385260 !ruby/object:Gem::Requirement
28
50
  none: false
29
51
  requirements:
30
52
  - - ! '>='
@@ -32,7 +54,7 @@ dependencies:
32
54
  version: '0'
33
55
  type: :development
34
56
  prerelease: false
35
- version_requirements: *70239312326880
57
+ version_requirements: *70259048385260
36
58
  description: Adds formatted_length function to classes that acts_like_formatted_length,
37
59
  converting a length represented in seconds to a string
38
60
  email:
@@ -41,60 +63,24 @@ executables: []
41
63
  extensions: []
42
64
  extra_rdoc_files: []
43
65
  files:
44
- - lib/formatted_length/acts_as_formatted_length.rb
45
- - lib/formatted_length/version.rb
46
- - lib/formatted_length.rb
47
- - lib/tasks/formatted_length_tasks.rake
66
+ - .gitignore
67
+ - .rspec
68
+ - .travis.yml
69
+ - Gemfile
70
+ - Gemfile.lock
48
71
  - MIT-LICENSE
49
- - Rakefile
50
72
  - README.md
51
- - test/acts_as_formatted_length_test.rb
52
- - test/dummy/app/assets/javascripts/application.js
53
- - test/dummy/app/assets/stylesheets/application.css
54
- - test/dummy/app/controllers/application_controller.rb
55
- - test/dummy/app/helpers/application_helper.rb
56
- - test/dummy/app/models/model_with_default_length_field.rb
57
- - test/dummy/app/models/model_with_length_field_and_setter.rb
58
- - test/dummy/app/models/model_with_non_default_length_field.rb
59
- - test/dummy/app/views/layouts/application.html.erb
60
- - test/dummy/config/application.rb
61
- - test/dummy/config/boot.rb
62
- - test/dummy/config/database.yml
63
- - test/dummy/config/environment.rb
64
- - test/dummy/config/environments/development.rb
65
- - test/dummy/config/environments/production.rb
66
- - test/dummy/config/environments/test.rb
67
- - test/dummy/config/initializers/backtrace_silencers.rb
68
- - test/dummy/config/initializers/inflections.rb
69
- - test/dummy/config/initializers/mime_types.rb
70
- - test/dummy/config/initializers/secret_token.rb
71
- - test/dummy/config/initializers/session_store.rb
72
- - test/dummy/config/initializers/wrap_parameters.rb
73
- - test/dummy/config/locales/en.yml
74
- - test/dummy/config/routes.rb
75
- - test/dummy/config.ru
76
- - test/dummy/db/development.sqlite3
77
- - test/dummy/db/migrate/20111026123348_create_model_with_default_length_fields.rb
78
- - test/dummy/db/migrate/20111026123424_create_model_with_non_default_length_fields.rb
79
- - test/dummy/db/migrate/20111026144756_create_model_with_length_field_and_setters.rb
80
- - test/dummy/db/schema.rb
81
- - test/dummy/db/test.sqlite3
82
- - test/dummy/log/development.log
83
- - test/dummy/log/test.log
84
- - test/dummy/public/404.html
85
- - test/dummy/public/422.html
86
- - test/dummy/public/500.html
87
- - test/dummy/public/favicon.ico
88
- - test/dummy/Rakefile
89
- - test/dummy/script/rails
90
- - test/dummy/test/fixtures/model_with_default_length_fields.yml
91
- - test/dummy/test/fixtures/model_with_length_field_and_setters.yml
92
- - test/dummy/test/fixtures/model_with_non_default_length_fields.yml
93
- - test/dummy/test/unit/model_with_default_length_field_test.rb
94
- - test/dummy/test/unit/model_with_length_field_and_setter_test.rb
95
- - test/dummy/test/unit/model_with_non_default_length_field_test.rb
96
- - test/formatted_length_test.rb
97
- - test/test_helper.rb
73
+ - Rakefile
74
+ - formatted_length.gemspec
75
+ - lib/formatted_length.rb
76
+ - lib/formatted_length/formatted_length.rb
77
+ - lib/formatted_length/model_additions.rb
78
+ - lib/formatted_length/railtie.rb
79
+ - lib/formatted_length/version.rb
80
+ - lib/tasks/formatted_length_tasks.rake
81
+ - spec/formatted_length/formatted_length_spec.rb
82
+ - spec/formatted_length/model_additions_spec.rb
83
+ - spec/spec_helper.rb
98
84
  homepage: http://github.com/mrnbrkt
99
85
  licenses: []
100
86
  post_install_message:
@@ -107,69 +93,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
93
  - - ! '>='
108
94
  - !ruby/object:Gem::Version
109
95
  version: '0'
110
- segments:
111
- - 0
112
- hash: 256112071020433159
113
96
  required_rubygems_version: !ruby/object:Gem::Requirement
114
97
  none: false
115
98
  requirements:
116
99
  - - ! '>='
117
100
  - !ruby/object:Gem::Version
118
101
  version: '0'
119
- segments:
120
- - 0
121
- hash: 256112071020433159
122
102
  requirements: []
123
- rubyforge_project:
103
+ rubyforge_project: formatted_length
124
104
  rubygems_version: 1.8.10
125
105
  signing_key:
126
106
  specification_version: 3
127
107
  summary: Adds ability to present integer amount of seconds as text
128
108
  test_files:
129
- - test/acts_as_formatted_length_test.rb
130
- - test/dummy/app/assets/javascripts/application.js
131
- - test/dummy/app/assets/stylesheets/application.css
132
- - test/dummy/app/controllers/application_controller.rb
133
- - test/dummy/app/helpers/application_helper.rb
134
- - test/dummy/app/models/model_with_default_length_field.rb
135
- - test/dummy/app/models/model_with_length_field_and_setter.rb
136
- - test/dummy/app/models/model_with_non_default_length_field.rb
137
- - test/dummy/app/views/layouts/application.html.erb
138
- - test/dummy/config/application.rb
139
- - test/dummy/config/boot.rb
140
- - test/dummy/config/database.yml
141
- - test/dummy/config/environment.rb
142
- - test/dummy/config/environments/development.rb
143
- - test/dummy/config/environments/production.rb
144
- - test/dummy/config/environments/test.rb
145
- - test/dummy/config/initializers/backtrace_silencers.rb
146
- - test/dummy/config/initializers/inflections.rb
147
- - test/dummy/config/initializers/mime_types.rb
148
- - test/dummy/config/initializers/secret_token.rb
149
- - test/dummy/config/initializers/session_store.rb
150
- - test/dummy/config/initializers/wrap_parameters.rb
151
- - test/dummy/config/locales/en.yml
152
- - test/dummy/config/routes.rb
153
- - test/dummy/config.ru
154
- - test/dummy/db/development.sqlite3
155
- - test/dummy/db/migrate/20111026123348_create_model_with_default_length_fields.rb
156
- - test/dummy/db/migrate/20111026123424_create_model_with_non_default_length_fields.rb
157
- - test/dummy/db/migrate/20111026144756_create_model_with_length_field_and_setters.rb
158
- - test/dummy/db/schema.rb
159
- - test/dummy/db/test.sqlite3
160
- - test/dummy/log/development.log
161
- - test/dummy/log/test.log
162
- - test/dummy/public/404.html
163
- - test/dummy/public/422.html
164
- - test/dummy/public/500.html
165
- - test/dummy/public/favicon.ico
166
- - test/dummy/Rakefile
167
- - test/dummy/script/rails
168
- - test/dummy/test/fixtures/model_with_default_length_fields.yml
169
- - test/dummy/test/fixtures/model_with_length_field_and_setters.yml
170
- - test/dummy/test/fixtures/model_with_non_default_length_fields.yml
171
- - test/dummy/test/unit/model_with_default_length_field_test.rb
172
- - test/dummy/test/unit/model_with_length_field_and_setter_test.rb
173
- - test/dummy/test/unit/model_with_non_default_length_field_test.rb
174
- - test/formatted_length_test.rb
175
- - test/test_helper.rb
109
+ - spec/formatted_length/formatted_length_spec.rb
110
+ - spec/formatted_length/model_additions_spec.rb
111
+ - spec/spec_helper.rb