active_object 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -0
- data/lib/active_object/hash.rb +2 -1
- data/lib/active_object/numeric.rb +12 -0
- data/lib/active_object/version.rb +1 -1
- data/spec/lib/numeric_spec.rb +52 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8f47131a26b6c89bb04924a24a230950889b374
|
4
|
+
data.tar.gz: 7b59b34890364117fdabeecf0c05dcabad9d506f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3eb7ed5a3fd09b9cbc858b5c93081e32a04bd6a2bcbf439fb315dc9ded6cc536577b87bd2170b0317f0274b02386c7e7fb812ea03ddfe217e695bf30e3936c9c
|
7
|
+
data.tar.gz: 7c1233af1cf71e538cc5cb62d9120e785faf52a94013074adc22df8af1caf62c6b9eb172e486d6c392a464c7b187b9b9101bf977a48d7c6be3793c109ee99c50
|
data/README.md
CHANGED
@@ -833,6 +833,21 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
833
833
|
80.to_byte(:megabyte, :gigabyte) #=> 0.078125 #GB
|
834
834
|
```
|
835
835
|
|
836
|
+
####To Currency:####
|
837
|
+
`to_currency` converts a number to currency string.
|
838
|
+
|
839
|
+
**Options**
|
840
|
+
* precision: 2
|
841
|
+
* unit: "$"
|
842
|
+
|
843
|
+
```ruby
|
844
|
+
3.to_currency #=> "$3.00"
|
845
|
+
3.1.to_currency #=> "$3.10"
|
846
|
+
3.11.to_currency #=> "$3.11"
|
847
|
+
3.11111.to_currency #=> "$3.11"
|
848
|
+
3.to_currency(unit: "@") #=> "@3.00"
|
849
|
+
```
|
850
|
+
|
836
851
|
####To Length:####
|
837
852
|
`to_length` converts a length from one unit to another unit.
|
838
853
|
|
@@ -861,6 +876,21 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
861
876
|
3.5.to_nearest_value([3.0, 3.3, 3.6, 3.9]) #=> 3.6
|
862
877
|
```
|
863
878
|
|
879
|
+
####To Percantage:####
|
880
|
+
`to_percentage` converts a number to percentage string.
|
881
|
+
|
882
|
+
**Options**
|
883
|
+
* precision: 2
|
884
|
+
* unit: "%"
|
885
|
+
|
886
|
+
```ruby
|
887
|
+
3.to_percentage #=> "3.00%"
|
888
|
+
3.1.to_percentage #=> "3.10%"
|
889
|
+
3.11.to_percentage #=> "3.11%"
|
890
|
+
3.11111.to_percentage #=> "3.11%"
|
891
|
+
3.to_percentage(unit: "@") #=> "3.00@"
|
892
|
+
```
|
893
|
+
|
864
894
|
####To Temperature:####
|
865
895
|
`to_temperature` converts a temperature from one unit to another unit.
|
866
896
|
|
data/lib/active_object/hash.rb
CHANGED
@@ -5,7 +5,8 @@ class Hash
|
|
5
5
|
valid_keys.flatten!
|
6
6
|
each_key do |k|
|
7
7
|
unless valid_keys.include?(k)
|
8
|
-
raise ArgumentError,
|
8
|
+
raise ArgumentError,
|
9
|
+
"Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
@@ -363,6 +363,12 @@ class Numeric
|
|
363
363
|
to_f * 1.send(from) / 1.send(to)
|
364
364
|
end
|
365
365
|
|
366
|
+
def to_currency(options={})
|
367
|
+
unit = options.fetch(:unit, "$")
|
368
|
+
|
369
|
+
"#{unit}#{pad_precision(options.only(:precision))}"
|
370
|
+
end
|
371
|
+
|
366
372
|
def to_length(from, to)
|
367
373
|
valid_keys = [
|
368
374
|
:meter, :meters,
|
@@ -455,6 +461,12 @@ class Numeric
|
|
455
461
|
value
|
456
462
|
end
|
457
463
|
|
464
|
+
def to_percentage(options={})
|
465
|
+
unit = options.fetch(:unit, "%")
|
466
|
+
|
467
|
+
"#{pad_precision(options.only(:precision))}#{unit}"
|
468
|
+
end
|
469
|
+
|
458
470
|
def to_temperature(from, to)
|
459
471
|
valid_keys = [:celsius, :fahrenheit, :kelvin]
|
460
472
|
|
data/spec/lib/numeric_spec.rb
CHANGED
@@ -427,6 +427,32 @@ describe Numeric do
|
|
427
427
|
end
|
428
428
|
end
|
429
429
|
|
430
|
+
describe "#to_currency" do
|
431
|
+
it "to be $3.00" do
|
432
|
+
expect(3.to_currency).to eq("$3.00")
|
433
|
+
end
|
434
|
+
|
435
|
+
it "to be $3.10" do
|
436
|
+
expect(3.1.to_currency).to eq("$3.10")
|
437
|
+
end
|
438
|
+
|
439
|
+
it "to be $3.11" do
|
440
|
+
expect(3.11.to_currency).to eq("$3.11")
|
441
|
+
end
|
442
|
+
|
443
|
+
it "to be @3.11" do
|
444
|
+
expect(3.11.to_currency(unit: "@")).to eq("@3.11")
|
445
|
+
end
|
446
|
+
|
447
|
+
it "to be $3.00000" do
|
448
|
+
expect(3.to_currency(precision: 5)).to eq("$3.00000")
|
449
|
+
end
|
450
|
+
|
451
|
+
it "to be $3.11" do
|
452
|
+
expect(3.11111.to_currency).to eq("$3.11")
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
430
456
|
describe "#to_length" do
|
431
457
|
it "to be 1" do
|
432
458
|
expect(1.to_length(:millimeter, :millimeter)).to eq(1)
|
@@ -513,6 +539,32 @@ describe Numeric do
|
|
513
539
|
end
|
514
540
|
end
|
515
541
|
|
542
|
+
describe "#to_percentage" do
|
543
|
+
it "to be 3.00%" do
|
544
|
+
expect(3.to_percentage).to eq("3.00%")
|
545
|
+
end
|
546
|
+
|
547
|
+
it "to be 3.10%" do
|
548
|
+
expect(3.1.to_percentage).to eq("3.10%")
|
549
|
+
end
|
550
|
+
|
551
|
+
it "to be 3.11%" do
|
552
|
+
expect(3.11.to_percentage).to eq("3.11%")
|
553
|
+
end
|
554
|
+
|
555
|
+
it "to be 3.11@" do
|
556
|
+
expect(3.11.to_percentage(unit: "@")).to eq("3.11@")
|
557
|
+
end
|
558
|
+
|
559
|
+
it "to be 3.00000%" do
|
560
|
+
expect(3.to_percentage(precision: 5)).to eq("3.00000%")
|
561
|
+
end
|
562
|
+
|
563
|
+
it "to be 3.11%" do
|
564
|
+
expect(3.11111.to_percentage).to eq("3.11%")
|
565
|
+
end
|
566
|
+
end
|
567
|
+
|
516
568
|
describe "#to_temperature" do
|
517
569
|
it "to be 212" do
|
518
570
|
expect(100.to_temperature(:celsius, :fahrenheit)).to eq(212)
|