lazier 3.3.7 → 3.3.8

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.
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Sun Aug 18 16:31:08 2013 by
106
+ Generated on Mon Sep 2 17:35:26 2013 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.7 (ruby-2.0.0).
109
109
  </div>
data/lazier.gemspec CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |gem|
25
25
  gem.required_ruby_version = ">= 1.9.3"
26
26
 
27
27
  gem.add_dependency("json", "~> 1.8.0")
28
- gem.add_dependency("actionpack", ">= 3.2.13") # We don't use ~> to enable use with 4.0
28
+ gem.add_dependency("activesupport", ">= 3.2.13") # We don't use ~> to enable use with 4.0
29
29
  gem.add_dependency("tzinfo", ">= 0.3.37") # We don't use ~> to enable use with 0.3.37 (required by activesupport 4.0) and 1.x, which is the latest available
30
30
  gem.add_dependency("r18n-desktop", "~> 1.1.5")
31
31
  gem.add_dependency("hashie", "~> 2.0.5")
data/lib/lazier.rb CHANGED
@@ -6,8 +6,7 @@
6
6
 
7
7
  require "json"
8
8
  require "tzinfo"
9
- require "active_support/all"
10
- require "action_view"
9
+ require "active_support/core_ext"
11
10
  require "r18n-desktop"
12
11
  require "hashie"
13
12
 
@@ -109,7 +108,7 @@ module Lazier
109
108
  # Finds a class to instantiate.
110
109
  #
111
110
  # @param cls [Symbol|String|Object] If a `String` or a `Symbol` or a `Class`, then it will be the class to instantiate. Otherwise the class of the object will returned.
112
- # @param scope [String] An additional scope to find the class. `%CLASS%` will be substituted with the class name.
111
+ # @param scope [String] An additional scope to find the class. `%CLASS%`, `%`, `$`, `?` and `@` will be substituted with the class name.
113
112
  # @param only_in_scope [Boolean] If only try to instantiate the class in the scope.
114
113
  # @return [Class] The found class.
115
114
  def self.find_class(cls, scope = "::%CLASS%", only_in_scope = false)
@@ -123,13 +122,24 @@ module Lazier
123
122
  rv = search_class(cls) # Search outside scope
124
123
  end
125
124
 
126
- rv = search_class(scope.to_s.gsub("%CLASS%", cls)) if !rv && cls !~ /^::/ && scope.present? # Search inside scope
125
+ rv = search_class(scope.to_s.gsub(/%CLASS%|[@%$?]/, cls)) if !rv && cls !~ /^::/ && scope.present? # Search inside scope
127
126
  rv || raise(NameError.new("", cls))
128
127
  else
129
128
  cls.is_a?(::Class) ? cls : cls.class
130
129
  end
131
130
  end
132
131
 
132
+ # Measure the time in milliseconds required to execute the given block.
133
+ #
134
+ # @param message [String|NilClass] An optional message (see return value).
135
+ # @param precision [Fixnum] The precision for the message (see return value)..
136
+ # @param block [Proc] The block to evaluate.
137
+ # @return [Float|String] If a `message` is provided, then the message itself plus the duration under parenthesis will be returned, otherwise the duration alone as a number.
138
+ def self.benchmark(message = nil, precision = 0, &block)
139
+ rv = Benchmark.ms(&block)
140
+ message ? ("%s (%0.#{precision}f ms)" % [message, rv]) : rv
141
+ end
142
+
133
143
  private
134
144
  # Tries to search a class.
135
145
  #
data/lib/lazier/object.rb CHANGED
@@ -7,7 +7,6 @@
7
7
  module Lazier
8
8
  # Extensions for all objects.
9
9
  module Object
10
- include ::ActionView::Helpers::NumberHelper
11
10
  extend ::ActiveSupport::Concern
12
11
 
13
12
  # Expression to match a boolean value.
@@ -141,7 +140,7 @@ module Lazier
141
140
  # @param precision [Fixnum] The precision to keep.
142
141
  # @return [Float] The rounded float representaton of the object.
143
142
  def round_to_precision(precision = 2)
144
- is_number? ? number_with_precision(to_float, precision: [precision, 0].max) : nil
143
+ is_number? ? to_float.round([precision, 0].max) : nil
145
144
  end
146
145
 
147
146
  # Formats a number.
@@ -156,9 +155,12 @@ module Lazier
156
155
  if is_number? then
157
156
  settings = ::Lazier.settings.format_number
158
157
  add_string ||= settings[:add_string]
159
- format, unit = (add_string ? ["%n %u", add_string] : ["%n", ""])
160
158
 
161
- number_to_currency(self, {precision: [precision || settings[:precision], 0].max, separator: decimal_separator || settings[:decimal_separator], delimiter: k_separator || settings[:k_separator], format: format, unit: unit})
159
+ rv = ("%0.#{[precision || settings[:precision], 0].max}f" % to_float).split(".")
160
+ rv[0].gsub!(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{k_separator || settings[:k_separator]}")
161
+ rv = rv.join(decimal_separator || settings[:decimal_separator])
162
+ rv += " #{add_string}" if add_string
163
+ rv
162
164
  else
163
165
  nil
164
166
  end
@@ -16,7 +16,7 @@ module Lazier
16
16
  MINOR = 3
17
17
 
18
18
  # The patch version.
19
- PATCH = 7
19
+ PATCH = 8
20
20
 
21
21
  # The current version of lazier.
22
22
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
@@ -251,10 +251,10 @@ describe Lazier::Object do
251
251
 
252
252
  describe "#round_to_precision" do
253
253
  it "should round number" do
254
- expect(123.456789.round_to_precision(2)).to eq("123.46")
255
- expect(123.456789.round_to_precision(0)).to eq("123")
256
- expect("123.456789".round_to_precision(2)).to eq("123.46")
257
- expect(123.456789.round_to_precision(-1)).to eq("123")
254
+ expect(123.456789.round_to_precision(2)).to eq(123.46)
255
+ expect(123.456789.round_to_precision(0)).to eq(123)
256
+ expect("123.456789".round_to_precision(2)).to eq(123.46)
257
+ expect(123.456789.round_to_precision(-1)).to eq(123)
258
258
  end
259
259
 
260
260
  it "should return nil for non numeric values" do
@@ -264,7 +264,11 @@ describe Lazier::Object do
264
264
 
265
265
  describe "#format_number" do
266
266
  it "should format number" do
267
+ expect(123.format_number(0)).to eq("123")
268
+ expect(123.456789.format_number).to eq("123.46")
269
+ expect(12312.456789.format_number).to eq("12,312.46")
267
270
  expect(123123.456789.format_number).to eq("123,123.46")
271
+ expect(1123123.456789.format_number).to eq("1,123,123.46")
268
272
  expect(123123.456789.format_number(2)).to eq("123,123.46")
269
273
  expect(123123.456789.format_number(3, "@")).to eq("123,123@457")
270
274
  expect(123123.456789.format_number(3, "@", "$")).to eq("123,123@457 $")
data/spec/lazier_spec.rb CHANGED
@@ -52,6 +52,10 @@ describe Lazier do
52
52
  it "should return a valid class" do
53
53
  expect(::Lazier.find_class("String")).to eq(String)
54
54
  expect(::Lazier.find_class("TestClass", "::LazierTest::%CLASS%")).to eq(::LazierTest::TestClass)
55
+ expect(::Lazier.find_class("TestClass", "::LazierTest::@")).to eq(::LazierTest::TestClass)
56
+ expect(::Lazier.find_class("TestClass", "::LazierTest::$")).to eq(::LazierTest::TestClass)
57
+ expect(::Lazier.find_class("TestClass", "::LazierTest::?")).to eq(::LazierTest::TestClass)
58
+ expect(::Lazier.find_class("TestClass", "::LazierTest::%")).to eq(::LazierTest::TestClass)
55
59
  end
56
60
 
57
61
  it "should raise an exception if the class is not found" do
@@ -74,4 +78,21 @@ describe Lazier do
74
78
  expect(::Lazier.find_class(Hash)).to eq(Hash)
75
79
  end
76
80
  end
81
+
82
+ describe ".benchmark" do
83
+ it "should execute the given block" do
84
+ control = ""
85
+ Lazier.benchmark { control = "OK" }
86
+ expect(control).to eq("OK")
87
+ end
88
+
89
+ it "without a message should return the elapsed time" do
90
+ expect(Lazier.benchmark { control = "OK" }).to be_a(Float)
91
+ end
92
+
93
+ it "with a message should embed the elapsed time into the given message" do
94
+ expect(Lazier.benchmark("MESSAGE") { control = "OK" }).to match(/^MESSAGE \(\d+ ms\)$/)
95
+ expect(Lazier.benchmark("MESSAGE", 2) { control = "OK" }).to match(/^MESSAGE \(\d+\.\d+ ms\)$/)
96
+ end
97
+ end
77
98
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazier
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.7
4
+ version: 3.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shogun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-18 00:00:00.000000000 Z
11
+ date: 2013-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.8.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: actionpack
28
+ name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='