seis_ruby 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ require 'thor'
3
3
  class ::SeisRuby::Application < Thor
4
4
  require 'yaml'
5
5
  require 'fileutils'
6
+ require 'ruby_patch'
6
7
 
7
8
  COMMAND = 'seis_ruby'
8
9
  COMPLETION_FILE = File.join(ENV['HOME'], ".config", COMMAND, "completion.bash")
@@ -12,10 +13,10 @@ class ::SeisRuby::Application < Thor
12
13
  repl_hook()
13
14
 
14
15
  Pry.start(
15
- ::TOPLEVEL_BINDING,
16
+ ::SeisRuby,
16
17
  prompt: [
17
- lambda{|obj, nest_level, pry| "[#{pry.input_array.size}] SeisRuby(#{obj}):#{nest_level}> "},
18
- lambda{|obj, nest_level, pry| "[#{pry.input_array.size}] SeisRuby(#{obj}):#{nest_level}* "}])
18
+ lambda{|obj, nest_level, pry| "[#{pry.input_array.size}] #{obj}:#{nest_level}> "},
19
+ lambda{|obj, nest_level, pry| "[#{pry.input_array.size}] #{obj}:#{nest_level}* "}])
19
20
  end
20
21
 
21
22
  desc "yamlize URI", "Read data from URI, parse it and output to STDOUT in YAML format."
@@ -1,5 +1,5 @@
1
1
  module ::SeisRuby::Data::Sac::Binary::Head
2
- HEAD_FORMAT_STRING = "f70i40A8A16" + "A8"*21
2
+ HEAD_FORMAT_STRING = "f70l40A8A16" + "A8"*21
3
3
  BODY_FORMAT_STRING = "f*"
4
4
  FORMAT_STRING = HEAD_FORMAT_STRING + BODY_FORMAT_STRING
5
5
  end
@@ -61,80 +61,94 @@ module ::SeisRuby::Data::Sac::Head
61
61
  ].map.with_index.with_object({}){|(v, i), h| h[i + 1] = v}
62
62
 
63
63
  # Float
