mongoid-serializers 0.0.1.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1cc56d2a1203878b42381e89515905fdbda3451a
4
+ data.tar.gz: 4fe3de42f00ecaff330e806b9730ad5064c210ef
5
+ SHA512:
6
+ metadata.gz: eebe627e1b2d4c15ef87326f28b2821783998b11d0661e0e125f1e979978f5408b1bf9fc4bf3518042b42783a457a5ca9c31793666db8a714711db722b972b85
7
+ data.tar.gz: daf5a3e227eaef0f4df5a24193991af0be84a8b4f0a66be48757f9390c04c4eb79db892a6d943be667a63ae440555dc5cb2c303c338447b087e5d64cfa978a34
@@ -0,0 +1,15 @@
1
+ git /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.gem
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-serializers.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Giovanni Bonetti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # Mongoid::Serializers
2
+
3
+ A few useful serializers for Mongoid Attributes, like Date (YYYY-MM-DD) and Time (HH:mm)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mongoid-serializers'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mongoid-serializers
20
+
21
+ ## Usage
22
+
23
+ In your model, specify the field like this:
24
+
25
+ ```ruby
26
+ field :date, type: Mongoid::Serializers::Date
27
+ field :time, type: Mongoid::Serializers::Time
28
+ ```
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/giovannibonetti/mongoid-serializers/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,18 @@
1
+ # require "action_view/helpers"
2
+ require "mongoid"
3
+ require "mongoid/serializers/version"
4
+ require "mongoid/serializers/date"
5
+ require "mongoid/serializers/duration"
6
+ require "mongoid/serializers/money"
7
+ require "mongoid/serializers/percentage"
8
+ require "mongoid/serializers/time"
9
+
10
+ module Mongoid
11
+ module Serializers
12
+ # Nothing happens here -> each Serializer is independent
13
+ # eager_autoload do
14
+ # autoload :Date
15
+ # end
16
+
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ module Mongoid
2
+ module Serializers
3
+ class Date < ::Date
4
+
5
+ def mongoize
6
+ # puts "Instance.mongoize(#{self})"
7
+ self.strftime("%Y-%m-%d")
8
+ end
9
+
10
+ def to_s
11
+ self.strftime("%Y-%m-%d")
12
+ end
13
+
14
+ def as_json(*args)
15
+ self.strftime("%Y-%m-%d")
16
+ end
17
+
18
+ class << self
19
+ def mongoize(object)
20
+ # puts "Class.mongoize(#{object})"
21
+ case object
22
+ when self then object.mongoize
23
+ when String then
24
+ begin
25
+ self.parse(object).mongoize
26
+ rescue ArgumentError
27
+ nil
28
+ end
29
+ when Date then self.parse(object.to_s).mongoize
30
+ end
31
+ end
32
+
33
+ def demongoize(object)
34
+ # puts "Class.demongoize(#{object})"
35
+ self.parse(object) if object
36
+ end
37
+
38
+ def evolve(object)
39
+ mongoize(object)
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,78 @@
1
+ require 'mongoid/serializers/helpers'
2
+
3
+ module Mongoid
4
+ module Serializers
5
+ class Duration
6
+
7
+ attr_reader :minutes
8
+
9
+ def initialize(minutes)
10
+ # puts "Class.initialize(#{minutes})"
11
+ @minutes = case minutes
12
+ when String then hhmm_to_minutes(minutes)
13
+ else minutes.to_i
14
+ end
15
+ end
16
+
17
+ def to_s
18
+ minutes.minutes.to_hhmm
19
+ end
20
+
21
+ def as_json(*args)
22
+ minutes
23
+ end
24
+
25
+ def seconds
26
+ (minutes * 60).seconds
27
+ end
28
+
29
+ def mongoize
30
+ # puts "Instance.mongoize(#{self})"
31
+ minutes
32
+ end
33
+
34
+ def nonzero?
35
+ minutes > 0
36
+ end
37
+
38
+ ## Arithmetic operators, Fixnum conversion
39
+ def coerce(other)
40
+ if other.is_a?(Fixnum)
41
+ [other, self.seconds]
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ # def +(other)
48
+ # self.seconds + other
49
+ # end
50
+
51
+ class << self
52
+ def mongoize(object)
53
+ # puts "Class.mongoize(#{object})"
54
+ case object
55
+ when self then object.mongoize
56
+ else self.new(object).mongoize
57
+ end
58
+ end
59
+
60
+ def demongoize(object)
61
+ # puts "Class.demongoize(#{object})"
62
+ self.new(object) if object
63
+ end
64
+
65
+ def evolve(object)
66
+ mongoize(object)
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def method_missing(method, *args, &block)
73
+ self.seconds.send(method, *args, &block)
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,58 @@
1
+ require 'active_support/core_ext/numeric' # from gem 'activesupport'
2
+ # Nas funções abaixo, onde está escrito "seconds" você pode ler ActiveSupport do tipo (10.hours + 30.minutes)
3
+ # pois o Active Support converte, por exemplo, 1.hour no número inteiro (Fixnum) 3600
4
+
5
+ def hhmm_to_number(hhmm)
6
+ digits = hhmm.to_s.gsub(/\D/,"").gsub(/^0+/,"")
7
+ # Remoção de digitos demais do lado direito
8
+ digits = digits.slice(0, 4) if (digits.length > 4)
9
+ hours = digits[0..-3].to_i
10
+ minutes = digits[-2..-1].to_i
11
+ hours * 3600 + minutes * 60
12
+ end
13
+
14
+ def hhmm_to_minutes(hhmm)
15
+ (hhmm_to_number(hhmm) / 60).to_i
16
+ end
17
+
18
+ def hhmm_to_seconds(hhmm)
19
+ hhmm_to_number(hhmm).seconds
20
+ end
21
+
22
+ def time_to_borel_interval(start_time=0, end_time=0, allow_night_shift=false)
23
+ start_time = hhmm_to_seconds(start_time) unless start_time.is_a? Fixnum
24
+ end_time = hhmm_to_seconds(end_time) unless end_time.is_a? Fixnum
25
+ raise Exception if (start_time > 24 * 3600) || (end_time > 24 * 3600) || (start_time < 0) || (end_time < 0)
26
+
27
+ # A duração do intervalo (.width) será sempre um número em (0.hours, 24.hours] (aberto no 0, fechado no 24)
28
+ if start_time < end_time
29
+ Interval[start_time.to_i, end_time.to_i]
30
+ elsif (start_time >= end_time) && allow_night_shift
31
+ Interval[start_time.to_i, (end_time.to_i + 24 * 3600)]
32
+ elsif (start_time == end_time) && (start_time == 0)
33
+ Interval[0, 24 * 3600] # Dia inteiro
34
+ else
35
+ return Interval[]
36
+ end
37
+ end
38
+
39
+ # Gera uma semana 'em torno de' date
40
+ # Por padrão, a semana começa na segunda-feira
41
+ def week_containing(date,start_on=1)
42
+ date = Date.parse(date) if date.is_a? String
43
+ week_start = date - (date.wday - start_on) % 7
44
+ # week_start = (date - start_on) - (date.wday - start_on) % 7
45
+ week = (0..6).map{|i| week_start + i}
46
+ end
47
+
48
+ # Gera um objeto DateTime a partir de um objeto Date/string e outro Time/string
49
+ def DateTimefrom(dt,tm,utc=nil)
50
+ dt = Date.parse(dt) if dt.is_a?(String)
51
+ tm = Time.zone.parse(tm) if tm.is_a?(String)
52
+ _ = [dt.year, dt.month, dt.day, tm.hour, tm.min, tm.sec].map(&:to_i)
53
+ if utc
54
+ DateTime.new(*_, utc)
55
+ else
56
+ Time.zone.local(*_)
57
+ end
58
+ end
@@ -0,0 +1,112 @@
1
+ # require 'action_view/helpers'
2
+
3
+ module Mongoid
4
+ module Serializers
5
+ class Money
6
+ # include ActionView::Helpers::NumberHelper
7
+ include ::Comparable
8
+ attr_reader :money
9
+
10
+ def initialize(money)
11
+ @money =
12
+ case money
13
+ when Integer then money
14
+ when Float then (100 * money).round
15
+ when String then money.gsub(/\D/,'').gsub(/^0+(.*)/,'\1').to_i
16
+ else 0
17
+ end
18
+ end
19
+
20
+ # def to_s
21
+ # number_to_currency(money / 100.0)
22
+ # end
23
+
24
+ def to_f
25
+ money / 100.0
26
+ end
27
+
28
+ def as_json(*args)
29
+ # In Javascript all numbers are floating point
30
+ money / 100.0
31
+ end
32
+
33
+ def mongoize
34
+ # puts "Instance.mongoize(#{self})"
35
+ money
36
+ end
37
+
38
+ def zero?
39
+ money == 0
40
+ end
41
+
42
+ def nonzero?
43
+ money > 0
44
+ end
45
+
46
+ ## Arithmetic operators
47
+ def coerce(other)
48
+ if other.is_a?(Numeric)
49
+ [other, self.money / 100.0]
50
+ else
51
+ super
52
+ end
53
+ end
54
+
55
+ def +(other)
56
+ self.class.new(self.to_f + other.to_f)
57
+ end
58
+
59
+ def -(other)
60
+ self.class.new(self.to_f - other.to_f)
61
+ end
62
+
63
+ def *(other)
64
+ if other.is_a?(Numeric) || other.is_a?(Mongoid::Serializers::Percentage)
65
+ self.class.new(self.to_f * other.to_f)
66
+ else
67
+ self.to_f * other.to_f
68
+ end
69
+ end
70
+
71
+ def /(other)
72
+ if other.is_a?(Numeric) || other.is_a?(Mongoid::Serializers::Percentage)
73
+ self.class.new(self.to_f / other.to_f)
74
+ else
75
+ self.to_f / other.to_f
76
+ end
77
+ end
78
+
79
+ def <=> (other)
80
+ self.to_f <=> other.to_f
81
+ rescue
82
+ nil
83
+ end
84
+
85
+ class << self
86
+ def mongoize(object)
87
+ # puts "Class.mongoize(#{object})"
88
+ case object
89
+ when self then object.mongoize
90
+ else self.new(object).mongoize
91
+ end
92
+ end
93
+
94
+ def demongoize(object)
95
+ # puts "Class.demongoize(#{object})"
96
+ self.new(object) if object
97
+ end
98
+
99
+ def evolve(object)
100
+ mongoize(object)
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ def method_missing(method, *args, &block)
107
+ self.new money.send(method, *args, &block)
108
+ end
109
+
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,99 @@
1
+ module Mongoid
2
+ module Serializers
3
+ class Percentage
4
+ attr_reader :percentage
5
+
6
+ def initialize(percentage)
7
+ @percentage =
8
+ case percentage
9
+ when Numeric then percentage
10
+ when String then 100 * eval( percentage.gsub(/[^\d\,\.%]/, '').gsub(',', '.').gsub('%', "/100.0") ).to_f
11
+ else 0
12
+ end
13
+
14
+ # Floating-point precision: 1 digit
15
+ @percentage = (10 * @percentage).round / 10.0
16
+ end
17
+
18
+ def to_s
19
+ "#{'%.1f' % percentage} %"
20
+ end
21
+
22
+ def to_f
23
+ percentage / 100.0
24
+ end
25
+
26
+ def as_json(*args)
27
+ percentage
28
+ end
29
+
30
+ def mongoize
31
+ # puts "Instance.mongoize(#{self})"
32
+ percentage
33
+ end
34
+
35
+ def nonzero?
36
+ percentage > 0
37
+ end
38
+
39
+ ## Arithmetic operators
40
+ def coerce(other)
41
+ if other.is_a?(Numeric)
42
+ [other, self.to_f]
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ def +(other)
49
+ if other.is_a?(self.class)
50
+ self.class.new(self.percentage + other.percentage)
51
+ else
52
+ other + self.to_f
53
+ end
54
+ end
55
+
56
+ def -(other)
57
+ self.class.new(100.0 * (self.to_f - other.to_f))
58
+ end
59
+
60
+ def *(other)
61
+ if other.is_a?(self.class) || other.is_a?(Numeric)
62
+ self.class.new(self.percentage * other.to_f)
63
+ else
64
+ other * self.to_f
65
+ end
66
+ end
67
+
68
+ def /(other)
69
+ self.class.new(100.0 * (self.to_f / other.to_f))
70
+ end
71
+
72
+ class << self
73
+ def mongoize(object)
74
+ # puts "Class.mongoize(#{object})"
75
+ case object
76
+ when self then object.mongoize
77
+ else self.new(object).mongoize
78
+ end
79
+ end
80
+
81
+ def demongoize(object)
82
+ # puts "Class.demongoize(#{object})"
83
+ self.new(object) if object
84
+ end
85
+
86
+ def evolve(object)
87
+ mongoize(object)
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def method_missing(method, *args, &block)
94
+ self.class.new percentage.send(method, *args, &block)
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,74 @@
1
+ require 'mongoid/serializers/helpers'
2
+
3
+ module Mongoid
4
+ module Serializers
5
+ class Time < ::Time
6
+ # Para obter os comportamentos Time.zone o ideal talvez fosse herdar de ActiveSupport::TimeWithZone
7
+
8
+ def mongoize
9
+ # puts "Instance.mongoize(#{self})"
10
+ self.strftime("%H:%M")
11
+ end
12
+
13
+ ## Arithmetic operators
14
+ def +(other)
15
+ if other.is_a?(Mongoid::Serializers::Duration)
16
+ self.class.parse (self.seconds + other.seconds).to_hhmm
17
+ elsif other.is_a?(Fixnum) # ActiveSupport -> duration -> seconds
18
+ self.class.parse (self.seconds + other).to_hhmm
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def -(other)
25
+ if other.is_a?(self.class)
26
+ Mongoid::Serializers::Duration.new(self.minutes - other.minutes)
27
+ elsif other.is_a?(Mongoid::Serializers::Duration)
28
+ self.class.parse (self.seconds - other.seconds).to_hhmm
29
+ elsif other.is_a?(Fixnum) # ActiveSupport -> duration -> seconds
30
+ self.class.parse (self.seconds - other).to_hhmm
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ class << self
37
+ def mongoize(object)
38
+ # puts "Class.mongoize(#{object})"
39
+ case object
40
+ when self then object.mongoize
41
+ when String then self.parse(object).mongoize
42
+ when Time then self.parse(object.strftime("%H:%M")).mongoize
43
+ end
44
+ end
45
+
46
+ def demongoize(object)
47
+ # puts "Class.demongoize(#{object})"
48
+ self.parse(object) if object
49
+ end
50
+
51
+ def evolve(object)
52
+ mongoize(object)
53
+ end
54
+ end
55
+
56
+ def to_s
57
+ self.strftime("%H:%M")
58
+ end
59
+
60
+ def as_json(*args)
61
+ self.strftime("%H:%M")
62
+ end
63
+
64
+ def minutes
65
+ hhmm_to_minutes(self.to_s)
66
+ end
67
+
68
+ def seconds
69
+ hhmm_to_seconds(self.to_s)
70
+ end
71
+
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Serializers
3
+ VERSION = "0.0.1-rc1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid/serializers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid-serializers"
8
+ spec.version = Mongoid::Serializers::VERSION
9
+ spec.authors = ["Giovanni Bonetti"]
10
+ spec.email = ["giovanni.bonetti@gmail.com"]
11
+ spec.summary = %q{A few useful serializers for Mongoid Attributes, like Date (YYYY-MM-DD) and Time (HH:mm).}
12
+ spec.description = %q{If Mongoid default serializers for Date and Time don't work for you, try this out!}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'mongoid', '>= 3.0'
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-serializers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Giovanni Bonetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: If Mongoid default serializers for Date and Time don't work for you,
56
+ try this out!
57
+ email:
58
+ - giovanni.bonetti@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/mongoid/serializers.rb
69
+ - lib/mongoid/serializers/date.rb
70
+ - lib/mongoid/serializers/duration.rb
71
+ - lib/mongoid/serializers/helpers.rb
72
+ - lib/mongoid/serializers/money.rb
73
+ - lib/mongoid/serializers/percentage.rb
74
+ - lib/mongoid/serializers/time.rb
75
+ - lib/mongoid/serializers/version.rb
76
+ - mongoid-serializers.gemspec
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">"
93
+ - !ruby/object:Gem::Version
94
+ version: 1.3.1
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A few useful serializers for Mongoid Attributes, like Date (YYYY-MM-DD) and
101
+ Time (HH:mm).
102
+ test_files: []