Class: Zena::FoxyParser
- Inherits:
-
Object
show all
- Defined in:
- lib/zena/foxy_parser.rb
Direct Known Subclasses
FoxyAttachmentsParser, FoxyColumnsParser, FoxyCommentsParser, FoxyGroupsUsersParser, FoxyIformatsParser, FoxyLinksParser, FoxyNodesParser, FoxyNodesRolesParser, FoxyRelationsParser, FoxyRolesParser, FoxySitesParser, FoxyUsersParser, FoxyVersionsParser, FoxyZipsParser
Constant Summary
collapse
- @@parser_for_table =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(table_name, opts = {}) ⇒ FoxyParser
Returns a new instance of FoxyParser.
77
78
79
80
81
82
|
# File 'lib/zena/foxy_parser.rb', line 77
def initialize(table_name, opts={})
@table = table_name
@column_names = Node.connection.columns(table).map {|c| c.name }
@elements = {}
@options = opts
end
|
Instance Attribute Details
#column_names ⇒ Object
Returns the value of attribute column_names.
5
6
7
|
# File 'lib/zena/foxy_parser.rb', line 5
def column_names
@column_names
end
|
#defaults ⇒ Object
Returns the value of attribute defaults.
5
6
7
|
# File 'lib/zena/foxy_parser.rb', line 5
def defaults
@defaults
end
|
#name ⇒ Object
Returns the value of attribute name.
5
6
7
|
# File 'lib/zena/foxy_parser.rb', line 5
def name
@name
end
|
#site ⇒ Object
Returns the value of attribute site.
5
6
7
|
# File 'lib/zena/foxy_parser.rb', line 5
def site
@site
end
|
#table ⇒ Object
Returns the value of attribute table.
5
6
7
|
# File 'lib/zena/foxy_parser.rb', line 5
def table
@table
end
|
Class Method Details
.dump_fixtures(index_tables) ⇒ Object
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/zena/foxy_parser.rb', line 50
def dump_fixtures(index_tables)
index_tables.each do |table_name|
klass = Class.new(ActiveRecord::Base)
klass.set_table_name table_name
File.open(path_for_table(table_name), 'wb') do |file|
file.puts records_to_yaml(klass.all)
end
end
end
|
.id(site, key) ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/zena/foxy_parser.rb', line 9
def id(site, key)
return nil if key.blank?
if key == 0 key
else
Fixtures.identify("#{site}_#{key}")
end
end
|
.multi_site_id(key) ⇒ Object
18
19
20
21
|
# File 'lib/zena/foxy_parser.rb', line 18
def multi_site_id(key)
return nil if key.blank?
Fixtures.identify(key)
end
|
.multi_site_tables ⇒ Object
23
24
25
26
|
# File 'lib/zena/foxy_parser.rb', line 23
def multi_site_tables
['users', 'sites']
end
|
.new(table_name, opts = {}) ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/zena/foxy_parser.rb', line 35
def new(table_name, opts={})
class_name = "Foxy#{table_name.to_s.camelcase}Parser"
begin
klass = eval(class_name) raise ArgumentError unless klass.ancestors.include?(FoxyParser)
rescue ArgumentError
klass = self
end
klass.o_new(table_name.to_s, opts)
end
|
.o_new ⇒ Object
33
|
# File 'lib/zena/foxy_parser.rb', line 33
alias o_new new
|
.parses(table_name) ⇒ Object
46
47
48
|
# File 'lib/zena/foxy_parser.rb', line 46
def parses(table_name)
@@parser_for_table[table_name.to_s] = self
end
|
.path_for_table(table) ⇒ Object
68
69
70
|
# File 'lib/zena/foxy_parser.rb', line 68
def path_for_table(table)
"#{RAILS_ROOT}/test/fixtures/#{table}.yml"
end
|
.prelude ⇒ Object
included at start of fixture file
29
30
31
|
# File 'lib/zena/foxy_parser.rb', line 29
def prelude
""
end
|
.records_to_yaml(records) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/zena/foxy_parser.rb', line 61
def records_to_yaml(records)
Hash[*records.map do |rec|
id = Fixtures.identify(rec.attributes.values * '_')
[id, rec.attributes]
end.flatten].to_yaml
end
|
Instance Method Details
#all_elements ⇒ Object
97
98
99
|
# File 'lib/zena/foxy_parser.rb', line 97
def all_elements
@elements
end
|
#path_for_table(table) ⇒ Object
73
74
75
|
# File 'lib/zena/foxy_parser.rb', line 73
def path_for_table(table)
self.class.path_for_table(table)
end
|
#run ⇒ Object
88
89
90
91
92
93
94
95
|
# File 'lib/zena/foxy_parser.rb', line 88
def run
sites.each do |site|
@site = site
parse_fixtures
after_parse
end
@file.close if @file
end
|
#sites ⇒ Object
84
85
86
|
# File 'lib/zena/foxy_parser.rb', line 84
def sites
@sites ||= Dir["#{Zena::ROOT}/test/sites/*", "#{RAILS_ROOT}/bricks/**/sites/*"].map {|s| File.directory?(s) ? File.basename(s) : nil}.compact.uniq
end
|