hyperion-api 0.0.1.alpha4 → 0.0.1.alpha5

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.
@@ -23,8 +23,9 @@ module Hyperion
23
23
  # Assigns the datastore within the given block
24
24
  def with_datastore(name, opts={})
25
25
  self.datastore = new_datastore(name, opts)
26
- yield
26
+ result = yield
27
27
  self.datastore = nil
28
+ result
28
29
  end
29
30
 
30
31
  def new_datastore(name, opts={})
@@ -167,7 +168,7 @@ module Hyperion
167
168
  def format_record(record)
168
169
  if record
169
170
  record = record.reduce({}) do |new_record, (key, value)|
170
- new_record[snake_case(key.to_s).to_sym] = value
171
+ new_record[Util.snake_case(key.to_s).to_sym] = value
171
172
  new_record
172
173
  end
173
174
  record[:kind] = format_kind(record[:kind])
@@ -176,16 +177,11 @@ module Hyperion
176
177
  end
177
178
 
178
179
  def format_kind(kind)
179
- snake_case(kind.to_s)
180
+ Util.snake_case(kind.to_s)
180
181
  end
181
182
 
182
183
  def format_field(field)
183
- snake_case(field.to_s).to_sym
184
- end
185
-
186
- def snake_case(str)
187
- separate_camel_humps = str.gsub(/([a-z0-9])([A-Z])/, '\1 \2').downcase
188
- separate_camel_humps.gsub(/[ |\-]/, '_')
184
+ Util.snake_case(field.to_s).to_sym
189
185
  end
190
186
 
191
187
  end
@@ -4,13 +4,40 @@ module Hyperion
4
4
  class << self
5
5
 
6
6
  def camel_case(str)
7
- str.gsub(/[_| |\-][A-Za-z]/) { |a| a[1..-1].upcase }
7
+ cameled = str.gsub(/[_| |\-][A-Za-z]/) { |a| a[1..-1].upcase } if str
8
+ uncapitalize(cameled)
8
9
  end
9
10
 
10
11
  def class_name(str)
11
- cameled = camel_case(str)
12
- cameled[0] = cameled[0...1].capitalize
13
- cameled
12
+ capitalize(camel_case(str))
13
+ end
14
+
15
+ def snake_case(str)
16
+ str.gsub(/([a-z0-9])([A-Z])/, '\1 \2').downcase.gsub(/[ |\-]/, '_') if str
17
+ end
18
+
19
+ def capitalize(str)
20
+ do_to_first(str) do |first_letter|
21
+ first_letter.upcase
22
+ end
23
+ end
24
+
25
+ def uncapitalize(str)
26
+ do_to_first(str) do |first_letter|
27
+ first_letter.downcase
28
+ end
29
+ end
30
+
31
+ def do_to_first(str)
32
+ if str
33
+ first = yield(str[0, 1])
34
+ if str.length > 1
35
+ last = str[1..-1]
36
+ first + last
37
+ else
38
+ first
39
+ end
40
+ end
14
41
  end
15
42
 
16
43
  end
@@ -12,6 +12,12 @@ describe Hyperion::API do
12
12
  it 'will throw an error if the datastore is called before assignment' do
13
13
  expect{ subject.datastore }.to raise_error
14
14
  end
15
+
16
+ it 'assigns datastore and returns the result' do
17
+ api.with_datastore(:memory) do
18
+ :return
19
+ end.should == :return
20
+ end
15
21
  end
16
22
 
17
23
  context 'factory' do
@@ -15,6 +15,25 @@ describe Hyperion::Util do
15
15
  util.camel_case('with spaces').should == 'withSpaces'
16
16
  util.camel_case('with-dash').should == 'withDash'
17
17
  util.camel_case('starting Capital').should == 'startingCapital'
18
+ util.camel_case('StartingCapital').should == 'startingCapital'
19
+ util.camel_case('O').should == 'o'
20
+ util.camel_case('').should == ''
21
+ util.camel_case(nil).should == nil
22
+ end
23
+
24
+ it 'snake cases words' do
25
+ util.snake_case('fake_ds').should == 'fake_ds'
26
+ util.snake_case('defaultSceneName').should == 'default_scene_name'
27
+ util.snake_case('set defaultSceneName').should == 'set_default_scene_name'
28
+ util.snake_case('class_name').should == 'class_name'
29
+ util.snake_case('once_upon_a_time').should == 'once_upon_a_time'
30
+ util.snake_case('with spaces').should == 'with_spaces'
31
+ util.snake_case('with-dash').should == 'with_dash'
32
+ util.snake_case('starting Capital').should == 'starting_capital'
33
+ util.snake_case('StartingCapital').should == 'starting_capital'
34
+ util.snake_case('O').should == 'o'
35
+ util.snake_case('').should == ''
36
+ util.snake_case(nil).should == nil
18
37
  end
19
38
 
20
39
  it 'converts to class name' do
@@ -26,5 +45,10 @@ describe Hyperion::Util do
26
45
  util.class_name('with spaces').should == 'WithSpaces'
27
46
  util.class_name('with-dash').should == 'WithDash'
28
47
  util.class_name('starting Capital').should == 'StartingCapital'
48
+ util.class_name('StartingCapital').should == 'StartingCapital'
49
+ util.class_name('S').should == 'S'
50
+ util.class_name('s').should == 'S'
51
+ util.class_name('').should == ''
52
+ util.class_name(nil).should == nil
29
53
  end
30
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperion-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha4
4
+ version: 0.0.1.alpha5
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors: