open_location_code 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82d946e05ee8c635c1aea25b7c11bf8309d195a2
4
- data.tar.gz: 370f96971c64f1384c387a913ee5494f49e77b18
3
+ metadata.gz: 071697a22f6df46767823f06f9921bf3b3243907
4
+ data.tar.gz: ab25d5730ecf2bb1358debcbc79fcdd84f0610e7
5
5
  SHA512:
6
- metadata.gz: ddc3ec4f227802e24bdd6072055c6987771b6ded212f13ab70e62e7b351e5c1b16e60d67cd47314ad7625ff10e24167d6ee431d616059dda89dbb3719650a393
7
- data.tar.gz: 24affbebbf437f9dedf76472ecfda8641345c63709406134fcd8dd007cb88573d2b8ee9ff04431595d8ad16f51188f28275ef7e445b5abfedf57ebc818ab01f3
6
+ metadata.gz: 3c82c18998888801b588ff27d292a15343f93c264c39efce46907dedac5ee54415c189da5da0591bf834cf4950dc71a4bf2053a0789997d8edf05976b40b5c73
7
+ data.tar.gz: b00860bd8fbf620b1a6593901101e47d59a3e93eac58eb4c4b296bf0502eeb0bbe7e9bda43737e670d3ac25c94a66661f0b871df147aa07d147184b8dc114c46
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.swp
11
+ *.gem
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015 Jiren
3
+ Copyright (c) 2015 Jiren[jirenpatel@gmail.com]
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # OpenLocationCode
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/open_location_code`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Open Location Codes are a way of encoding location into a form that is
4
+ easier to use than latitude and longitude.
4
5
 
5
6
  Ref: https://github.com/google/open-location-code
6
7
 
@@ -10,7 +11,7 @@ Ref: https://github.com/google/open-location-code
10
11
  Add this line to your application's Gemfile:
11
12
 
12
13
  ```ruby
13
- gem 'open_location_code', github: 'jiren/open_location_code'
14
+ gem 'open_location_code'
14
15
  ```
15
16
 
16
17
  And then execute:
@@ -27,10 +28,56 @@ Or install it yourself as:
27
28
  code = OpenLocationCode.encode(47.365590, 8.524997) # 8FVC9G8F+6X
28
29
  code = OpenLocationCode.encode(47.365590, 8.524997, 12) #8FVC9G8F+6XQH
29
30
 
30
- code_area = OpenLocationCode.decode(code)
31
+ code_area = OpenLocationCode.decode('8FVC9G8F+6XQH')
31
32
  # #<OpenLocationCode::CodeArea:0x007fe7eb050110 @latitude_lo=47.36557499999997, @longitude_lo=8.524968750000008, @latitude_hi=47.36557499999997, @longitude_hi=8.52500000000001, @code_length=12, @latitude_center=47.36557499999997, @longitude_center=8.52498437500001>
32
33
  ```
33
34
 
35
+ ## Rails
36
+
37
+ Include `Olc` module and call `has_olc` method
38
+
39
+ `has_olc` has default options `{ field: 'open_location_code', latitude: 'latitude', longitude: 'longitude', code_length: 10 }`
40
+
41
+
42
+ - ActiveRecord
43
+
44
+ Generate migration to add latitude, longitude and open_location_code fields.
45
+
46
+ ```
47
+ rails g migration add_olc_fields_to_events latitude:float longitude:float open_location_code:string
48
+ ```
49
+
50
+ In Model:
51
+
52
+ ```
53
+ class Event < < ActiveRecord::Base
54
+ include Olc
55
+
56
+ has_olc
57
+
58
+ # If fields not same as default options
59
+ has_olc(field: 'olc', latitude: 'lat', longitude: 'lng', code_length: 10)
60
+ end
61
+ ```
62
+
63
+ - Mongoid
64
+
65
+ `has_olc` will define field based on passed options[:field] value.
66
+
67
+ ```
68
+ class Event
69
+ include Mongoid::Document
70
+ include Olc
71
+
72
+ field :latitude, type: Float
73
+ field :longitude, type: Float
74
+
75
+ has_olc
76
+ end
77
+ ```
78
+
79
+
80
+
34
81
  ## Development
35
82
 
36
83
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -39,7 +86,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
39
86
 
40
87
  ## Contributing
41
88
 
42
- 1. Fork it ( https://github.com/[my-github-username]/open_location_code/fork )
89
+ 1. Fork it ( https://github.com/jiren/open_location_code )
43
90
  2. Create your feature branch (`git checkout -b my-new-feature`)
44
91
  3. Commit your changes (`git commit -am 'Add some feature'`)
45
92
  4. Push to the branch (`git push origin my-new-feature`)
@@ -107,3 +107,8 @@ module OpenLocationCode
107
107
  end
108
108
 
109
109
  end
110
+
111
+ if defined?(Rails)
112
+ require 'rails/olc'
113
+ end
114
+
@@ -2,5 +2,5 @@ module OpenLocationCode
2
2
  #
3
3
  # Version of lib
4
4
  #
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
@@ -0,0 +1,67 @@
1
+ module Olc
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+
6
+ # Default Olc options
7
+ OLC_OPTIONS = {
8
+ field: 'open_location_code',
9
+ latitude: 'latitude',
10
+ longitude: 'longitude',
11
+ code_length: 10
12
+ }
13
+
14
+ #
15
+ # Define before_save callback to generate olc code.
16
+ #
17
+ # @example
18
+ #
19
+ # has_olc
20
+ # # or
21
+ # has_olc(field: 'olc', latitude: 'lat', longitude: 'lng', code_length: 10)
22
+ #
23
+ #
24
+ # @params [Hash] options
25
+ # Default options
26
+ # {
27
+ # field: 'open_location_code',
28
+ # latitude: 'latitude',
29
+ # longitude: 'longitude',
30
+ # code_length: 10
31
+ # }
32
+ #
33
+ def has_olc(options = {})
34
+ options = OLC_OPTIONS.merge(options)
35
+
36
+ if defined?(Mongoid)
37
+ field options[:field], type: String
38
+ end
39
+
40
+ before_save do |obj|
41
+ lat_field = options[:latitude]
42
+ lng_field = options[:longitude]
43
+
44
+ changed_attrs = obj.changed_attributes
45
+
46
+ if changed_attrs.key?(lat_field) || changed_attrs.key?(lng_field)
47
+ if obj[lat_field] && obj[lng_field]
48
+ obj[options[:field]] = obj.olc_encode(options[:code_length])
49
+ else
50
+ obj[options[:field]] = nil
51
+ end
52
+ end
53
+ end
54
+
55
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
56
+ def olc_encode(code_length = nil)
57
+ OpenLocationCode.encode(#{options[:latitude]}, #{options[:longitude]}, code_length)
58
+ end
59
+
60
+ def olc_decode
61
+ OpenLocationCode.decode(#{options[:field]})
62
+ end
63
+ RUBY
64
+ end
65
+ end
66
+
67
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_location_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jiren
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-29 00:00:00.000000000 Z
11
+ date: 2015-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -62,6 +62,7 @@ files:
62
62
  - lib/open_location_code/decoder.rb
63
63
  - lib/open_location_code/encoder.rb
64
64
  - lib/open_location_code/version.rb
65
+ - lib/rails/olc.rb
65
66
  - open_location_code.gemspec
66
67
  homepage: https://github.com/jiren/open_location_code
67
68
  licenses: