simple_model 1.4.2 → 1.4.3
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.
- checksums.yaml +4 -4
- data/benchmarks/extend_core.rb +99 -0
- data/lib/simple_model/base.rb +2 -0
- data/lib/simple_model/config.rb +1 -1
- data/lib/simple_model/extend_core.rb +60 -21
- data/lib/simple_model/version.rb +1 -1
- data/spec/simple_model/base_spec.rb +0 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d0480495e4e082cfe0aa97259f7267a42b14a10
|
4
|
+
data.tar.gz: d011d540617029664f7487593fd00c532de03ca4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b12d771e2752809cc8122eaf778291c58657068377a434756074c196e59a3569e7f48d7fd21cae2ac8f11efecdc651337973c6186c43ecb68fc1b6e44fe4df56
|
7
|
+
data.tar.gz: 87486a5fbf7a4137ba4cde89cd5cc48ff7d12a0894158f8d654f79b6119c7f37e6c79a20dd0b315e80bf4da4a9ceaea0ac768cbd71f15a3c739554e2ca79dc67
|
@@ -0,0 +1,99 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require 'benchmark/ips'
|
3
|
+
require 'simple_model'
|
4
|
+
|
5
|
+
puts `ruby -v`
|
6
|
+
|
7
|
+
|
8
|
+
# HotTub uses persisted blocks in several places.
|
9
|
+
|
10
|
+
# Tests performance of passing a known block to block call or yield.
|
11
|
+
|
12
|
+
|
13
|
+
us = "07/14/2011 13:43:37"
|
14
|
+
iso = "2011-07-14 13:43:37"
|
15
|
+
json = "\/Date(1310669017000)\/"
|
16
|
+
|
17
|
+
puts us.to_time
|
18
|
+
puts iso.to_time
|
19
|
+
puts json.to_time
|
20
|
+
|
21
|
+
|
22
|
+
Benchmark.ips do |x|
|
23
|
+
|
24
|
+
x.report("date iso") do
|
25
|
+
iso.to_date
|
26
|
+
end
|
27
|
+
|
28
|
+
x.report("date us") do
|
29
|
+
us.to_date
|
30
|
+
end
|
31
|
+
|
32
|
+
x.report("date json") do
|
33
|
+
json.to_date
|
34
|
+
end
|
35
|
+
|
36
|
+
x.report("time iso") do
|
37
|
+
iso.to_time
|
38
|
+
end
|
39
|
+
|
40
|
+
x.report("time us") do
|
41
|
+
us.to_time
|
42
|
+
end
|
43
|
+
|
44
|
+
x.report("time json") do
|
45
|
+
json.to_time
|
46
|
+
end
|
47
|
+
|
48
|
+
x.compare!
|
49
|
+
end
|
50
|
+
|
51
|
+
# V-1.4.3
|
52
|
+
# ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
|
53
|
+
# Calculating -------------------------------------
|
54
|
+
# date iso 12.056k i/100ms
|
55
|
+
# date us 11.120k i/100ms
|
56
|
+
# date json 9.113k i/100ms
|
57
|
+
# time iso 2.409k i/100ms
|
58
|
+
# time us 2.098k i/100ms
|
59
|
+
# time json 10.795k i/100ms
|
60
|
+
# -------------------------------------------------
|
61
|
+
# date iso 200.936k (± 2.9%) i/s - 1.013M
|
62
|
+
# date us 184.495k (± 3.3%) i/s - 922.960k
|
63
|
+
# date json 130.588k (± 1.4%) i/s - 656.136k
|
64
|
+
# time iso 27.103k (± 1.7%) i/s - 137.313k
|
65
|
+
# time us 23.126k (± 1.0%) i/s - 117.488k
|
66
|
+
# time json 187.509k (± 2.4%) i/s - 939.165k
|
67
|
+
|
68
|
+
# Comparison:
|
69
|
+
# date iso: 200935.7 i/s
|
70
|
+
# time json: 187508.9 i/s - 1.07x slower
|
71
|
+
# date us: 184494.7 i/s - 1.09x slower
|
72
|
+
# date json: 130588.2 i/s - 1.54x slower
|
73
|
+
# time iso: 27103.4 i/s - 7.41x slower
|
74
|
+
# time us: 23126.3 i/s - 8.69x slower
|
75
|
+
|
76
|
+
# V-1.4.2
|
77
|
+
# ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
|
78
|
+
# Calculating -------------------------------------
|
79
|
+
# date iso 3.499k i/100ms
|
80
|
+
# date us 2.997k i/100ms
|
81
|
+
# date json 2.391k i/100ms
|
82
|
+
# time iso 2.484k i/100ms
|
83
|
+
# time us 2.219k i/100ms
|
84
|
+
# time json 1.760k i/100ms
|
85
|
+
# -------------------------------------------------
|
86
|
+
# date iso 41.965k (± 1.9%) i/s - 209.940k
|
87
|
+
# date us 35.729k (± 1.2%) i/s - 179.820k
|
88
|
+
# date json 27.491k (± 1.5%) i/s - 138.678k
|
89
|
+
# time iso 27.247k (± 1.6%) i/s - 136.620k
|
90
|
+
# time us 23.937k (± 1.2%) i/s - 119.826k
|
91
|
+
# time json 19.031k (± 1.4%) i/s - 96.800k
|
92
|
+
|
93
|
+
# Comparison:
|
94
|
+
# date iso: 41964.9 i/s
|
95
|
+
# date us: 35729.2 i/s - 1.17x slower
|
96
|
+
# date json: 27491.2 i/s - 1.53x slower
|
97
|
+
# time iso: 27247.5 i/s - 1.54x slower
|
98
|
+
# time us: 23937.0 i/s - 1.75x slower
|
99
|
+
# time json: 19031.1 i/s - 2.21x slower
|
data/lib/simple_model/base.rb
CHANGED
data/lib/simple_model/config.rb
CHANGED
@@ -2,7 +2,7 @@ module SimpleModel
|
|
2
2
|
class Config
|
3
3
|
DEFAULTS = {
|
4
4
|
:initialize_defaults => false,
|
5
|
-
:attributes_store => (RUBY_VERSION
|
5
|
+
:attributes_store => (RUBY_VERSION >= "2.2" ? :symbol : :string) # OPTIONS => :string, :symbol, :indifferent
|
6
6
|
}.freeze
|
7
7
|
|
8
8
|
ATTRIBUTE_STORES = {
|
@@ -53,9 +53,29 @@ module SimpleModel
|
|
53
53
|
#Extend Ruby String.rb
|
54
54
|
String.class_eval do
|
55
55
|
|
56
|
+
US_DATE_REGEX = (/(^[0-9]{1,2}[- \/.][0-9]{1,2}[- \/.][0-9]{4})/).freeze
|
57
|
+
|
58
|
+
JSON_DATE_REGEX = (/^(\/Date\().*(\)\/)$/).freeze
|
59
|
+
|
60
|
+
DIGIT_ONLY_REGEX = (/^+d$/).freeze
|
61
|
+
|
62
|
+
SPLIT_DATE_REGEX = (/\-|\/|\./).freeze
|
63
|
+
|
64
|
+
DATE_TR = ('/\/|\./').freeze
|
65
|
+
|
66
|
+
JSON_DATE_TR = ('/[a-zA-z\(\)\\\/]*/').freeze
|
67
|
+
|
68
|
+
EMPTY_STRING = ''.freeze
|
69
|
+
|
70
|
+
US_DATE_FORMAT = "%m-%d-%Y".freeze
|
71
|
+
|
72
|
+
ISO_DATE_FORMAT = "%Y-%m-%d".freeze
|
73
|
+
|
74
|
+
BOOLEAN_REGEX = (/^(true|t|yes|y|1)$/i).freeze
|
75
|
+
|
56
76
|
# returns boolean value for common boolean string values
|
57
77
|
def to_b
|
58
|
-
return true if self =~
|
78
|
+
return true if self =~ BOOLEAN_REGEX
|
59
79
|
false
|
60
80
|
end
|
61
81
|
|
@@ -69,20 +89,10 @@ module SimpleModel
|
|
69
89
|
# * safe_date_string("\/Date(1310669017000)\/") # =>
|
70
90
|
def safe_datetime_string
|
71
91
|
safe_date = nil
|
72
|
-
if self
|
73
|
-
safe_date =
|
74
|
-
|
75
|
-
|
76
|
-
if splt[2].length > 4
|
77
|
-
time = splt[2][4..(splt[2].length - 1)]
|
78
|
-
splt[2] = splt[2][0..3]
|
79
|
-
end
|
80
|
-
if splt.length == 3 && splt[2].length == 4
|
81
|
-
safe_date << "#{splt[2]}-#{splt[0]}-#{splt[1]}"
|
82
|
-
safe_date << "#{time}" unless time.nil? || time.to_s.length == 0
|
83
|
-
end
|
84
|
-
elsif self =~ /^\/Date\(/
|
85
|
-
safe_date = Time.at(((self.gsub(/(\/Date\()/,"")).gsub(/\)\/$/,"").to_i) / 1000).to_s
|
92
|
+
if self =~ US_DATE_REGEX
|
93
|
+
safe_date = us_date_to_iso_str
|
94
|
+
elsif self =~ JSON_DATE_REGEX
|
95
|
+
safe_date = json_date_to_time.to_s
|
86
96
|
else
|
87
97
|
safe_date = self
|
88
98
|
end
|
@@ -92,28 +102,56 @@ module SimpleModel
|
|
92
102
|
# Use safe_datetime_string help with those pesky US date formats in Ruby 1.9
|
93
103
|
# or to change an integer string to date
|
94
104
|
def to_date
|
95
|
-
return
|
96
|
-
|
105
|
+
return self.to_i.to_date if self =~ DIGIT_ONLY_REGEX
|
106
|
+
if self =~ US_DATE_REGEX
|
107
|
+
Date.strptime(self.tr(DATE_TR,'-'), US_DATE_FORMAT)
|
108
|
+
elsif self =~ JSON_DATE_REGEX
|
109
|
+
json_date_to_time.to_date
|
110
|
+
else
|
111
|
+
Date.strptime(self.tr(DATE_TR,'-'), ISO_DATE_FORMAT)
|
112
|
+
end
|
97
113
|
end
|
98
114
|
|
99
115
|
# Use safe_datetime_string help with those pesky US date formats in Ruby 1.9
|
100
116
|
# or to change an integer string to date
|
101
117
|
def to_time
|
102
|
-
return
|
103
|
-
|
118
|
+
return self.to_i.to_time if self =~ DIGIT_ONLY_REGEX
|
119
|
+
if self =~ US_DATE_REGEX
|
120
|
+
Time.parse(us_date_to_iso_str)
|
121
|
+
elsif self =~ JSON_DATE_REGEX
|
122
|
+
json_date_to_time
|
123
|
+
else
|
124
|
+
Time.parse(self)
|
125
|
+
end
|
104
126
|
end
|
105
127
|
|
106
128
|
alias :core_to_f :to_f
|
107
129
|
|
130
|
+
SCRUB_NUMBER_REGEX = /[^0-9\.\+\-]/.freeze
|
131
|
+
|
108
132
|
# Remove none numeric characters then run default ruby float cast
|
109
133
|
def to_f
|
110
|
-
gsub(
|
134
|
+
gsub(SCRUB_NUMBER_REGEX, EMPTY_STRING).core_to_f
|
111
135
|
end
|
112
136
|
|
113
137
|
alias :core_to_d :to_d
|
114
138
|
|
115
139
|
def to_d
|
116
|
-
gsub(
|
140
|
+
gsub(SCRUB_NUMBER_REGEX, EMPTY_STRING).core_to_d
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def json_date_to_time
|
146
|
+
(Time.at(self.tr(JSON_DATE_TR,EMPTY_STRING).to_i / 1000))
|
147
|
+
end
|
148
|
+
|
149
|
+
def us_date_to_iso_str
|
150
|
+
date_split = split(US_DATE_REGEX)
|
151
|
+
time = date_split[2]
|
152
|
+
date = date_split[1]
|
153
|
+
date_split = date.split(SPLIT_DATE_REGEX)
|
154
|
+
"#{date_split[2]}-#{date_split[0]}-#{date_split[1]}#{time}"
|
117
155
|
end
|
118
156
|
end
|
119
157
|
|
@@ -124,6 +162,7 @@ module SimpleModel
|
|
124
162
|
!zero?
|
125
163
|
end
|
126
164
|
end
|
165
|
+
|
127
166
|
Fixnum.class_eval do
|
128
167
|
include ToCurrencyS
|
129
168
|
|
data/lib/simple_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua T Mckinney
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- LICENSE.txt
|
108
108
|
- README.md
|
109
109
|
- Rakefile
|
110
|
+
- benchmarks/extend_core.rb
|
110
111
|
- benchmarks/simple_model.rb
|
111
112
|
- gemfiles/3.2.gemfile
|
112
113
|
- gemfiles/4.0.gemfile
|
@@ -147,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
148
|
version: '0'
|
148
149
|
requirements: []
|
149
150
|
rubyforge_project: simple_model
|
150
|
-
rubygems_version: 2.4.
|
151
|
+
rubygems_version: 2.4.5.1
|
151
152
|
signing_key:
|
152
153
|
specification_version: 4
|
153
154
|
summary: Simpifies building tableless models or models backed by webservices
|