Class: Zena::FoxyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/zena/foxy_parser.rb

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_namesObject (readonly)

Returns the value of attribute column_names.



5
6
7
# File 'lib/zena/foxy_parser.rb', line 5

def column_names
  @column_names
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



5
6
7
# File 'lib/zena/foxy_parser.rb', line 5

def defaults
  @defaults
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/zena/foxy_parser.rb', line 5

def name
  @name
end

#siteObject (readonly)

Returns the value of attribute site.



5
6
7
# File 'lib/zena/foxy_parser.rb', line 5

def site
  @site
end

#tableObject (readonly)

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 # special rgroup, wgroup, dgroup values...
    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_tablesObject



23
24
25
26
# File 'lib/zena/foxy_parser.rb', line 23

def multi_site_tables
  # Users are multi_site to ease 'login' during testing.
  ['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) # Module.const_get not working for some strange reason...
    raise ArgumentError unless klass.ancestors.include?(FoxyParser)
  rescue ArgumentError
    klass = self
  end
  klass.o_new(table_name.to_s, opts)
end

.o_newObject



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

.preludeObject

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_elementsObject



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

#runObject



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

#sitesObject



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