opium 1.1.7 → 1.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5e7b88ef5afc4f4bb9899031776ef2947c8043c
4
- data.tar.gz: 6d53e858f5ebfc9b448e7a9d98344ca4b2c30fc9
3
+ metadata.gz: 13bf8b7b6c86a85e42ef4efd054179cd07ec22d9
4
+ data.tar.gz: 328524b3e01c8ade53ffa6a2e8d7d306dc3b90f8
5
5
  SHA512:
6
- metadata.gz: 85938d9d4858f607cc93c87b4f212cef1e5812f1bce17b6fc9a18b9f268dc50755ea3f24375ed07dc5e038a96bcb99c79198698a0e09b6da0fa21d6f074f6b14
7
- data.tar.gz: 25eec023d9656b0a965da55b39c101a1b3fc551e419784cee75798bd07cb77809b0037dbbf262b3e418fa817415560951ce276fe2426e5fa4fee5660fd1c4ace
6
+ metadata.gz: a950bcccc7f3d52d077f983701dc15c5e93136fe9682022f79a40953efb1f0f968ee827834983b1d6e75324c49d865e69bdbf8426e388a700da62301b9c2ac82
7
+ data.tar.gz: 10f6b86273cf753b873a667b31b3f30993682a177fb6ed121b0f1e8aa80156ac8dd07494db380099288c965c09c0ad8bd094806e7bab3bb8640c7489b3b14c45
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.1.8
2
+ ### Resolved Issues
3
+ - #41: Boolean and GeoPoint are now namespaced within Opium. The ModelGenerator also maps attribute types for these classes.
4
+ - #42: GeoPoint now is capable of converting from String in `.to_ruby`
5
+
1
6
  ## 1.1.7
2
7
  ### Resolved Issues
3
8
  - #40: `Opium::File.to_ruby` now correctly handles JSON string representation of hashes.
@@ -9,6 +9,10 @@ module Rails
9
9
  'DateTime'
10
10
  when 'file'
11
11
  'Opium::File'
12
+ when 'geopoint', 'geo_point'
13
+ 'Opium::GeoPoint'
14
+ when 'boolean', 'bool'
15
+ 'Opium::Boolean'
12
16
  else
13
17
  type.to_s.camelcase
14
18
  end
@@ -4,7 +4,7 @@ class Array
4
4
  end
5
5
 
6
6
  def to_geo_point
7
- return GeoPoint.new(self) if self.size == 2
7
+ return Opium::GeoPoint.new(self) if self.size == 2
8
8
  fail ArgumentError, %(invalid value for GeoPoint: "#{ self.inspect }"), caller
9
9
  end
10
10
  end
@@ -1,4 +1,4 @@
1
- module Boolean
1
+ module Opium::Boolean
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  module ClassMethods
@@ -1,5 +1,5 @@
1
1
  class FalseClass
2
- include Boolean
2
+ include Opium::Boolean
3
3
 
4
4
  def to_bool
5
5
  self
@@ -1,37 +1,41 @@
1
- class GeoPoint
2
- def initialize( value )
3
- self.latitude, self.longitude = *
4
- case value
5
- when Hash
6
- [value[:latitude] || value['latitude'], value[:longitude] || value['longitude']]
7
- when Array
8
- [value.first, value.last]
9
- else
10
- raise ArgumentError.new( "invalid value for GeoPoint: \"#{value}\"" )
11
- end
12
- end
1
+ module Opium
2
+ class GeoPoint
3
+ def initialize( value )
4
+ self.latitude, self.longitude = *
5
+ case value
6
+ when Hash
7
+ [value[:latitude] || value['latitude'], value[:longitude] || value['longitude']]
8
+ when Array
9
+ [value.first, value.last]
10
+ when /^[+-]?\d+(\.\d+)?\s*,\s*[+-]?\d+(\.\d+)?$/
11
+ value.split(',').map {|c| c.to_f}
12
+ else
13
+ raise ArgumentError.new( "invalid value for GeoPoint: \"#{ value }\"" )
14
+ end
15
+ end
13
16
 
14
- attr_accessor :latitude, :longitude
17
+ attr_accessor :latitude, :longitude
15
18
 
16
- def to_geo_point
17
- self
18
- end
19
+ def to_geo_point
20
+ self
21
+ end
19
22
 
20
- def to_parse
21
- { "__type" => "GeoPoint", "latitude" => self.latitude, "longitude" => self.longitude }
22
- end
23
+ def to_parse
24
+ { "__type" => "GeoPoint", "latitude" => self.latitude, "longitude" => self.longitude }
25
+ end
23
26
 