64
- CONVERTER_FROM_HEAD_FLOAT = lambda{|x|
65
- x_f = x.to_f
66
- return nil if x_f == -12345.0
67
- x_f
68
- }
69
- CONVERTER_TO_HEAD_FLOAT = lambda{|x|
70
- return -12345.0 if x.nil?
71
- x.to_f
72
- }
73
-
74
- # Integer
75
- CONVERTER_FROM_HEAD_INTEGER = lambda{|n|
76
- n_i = n.to_f.round
77
- return nil if n_i == -12345
78
- n_i
79
- }
80
- CONVERTER_TO_HEAD_INTEGER = lambda{|n|
81
- return -12345 if n.nil?
82
- n.to_f.round
83
- }
84
-
85
- # Enumerated value
86
- CONVERTER_FROM_HEAD_ENUMERATED_VALUE = lambda{|n|
87
- n_i = n.to_f.round
88
- return nil if n_i == -12345
89
- raise ArgumentError, "Enumerated value out of range: #{n.inspect}.to_f.round" unless ENUMERATED_VALUES.has_key?(n_i)
90
- ENUMERATED_VALUES.fetch(n_i)
91
- }
92
- CONVERTER_TO_HEAD_ENUMERATED_VALUE = lambda{|sym|
93
- return -12345 if sym.nil?
94
- sym_sym = sym.to_sym
95
- raise ArgumentError, "Unknown enumerated value: #{sym.inspect}.to_sym" unless ENUMERATED_VALUES.has_value?(sym_sym)
96
- ENUMERATED_VALUES.key(sym_sym)
97
- }
98
-
99
- # Logical
100
- CONVERTER_FROM_HEAD_LOGICAL = lambda{|n| n.to_f.round == 1}
101
- CONVERTER_TO_HEAD_LOGICAL = lambda{|v|
102
- if v
103
- 1
104
- else
105
- 0
64
+ module FloatConverter
65
+ UNDEFINED = -12345.0
66
+
67
+ def self.from_head(x)
68
+ x_f = x.to_f
69
+ return nil if x_f == UNDEFINED
70
+ x_f
71
+ end
72
+
73
+ def self.to_head(x)
74
+ return UNDEFINED if x.nil?
75
+ x.to_f
76
+ end
77
+ end
78
+
79
+ module IntegerConverter
80
+ UNDEFINED = -12345
81
+
82
+ def self.from_head(n)
83
+ n_i = n.to_f.round
84
+ return nil if n_i == UNDEFINED
85
+ n_i
86
+ end
87
+
88
+ def self.to_head(n)
89
+ return UNDEFINED if n.nil?
90
+ n.to_f.round
91
+ end
92
+ end
93
+
94
+ module EnumeratedValueConverter
95
+ UNDEFINED = -12345
96
+
97
+ def self.from_head(n)
98
+ n_i = n.to_f.round
99
+ return nil if n_i == UNDEFINED
100
+ raise ArgumentError, "Enumerated value out of range: #{n.inspect}.to_f.round" unless ENUMERATED_VALUES.has_key?(n_i)
101
+ ENUMERATED_VALUES.fetch(n_i)
102
+ end
103
+
104
+ def self.to_head(sym)
105
+ return UNDEFINED if sym.nil?
106
+ sym_sym = sym.to_sym
107
+ raise ArgumentError, "Unknown enumerated value: #{sym.inspect}.to_sym" unless ENUMERATED_VALUES.has_value?(sym_sym)
108
+ ENUMERATED_VALUES.key(sym_sym)
106
109
  end
107
- }
108
-
109
- # String
110
- CONVERTER_FROM_HEAD_STRING = lambda{|str|
111
- stripped_str = str.to_s.strip
112
- return nil if stripped_str == "-12345"
113
- stripped_str
114
- }
115
- CONVERTER_TO_HEAD_STRING = lambda{|str|
116
- return "-12345" if str.nil?
117
- str.to_s.strip
118
- }
119
-
120
- CONVERTERS_FROM_HEAD\
121
- = [CONVERTER_FROM_HEAD_FLOAT]*70\
122
- + [CONVERTER_FROM_HEAD_INTEGER]*15\
123
- + [CONVERTER_FROM_HEAD_ENUMERATED_VALUE]*20\
124
- + [CONVERTER_FROM_HEAD_LOGICAL]*5\
125
- + [CONVERTER_FROM_HEAD_STRING]*23
126
-
127
- CONVERTERS_TO_HEAD\
128
- = [CONVERTER_TO_HEAD_FLOAT]*70\
129
- + [CONVERTER_TO_HEAD_INTEGER]*15\
130
- + [CONVERTER_TO_HEAD_ENUMERATED_VALUE]*20\
131
- + [CONVERTER_TO_HEAD_LOGICAL]*5\
132
- + [CONVERTER_TO_HEAD_STRING]*23
133
-
134
- Field = Struct.new(:name, :converter_from_head, :converter_to_head)
110
+ end
111
+
112
+ module LogicalConverter
113
+ def self.from_head(n)
114
+ n.to_f.round == 1
115
+ end
116
+
117
+ def self.to_head(v)
118
+ if v
119
+ 1
120
+ else
121
+ 0
122
+ end
123
+ end
124
+ end
125
+
126
+ module StringConverter
127
+ UNDEFINED = '-12345'
128
+
129
+ def self.from_head(str)
130
+ stripped_str = str.to_s.strip
131
+ return nil if stripped_str == UNDEFINED
132
+ stripped_str
133
+ end
134
+
135
+ def self.to_head(str)
136
+ return UNDEFINED if str.nil?
137
+ str.to_s.strip
138
+ end
139
+ end
140
+
141
+ CONVERTERS\
142
+ = [FloatConverter]*70\
143
+ + [IntegerConverter]*15\
144
+ + [EnumeratedValueConverter]*20\
145
+ + [LogicalConverter]*5\
146
+ + [StringConverter]*23
147
+
148
+ Field = Struct.new(:name, :converter)
135
149
  FIELDS = NAMES\
136
- .zip(CONVERTERS_FROM_HEAD, CONVERTERS_TO_HEAD)\
137
- .map{|name, from, to| Field.new(name, from, to)}
150
+ .zip(CONVERTERS)\
151
+ .map{|name, converter| Field.new(name, converter)}
138
152
 
139
153
  class << self
140
154
  # @param [Array] array
@@ -144,7 +158,7 @@ module ::SeisRuby::Data::Sac::Head
144
158
  .zip(array)\
145
159
  .map{|field, val|
146
160
  begin
147
- [field.name, field.converter_from_head.call(val)]
161
+ [field.name, field.converter.from_head(val)]
148
162
  rescue ArgumentError => e
149
163
  $stderr.puts "Field name: #{field.name.inspect}"
150
164
  raise e
@@ -156,7 +170,7 @@ module ::SeisRuby::Data::Sac::Head
156
170
  # @param [Hash] hash
157
171
  # @return [Array]
158
172
  def convert_to_head(hash)
159
- FIELDS.map{|field| field.converter_to_head.call(hash[field.name])}
173
+ FIELDS.map{|field| field.converter.to_head(hash[field.name])}
160
174
  end
161
175
  end
162
176
  end
@@ -1,3 +1,3 @@
1
1
  module SeisRuby
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -12,67 +12,61 @@ class HeadTest < ::MiniTest::Unit::TestCase
12
12
 
13
13
  T = ::SeisRuby::Data::Sac::Head
14
14
 
15
- F_FROM = T::CONVERTER_FROM_HEAD_FLOAT
16
- N_FROM = T::CONVERTER_FROM_HEAD_INTEGER
17
- I_FROM = T::CONVERTER_FROM_HEAD_ENUMERATED_VALUE
18
- L_FROM = T::CONVERTER_FROM_HEAD_LOGICAL
19
- K_FROM = T::CONVERTER_FROM_HEAD_STRING
15
+ F = T::FloatConverter
16
+ N = T::IntegerConverter
17
+ I = T::EnumeratedValueConverter
18
+ L = T::LogicalConverter
19
+ K = T::StringConverter
20
20
 
21
21
  def test_convert_from_head_undefined
22
- assert_nil(F_FROM.call(-12345.0))
23
- assert_nil(N_FROM.call(-12345))
24
- assert_nil(I_FROM.call(-12345))
25
- assert_nil(K_FROM.call("-12345"))
22
+ assert_nil(F.from_head(-12345.0))
23
+ assert_nil(N.from_head(-12345))
24
+ assert_nil(I.from_head(-12345))
25
+ assert_nil(K.from_head("-12345"))
26
26
  end
27
27
 
28
28
  def test_convert_from_head_valid
29
- assert_equal(42.0, F_FROM.call(42.0))
30
- assert_equal(42, N_FROM.call(42))
31
- assert_equal(:itime, I_FROM.call(1))
32
- assert_equal(:ipostq, I_FROM.call(42))
33
- assert_equal(:iu, I_FROM.call(86))
34
- assert_equal(true, L_FROM.call(1))
35
- assert_equal(false, L_FROM.call(0))
36
- assert_equal("42", K_FROM.call("42"))
29
+ assert_equal(42.0, F.from_head(42.0))
30
+ assert_equal(42, N.from_head(42))
31
+ assert_equal(:itime, I.from_head(1))
32
+ assert_equal(:ipostq, I.from_head(42))
33
+ assert_equal(:iu, I.from_head(86))
34
+ assert_equal(true, L.from_head(1))
35
+ assert_equal(false, L.from_head(0))
36
+ assert_equal("42", K.from_head("42"))
37
37
  end
38
38
 
39
39
  def test_convert_from_head_invalid
40
- assert_raises(ArgumentError, NoMethodError){F_FROM.call(:'1.0')}
41
- assert_raises(ArgumentError, NoMethodError){N_FROM.call(:'1')}
42
- assert_raises(ArgumentError, NoMethodError){I_FROM.call(-1)}
43
- assert_raises(ArgumentError, NoMethodError){I_FROM.call(0)}
44
- assert_raises(ArgumentError, NoMethodError){I_FROM.call(87)}
45
- assert_raises(ArgumentError, NoMethodError){I_FROM.call(88)}
40
+ assert_raises(ArgumentError, NoMethodError){F.from_head(:'1.0')}
41
+ assert_raises(ArgumentError, NoMethodError){N.from_head(:'1')}
42
+ assert_raises(ArgumentError, NoMethodError){I.from_head(-1)}
43
+ assert_raises(ArgumentError, NoMethodError){I.from_head(0)}
44
+ assert_raises(ArgumentError, NoMethodError){I.from_head(87)}
45
+ assert_raises(ArgumentError, NoMethodError){I.from_head(88)}
46
46
  end
47
47
 
48
- F_TO = T::CONVERTER_TO_HEAD_FLOAT
49
- N_TO = T::CONVERTER_TO_HEAD_INTEGER
50
- I_TO = T::CONVERTER_TO_HEAD_ENUMERATED_VALUE
51
- L_TO = T::CONVERTER_TO_HEAD_LOGICAL
52
- K_TO = T::CONVERTER_TO_HEAD_STRING
53
-
54
48
  def test_convert_to_head_undefined
55
- assert_equal(-12345.0, F_TO.call(nil))
56
- assert_equal(-12345, N_TO.call(nil))
57
- assert_equal(-12345, I_TO.call(nil))
58
- assert_equal(0, L_TO.call(nil))
59
- assert_equal("-12345", K_TO.call(nil))
49
+ assert_equal(-12345.0, F.to_head(nil))
50
+ assert_equal(-12345, N.to_head(nil))
51
+ assert_equal(-12345, I.to_head(nil))
52
+ assert_equal(0, L.to_head(nil))
53
+ assert_equal("-12345", K.to_head(nil))
60
54
  end
61
55
 
62
56
  def test_convert_to_head_valid
63
- assert_equal(42.0, F_TO.call(42.0))
64
- assert_equal(42, N_TO.call(42))
65
- assert_equal(1, I_TO.call(:itime))
66
- assert_equal(42, I_TO.call(:ipostq))
67
- assert_equal(86, I_TO.call(:iu))
68
- assert_equal(1, L_TO.call(true))
69
- assert_equal(0, L_TO.call(false))
70
- assert_equal("42", K_TO.call("42"))
57
+ assert_equal(42.0, F.to_head(42.0))
58
+ assert_equal(42, N.to_head(42))
59
+ assert_equal(1, I.to_head(:itime))
60
+ assert_equal(42, I.to_head(:ipostq))
61
+ assert_equal(86, I.to_head(:iu))
62
+ assert_equal(1, L.to_head(true))
63
+ assert_equal(0, L.to_head(false))
64
+ assert_equal("42", K.to_head("42"))
71
65
  end
72
66
 
73
67
  def test_convert_to_head_invalid
74
- assert_raises(ArgumentError, NoMethodError){F_TO.call(:"42.0")}
75
- assert_raises(ArgumentError, NoMethodError){N_TO.call(:"42")}
76
- assert_raises(ArgumentError, NoMethodError){I_TO.call(:this_is_not_exist)}
68
+ assert_raises(ArgumentError, NoMethodError){F.to_head(:"42.0")}
69
+ assert_raises(ArgumentError, NoMethodError){N.to_head(:"42")}
70
+ assert_raises(ArgumentError, NoMethodError){I.to_head(:this_is_not_exist)}
77
71
  end
78
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seis_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-15 00:00:00.000000000 Z
12
+ date: 2012-12-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: watchr
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &2154938040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '0.7'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: '0.7'
24
+ version_requirements: *2154938040
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: thor
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &2154937560 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ~>
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: '0.15'
38
33
  type: :runtime
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: '0.15'
35
+ version_requirements: *2154937560
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: mechanize
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &2154937100 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ~>
@@ -53,15 +43,10 @@ dependencies:
53
43
  version: '2.5'
54
44
  type: :runtime
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '2.5'
46
+ version_requirements: *2154937100
62
47
  - !ruby/object:Gem::Dependency
63
48
  name: pry
64
- requirement: !ruby/object:Gem::Requirement
49
+ requirement: &2154936640 !ruby/object:Gem::Requirement
65
50
  none: false
66
51
  requirements:
67
52
  - - ! '>='
@@ -69,15 +54,10 @@ dependencies:
69
54
  version: 0.9.10
70
55
  type: :runtime
71
56
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: 0.9.10
57
+ version_requirements: *2154936640
78
58
  - !ruby/object:Gem::Dependency
79
59
  name: ruby_patch
80
- requirement: !ruby/object:Gem::Requirement
60
+ requirement: &2154936180 !ruby/object:Gem::Requirement
81
61
  none: false
82
62
  requirements:
83
63
  - - ! '>='
@@ -85,12 +65,7 @@ dependencies:
85
65
  version: 1.1.0
86
66
  type: :development
87
67
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: 1.1.0
68
+ version_requirements: *2154936180
94
69
  description: Ruby library for earthquake science.
95
70
  email:
96
71
  executables:
@@ -166,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
141
  version: '0'
167
142
  requirements: []
168
143
  rubyforge_project:
169
- rubygems_version: 1.8.23
144
+ rubygems_version: 1.8.15
170
145
  signing_key:
171
146
  specification_version: 3
172
147
  summary: Ruby library for earthquake science.