tworgy-rails-ext 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -1
- data/lib/tworgy/rails_ext.rb +63 -0
- data/lib/tworgy/recursively.rb +32 -0
- data/spec/spec_helper.rb +7 -1
- data/spec/tworgy/rails_ext_spec.rb +1 -4
- data/spec/tworgy/recursively_spec.rb +19 -0
- data/tworgy-rails-ext.gemspec +7 -2
- metadata +6 -1
data/VERSION.yml
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
class String
|
2
|
+
def /(str_to_join)
|
3
|
+
File.join(self, str_to_join)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class Object
|
8
|
+
def pms
|
9
|
+
public_methods.sort.join(' ')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Time
|
14
|
+
def to_short
|
15
|
+
to_s('')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
if defined?(ActiveRecord)
|
20
|
+
|
21
|
+
class ActiveRecord::Base
|
22
|
+
def helpers
|
23
|
+
ActionController::Base.helpers
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.dump_to_yaml(dir = nil)
|
27
|
+
dir = RAILS_ROOT + (dir || "/db/backup/")
|
28
|
+
FileUtils.mkdir_p(dir)
|
29
|
+
file = dir + "#{self.table_name}.yml"
|
30
|
+
|
31
|
+
ActiveRecord::Base.transaction do
|
32
|
+
File.open(file, 'w+') do |f|
|
33
|
+
YAML.dump self.find(:all, :order => 'id').collect(&:attributes), f
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.load_from_yaml(dir = nil)
|
39
|
+
dir = RAILS_ROOT + (dir || "/db/backup/")
|
40
|
+
file = dir + "#{self.table_name}.yml"
|
41
|
+
|
42
|
+
ActiveRecord::Base.transaction do
|
43
|
+
self.delete_all
|
44
|
+
YAML.load_file(file).each do |fixture|
|
45
|
+
ActiveRecord::Base.connection.execute "INSERT INTO #{self.table_name} (#{fixture.keys.join(",")}) VALUES (#{fixture.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(",")})", 'Fixture Insert'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.to_fixture
|
51
|
+
write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT),
|
52
|
+
self.find(:all).inject({}) { |hsh, record|
|
53
|
+
hsh.merge("record_#{record.id}" => record.attributes)
|
54
|
+
}.to_yaml)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.write_file(path, content)
|
58
|
+
f = File.new(path, "w+")
|
59
|
+
f.puts content
|
60
|
+
f.close
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Array
|
2
|
+
def recursively(&block)
|
3
|
+
map do |item|
|
4
|
+
if item.is_a?(self.class)
|
5
|
+
item.recursively(&block)
|
6
|
+
else
|
7
|
+
yield item
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def recursively!(&block)
|
13
|
+
replace(recursively(&block))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Hash
|
18
|
+
def recursively(&block)
|
19
|
+
inject({}) do |hash, (key, value)|
|
20
|
+
if value.is_a?(Hash)
|
21
|
+
hash[key] = value.recursively(&block)
|
22
|
+
else
|
23
|
+
hash[key] = value
|
24
|
+
end
|
25
|
+
yield hash
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def recursively!(&block)
|
30
|
+
replace(recursively(&block))
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
require 'spec'
|
2
2
|
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'rails'
|
5
|
+
gem 'activesupport'
|
6
|
+
|
3
7
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
8
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
-
|
9
|
+
|
10
|
+
require 'tworgy/rails_ext'
|
11
|
+
require 'tworgy/recursively'
|
6
12
|
|
7
13
|
Spec::Runner.configure do |config|
|
8
14
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "recursively" do
|
4
|
+
it 'should turn the items in the array to strings' do
|
5
|
+
[1, 2, [3, 4]].recursively(&:to_s).should == ['1', '2', ['3', '4']]
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should turn the items in the hash to strings (except leaves)' do
|
9
|
+
{:a => {:b => :c}}.recursively do |ob|
|
10
|
+
if ob.is_a?(Hash)
|
11
|
+
ob.keys.each do |k|
|
12
|
+
ob[k.to_s] = ob[k]
|
13
|
+
ob.delete(k)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
ob
|
17
|
+
end.should == {'a' => {'b' => :c}}
|
18
|
+
end
|
19
|
+
end
|
data/tworgy-rails-ext.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{tworgy-rails-ext}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mat Holroyd"]
|
@@ -23,7 +23,11 @@ Gem::Specification.new do |s|
|
|
23
23
|
"Rakefile",
|
24
24
|
"VERSION.yml",
|
25
25
|
"init.rb",
|
26
|
+
"lib/tworgy/rails_ext.rb",
|
27
|
+
"lib/tworgy/recursively.rb",
|
26
28
|
"spec/spec_helper.rb",
|
29
|
+
"spec/tworgy/rails_ext_spec.rb",
|
30
|
+
"spec/tworgy/recursively_spec.rb",
|
27
31
|
"tworgy-rails-ext.gemspec"
|
28
32
|
]
|
29
33
|
s.homepage = %q{http://github.com/matholroyd/tworgy-rails-ext}
|
@@ -33,7 +37,8 @@ Gem::Specification.new do |s|
|
|
33
37
|
s.summary = %q{Rails extentions found to be quite handy}
|
34
38
|
s.test_files = [
|
35
39
|
"spec/spec_helper.rb",
|
36
|
-
"spec/tworgy/rails_ext_spec.rb"
|
40
|
+
"spec/tworgy/rails_ext_spec.rb",
|
41
|
+
"spec/tworgy/recursively_spec.rb"
|
37
42
|
]
|
38
43
|
|
39
44
|
if s.respond_to? :specification_version then
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tworgy-rails-ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mat Holroyd
|
@@ -30,7 +30,11 @@ files:
|
|
30
30
|
- Rakefile
|
31
31
|
- VERSION.yml
|
32
32
|
- init.rb
|
33
|
+
- lib/tworgy/rails_ext.rb
|
34
|
+
- lib/tworgy/recursively.rb
|
33
35
|
- spec/spec_helper.rb
|
36
|
+
- spec/tworgy/rails_ext_spec.rb
|
37
|
+
- spec/tworgy/recursively_spec.rb
|
34
38
|
- tworgy-rails-ext.gemspec
|
35
39
|
has_rdoc: true
|
36
40
|
homepage: http://github.com/matholroyd/tworgy-rails-ext
|
@@ -63,3 +67,4 @@ summary: Rails extentions found to be quite handy
|
|
63
67
|
test_files:
|
64
68
|
- spec/spec_helper.rb
|
65
69
|
- spec/tworgy/rails_ext_spec.rb
|
70
|
+
- spec/tworgy/recursively_spec.rb
|