24
- def to_s
25
- "#{self.latitude},#{self.longitude}"
26
- end
27
-
28
- class << self
29
- def to_ruby(object)
30
- object.to_geo_point unless object.nil?
27
+ def to_s
28
+ "#{ self.latitude },#{ self.longitude }"
31
29
  end
30
+
31
+ class << self
32
+ def to_ruby(object)
33
+ object.to_geo_point unless object.nil?
34
+ end
32
35
 
33
- def to_parse(object)
34
- object.to_geo_point.to_parse
36
+ def to_parse(object)
37
+ object.to_geo_point.to_parse
38
+ end
35
39
  end
36
40
  end
37
41
  end
@@ -1,6 +1,6 @@
1
1
  class Hash
2
2
  def to_geo_point
3
- return GeoPoint.new(self) if [:latitude, :longitude].all? {|required| self.key? required}
3
+ return Opium::GeoPoint.new(self) if [:latitude, :longitude].all? {|required| self.key? required}
4
4
  raise ArgumentError.new( "invalid value for GeoPoint: \"#{self}\"" )
5
5
  end
6
6
 
@@ -6,8 +6,7 @@ class ::String
6
6
  end
7
7
 
8
8
  def to_geo_point
9
- return GeoPoint.new( self.split(',').map {|c| c.to_f} ) if self =~ /^[+-]?\d+(\.\d+)?\s*,\s*[+-]?\d+(\.\d+)?$/
10
- raise ArgumentError.new("invalid value for GeoPoint: \"#{self}\"")
9
+ Opium::GeoPoint.new( self )
11
10
  end
12
11
 
13
12
  class << self
@@ -1,5 +1,5 @@
1
1
  class TrueClass
2
- include Boolean
2
+ include Opium::Boolean
3
3
 
4
4
  def to_bool
5
5
  self
data/lib/opium/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Opium
2
- VERSION = "1.1.7"
2
+ VERSION = "1.1.8"
3
3
  end
@@ -5,7 +5,7 @@ describe Array do
5
5
 
6
6
  describe 'instance' do
7
7
  describe ':to_parse' do
8
- subject { [GeoPoint.new( latitude: 33.33, longitude: -117.117 )] }
8
+ subject { [Opium::GeoPoint.new( latitude: 33.33, longitude: -117.117 )] }
9
9
 
10
10
  it 'should call :to_parse on each element' do
11
11
  subject.to_parse.tap do |result|
@@ -18,7 +18,7 @@ describe Array do
18
18
  describe 'with two values' do
19
19
  subject { [33.33, -117.117] }
20
20
 
21
- it { subject.to_geo_point.should be_a_kind_of(GeoPoint) }
21
+ it { subject.to_geo_point.should be_a_kind_of( Opium::GeoPoint ) }
22
22
  it 'should have the expected latitude and longitude' do
23
23
  subject.to_geo_point.latitude.should == 33.33
24
24
  subject.to_geo_point.longitude.should == -117.117
@@ -1,26 +1,40 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Boolean do
4
- subject { Boolean }
5
- it { should respond_to(:to_ruby).with(1).argument }
6
- it { should respond_to(:to_parse).with(1).argument }
3
+ describe Opium::Boolean do
4
+ subject { Opium::Boolean }
7
5
 
8
- it ":to_ruby(object) should convert object to a boolean" do
9
- ["true", "false", true, false, 1.0, 1, 0.0, 0].each do |value|
10
- subject.to_ruby(value).should be_a_kind_of(Boolean)
11
- subject.to_ruby(value).should == value.to_bool
6
+ it { is_expected.to respond_to( :to_ruby, :to_parse ).with( 1 ).argument }
7
+
8
+ describe '.to_ruby' do
9
+ let(:result) { subject.to_ruby( convert_from ) }
10
+
11
+ context 'with valid boolean objects' do
12
+ ["true", "false", true, false, 1.0, 1, 0.0, 0].each do |value|
13
+ let(:convert_from) { value }
14
+
15
+ it { expect { result }.to_not raise_exception }
16
+ it { expect( result ).to be_a described_class }
17
+ it { expect( result ).to eq convert_from.to_bool }
18
+ end
12
19
  end
13
20
  end
14
21
 
15
- it ":to_parse(object) should convert object to a boolean" do
16
- ["true", "false", true, false, 1.0, 1, 0.0, 0].each do |value|
17
- subject.to_parse(value).should be_a_kind_of(Boolean)
18
- subject.to_parse(value).should == value.to_bool
22
+ describe '.to_parse' do
23
+ let(:result) { subject.to_parse( convert_from ) }
24
+
25
+ context 'with valid boolean objects' do
26
+ ["true", "false", true, false, 1.0, 1, 0.0, 0].each do |value|
27
+ let(:convert_from) { value }
28
+
29
+ it { expect { result }.to_not raise_exception }
30
+ it { expect( result ).to be_a described_class }
31
+ it { expect( result ).to eq convert_from.to_bool }
32
+ end
19
33
  end
20
34
  end
21
35
 
22
36
  describe "instance" do
23
- subject { Class.new { include Boolean }.new }
37
+ subject { Class.new { include Opium::Boolean }.new }
24
38
  it { should respond_to(:to_ruby, :to_parse).with(0).arguments }
25
39
  its(:to_ruby) { should == subject }
26
40
  its(:to_parse) { should == subject }
@@ -1,35 +1,76 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe GeoPoint do
4
- subject { GeoPoint }
3
+ describe Opium::GeoPoint do
4
+ subject { described_class }
5
5
 
6
- it { should respond_to(:to_ruby, :to_parse).with(1).argument }
6
+ it { is_expected.to respond_to( :to_ruby, :to_parse ).with( 1 ).argument }
7
7
 
8
- it ":to_ruby(object) should create a GeoPoint" do
9
- [ "33.33, -117.117", {latitude: 33.33, longitude: -117.117}, [33.33, -117.117], GeoPoint.new( [33.33, -117.117] ) ].each do |value|
10
- result = subject.to_ruby( value )
11
- result.should be_a_kind_of(GeoPoint)
12
- result.latitude.should == 33.33
13
- result.longitude.should == -117.117
8
+ describe '.to_ruby' do
9
+ let(:result) { subject.to_ruby( convert_from ) }
10
+
11
+ context 'with valid object representations' do
12
+ [ "33.33, -117.117", {latitude: 33.33, longitude: -117.117}, [33.33, -117.117], described_class.new( [33.33, -117.117] ) ].each do |value|
13
+ let(:convert_from) { value }
14
+ it { expect { result }.to_not raise_exception }
15
+ it { expect( result ).to be_a described_class }
16
+ it( 'sets the proper latitude' ) { expect( result.latitude ).to eq 33.33 }
17
+ it( 'sets the proper longitude' ) { expect( result.longitude ).to eq -117.117 }
18
+ end
19
+ end
20
+
21
+ context 'with invalid object representations' do
22
+ [ "malformed", {latitude: 0, bad_key: :unknown}, [0], 1, 1.0, true, false ].each do |value|
23
+ let(:convert_from) { value }
24
+ it { expect { subject.to_ruby( value ) }.to raise_exception }
25
+ end
14
26
  end
15
27
  end
16
28
 
17
- it ":to_ruby(bad_data) should raise exception" do
18
- [ "malformed", {latitude: 0, bad_key: :unknown}, [0], 1, 1.0, true, false ].each do |value|
19
- expect { subject.to_ruby( value ) }.to raise_exception
29
+ describe '.to_parse' do
30
+ let(:result) { subject.to_parse( convert_from ) }
31
+ let(:expected) { { "__type" => "GeoPoint", "latitude" => 33.33, "longitude" => -117.117 } }
32
+
33
+ context 'with valid object representations' do
34
+ [ "33.33, -117.117", {latitude: 33.33, longitude: -117.117}, [33.33, -117.117], described_class.new( [33.33, -117.117] ) ].each do |value|
35
+ let(:convert_from) { value }
36
+
37
+ it { expect { result }.to_not raise_exception }
38
+ it { expect( result ).to eq expected }
39
+ end
20
40
  end
21
41
  end
22
42
 
23
- it ":to_parse(object) should ensure a geo_point and make into parse object" do
24
- [ "33.33, -117.117", {latitude: 33.33, longitude: -117.117}, [33.33, -117.117], GeoPoint.new( [33.33, -117.117] ) ].each do |value|
25
- result = subject.to_parse( value )
26
- result.should == { "__type" => "GeoPoint", "latitude" => 33.33, "longitude" => -117.117 }
27
- end
43
+ describe '#initialize' do
44
+ let(:result) { described_class.new( initialize_from ) }
45
+
46
+ context 'with an array value' do
47
+ let(:initialize_from) { [ 33.33, -117.117 ] }
48
+
49
+ it { expect { result }.to_not raise_exception }
50
+ end
51
+
52
+ context 'with a hash value' do
53
+ let(:initialize_from) { { latitude: 33.33, longitude: -117.117 } }
54
+
55
+ it { expect { result }.to_not raise_exception }
56
+ end
57
+
58
+ context 'with a string value' do
59
+ let(:initialize_from) { "33.33, -117.117" }
60
+
61
+ it { expect { result }.to_not raise_exception }
62
+ end
63
+
64
+ context 'with anything else' do
65
+ let(:initialize_from) { 42 }
66
+
67
+ it { expect { result }.to raise_exception }
68
+ end
28
69
  end
