spacetimeid 0.1.1 → 0.2.0

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: 93090963880cbbf02e25a5296c4456bd2637c0e4
4
- data.tar.gz: bbb8634e5b39dc4e5e1f302cdff8c0feb51acef3
3
+ metadata.gz: 5fc79cd5165be1f56d4c372249430fff31c28e64
4
+ data.tar.gz: edcb65f0d8d7f0f5902dfaaa54256e57cc2f042b
5
5
  SHA512:
6
- metadata.gz: 8031c66c95bd89eb0048813ad0cc75339549be400529c6126242ebbfa91641f9377d6c221d18250d29b1a9b41129eb234cce33eb3b8be963af65c2fa142fe352
7
- data.tar.gz: 123ae0bed81d9881bc1650ed97068a2b60803dd06a3ecd92f5e047471cbcddb45a17f33407adf6c5e076d29ceae8d86df6981ffc8432596d6687d93dfaa63fb7
6
+ metadata.gz: b4abe4d9baba994033bdaf94c9825e7f207a3b676a65fa5770c061f75ba10965264ab423875ddd9ec97c4a67373c4a715a6afb5d971fa057122ba9b084aa0ec5
7
+ data.tar.gz: 9cea49cdf67f08364d9c72a30cd26d10b316384f982e295b32abce40d77de91bda789eaf29cf1b32d9c0ce98875c9b5c936d8076bd2ad9b048e78704e7755bd1
data/README.md CHANGED
@@ -24,8 +24,8 @@ id.id
24
24
  # DEFAULTS = {
25
25
  # xy_base_step: 0.01, # 0.01 degrees
26
26
  # xy_expansion: 5, # expands into a 5x5 grid recursively
27
- # ts_base_step: 600, # 10 minutes
28
- # ts_expansion: [1, 3, 2, 3, 2], # each time interval expands this many times the previous one
27
+ # ts_base_step: 3600, # 1 hour
28
+ # ts_expansion: 2, # each time interval expands this many times the previous one
29
29
  # decimals: 2
30
30
  # }
31
31
 
@@ -0,0 +1,36 @@
1
+ module InheritableAttribute
2
+
3
+ def inheritable_attr(name, options={})
4
+ instance_eval %Q{
5
+ def #{name}=(v)
6
+ @#{name} = v
7
+ end
8
+
9
+ def #{name}
10
+ return @#{name} if instance_variable_defined?(:@#{name})
11
+ @#{name} = InheritableAttribute.inherit_for(self, :#{name}, #{options})
12
+ end
13
+ }
14
+ end
15
+
16
+ def self.inherit_for(klass, name, options={})
17
+ return unless klass.superclass.respond_to?(name)
18
+
19
+ value = klass.superclass.send(name) # could be nil
20
+
21
+ return value if options[:clone] == false
22
+ Clone.(value) # this could be dynamic, allowing other inheritance strategies.
23
+ end
24
+
25
+ class Clone
26
+ # The second argument allows injecting more types.
27
+ def self.call(value, uncloneable=uncloneable())
28
+ uncloneable.each { |klass| return value if value.kind_of?(klass) }
29
+ value.clone
30
+ end
31
+
32
+ def self.uncloneable
33
+ [Symbol, TrueClass, FalseClass, NilClass]
34
+ end
35
+ end
36
+ end
data/lib/space_time_id.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  class SpaceTimeId
2
-
3
- DEFAULTS = {
4
- xy_base_step: 0.01, # 0.01 degrees
5
- xy_expansion: 5, # expands into a 5x5 grid recursively
6
- ts_base_step: 600, # 10 minutes
7
- ts_expansion: [1, 3, 2, 3, 2], # expands 3 times each interval, then 2, then 3....
2
+ extend InheritableAttribute
3
+
4
+ inheritable_attr :default_options
5
+ self.default_options = {
6
+ xy_base_step: 0.01, # 0.01 degrees
7
+ xy_expansion: 5, # expands into a 5x5 grid recursively
8
+ ts_base_step: 60 * 60, # 1 hour
9
+ ts_expansion: 2, # expands 2 times each interval
8
10
  decimals: 2
9
11
  }
10
12
 
@@ -16,10 +18,9 @@ class SpaceTimeId
16
18
  attr_accessor :level
17
19
 
18
20
  def initialize(*args)
19
- self.options = DEFAULTS.merge(args.extract_options!)
21
+ self.options = self.class.default_options.merge(args.extract_options!)
20
22
  self.interval = options.delete(:interval) || 0
21
23
  self.level = options.delete(:level) || 0
22
- options[:ts_expansion] = Array(options[:ts_expansion])
23
24
  if args.length == 1 && args.first.is_a?(String)
24
25
  initialize(*args.first.split("_").map(&:to_f), original_options)
25
26
  elsif args.length == 1 && args.first.is_a?(Array) && args.first.length == 2
@@ -119,11 +120,7 @@ class SpaceTimeId
119
120
  end
120
121
 
121
122
  def ts_step
122
- @ts_step ||= ts_step_aux(interval)
123
- end
124
-
125
- def ts_step_aux(interval)
126
- ts_base_step * ts_expansion
123
+ interval == 0 ? ts_base_step : ts_base_step * ts_expansion * interval
127
124
  end
128
125
 
129
126
  def ts_base_step
@@ -131,15 +128,7 @@ class SpaceTimeId
131
128
  end
132
129
 
133
130
  def ts_expansion
134
- ts_expansion_aux
135
- options[:ts_expansion][0..interval].reduce(:*)
136
- end
137
-
138
- def ts_expansion_aux
139
- if interval >= options[:ts_expansion].length
140
- padd = [options[:ts_expansion].last] * (options[:ts_expansion].length - interval + 1)
141
- options[:ts_expansion] += padd
142
- end
131
+ options[:ts_expansion]
143
132
  end
144
133
 
145
134
  # # # # # # # # # # # # # T I M E R E L A T I O N S # # # # # # # # # # # #
data/lib/spacetimeid.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'extract_options'
2
+ require 'inheritable_attribute'
2
3
  autoload :SpaceTimeId, 'space_time_id'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spacetimeid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Lamprianidis
@@ -33,6 +33,7 @@ files:
33
33
  - Gemfile
34
34
  - README.md
35
35
  - lib/extract_options.rb
36
+ - lib/inheritable_attribute.rb
36
37
  - lib/space_time_id.rb
37
38
  - lib/spacetimeid.rb
38
39
  homepage: https://github.com/glampr/spacetimeid