29
70
 
30
71
  describe "instance" do
31
72
  describe "with an array value" do
32
- subject { GeoPoint.new [33.33, -117.117] }
73
+ subject { described_class.new [33.33, -117.117] }
33
74
  it { should respond_to(:latitude, :longitude, :to_geo_point, :to_parse).with(0).arguments }
34
75
  it { should respond_to(:latitude=, :longitude=).with(1).argument }
35
76
 
@@ -41,7 +82,7 @@ describe GeoPoint do
41
82
  end
42
83
 
43
84
  describe "with a hash value" do
44
- subject { GeoPoint.new latitude: 33.33, longitude: -117.117 }
85
+ subject { described_class.new latitude: 33.33, longitude: -117.117 }
45
86
  it { should respond_to(:latitude, :longitude, :to_geo_point, :to_parse).with(0).arguments }
46
87
  it { should respond_to(:latitude=, :longitude=).with(1).argument }
47
88
 
@@ -5,7 +5,7 @@ describe Hash do
5
5
 
6
6
  describe "instance" do
7
7
  describe ":to_parse" do
8
- subject { { 'foo' => GeoPoint.new( latitude: 33.33, longitude: -117.117 ) } }
8
+ subject { { 'foo' => Opium::GeoPoint.new( latitude: 33.33, longitude: -117.117 ) } }
9
9
 
10
10
  it 'should return a new hash, with :to_parse called on each value' do
11
11
  subject.to_parse.tap do |result|
@@ -18,7 +18,7 @@ describe Hash do
18
18
  describe ":to_geo_point" do
19
19
  subject { { latitude: 33.33, longitude: -117.117 } }
20
20
 
21
- it { subject.to_geo_point.should be_a_kind_of(GeoPoint) }
21
+ it { subject.to_geo_point.should be_a_kind_of( Opium::GeoPoint ) }
22
22
  it "should have the expected latitude and longitude" do
23
23
  subject.to_geo_point.latitude.should == 33.33
24
24
  subject.to_geo_point.longitude.should == -117.117
@@ -35,14 +35,14 @@ describe String do
35
35
 
36
36
  it "should be able to convert various values to true" do
37
37
  %w[true t yes y 1].each do |value|
38
- value.to_bool.should be_a_kind_of( Boolean )
38
+ value.to_bool.should be_a_kind_of( Opium::Boolean )
39
39
  value.to_bool.should == true
40
40
  end
41
41
  end
42
42
 
43
43
  it "should be able to convert various values to false" do
44
44
  [""] + %w[false f no n 0].each do |value|
45
- value.to_bool.should be_a_kind_of( Boolean )
45
+ value.to_bool.should be_a_kind_of( Opium::Boolean )
46
46
  value.to_bool.should == false
47
47
  end
48
48
  end
@@ -53,7 +53,7 @@ describe String do
53
53
 
54
54
  it ":to_geo_point should be able to convert a 'lat, lng' value" do
55
55
  result = "33.33, -117.117".to_geo_point
56
- result.should be_a_kind_of(GeoPoint)
56
+ result.should be_a_kind_of( Opium::GeoPoint )
57
57
  result.latitude.should == 33.33
58
58
  result.longitude.should == -117.117
59
59
  end
@@ -6,7 +6,7 @@ describe Opium::Model do
6
6
  include Opium::Model
7
7
  field :name, type: String
8
8
  field :occurred_at, type: DateTime
9
- field :severe, type: Boolean
9
+ field :severe, type: Opium::Boolean
10
10
  end )
11
11
  end
12
12
 
@@ -197,7 +197,7 @@ describe Opium::User do
197
197
  context 'within a subclass' do
198
198
  before do
199
199
  stub_const( 'SpecialUser', Class.new(Opium::User) do
200
- field :has_web_access, type: Boolean
200
+ field :has_web_access, type: Opium::Boolean
201
201
  end )
202
202
  end
203
203
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opium
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.7
4
+ version: 1.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Bowers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-30 00:00:00.000000000 Z
11
+ date: 2015-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler