Class: Csv2other
- Inherits:
-
Object
- Object
- Csv2other
- Defined in:
- lib/csv2other.rb,
lib/csv2other/version.rb
Constant Summary collapse
- VERSION =
"0.0.4"
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize ⇒ Csv2other
constructor
A new instance of Csv2other.
- #list ⇒ Object
- #load_template(template_filename) ⇒ Object
- #parse(csv_filename, key) ⇒ Object
- #render(my_binding = binding) ⇒ Object
- #validate_with_xsd(xsd_path, binding_from, xpath = nil) ⇒ Object
Constructor Details
#initialize ⇒ Csv2other
Returns a new instance of Csv2other.
9 10 11 |
# File 'lib/csv2other.rb', line 9 def initialize @content = {} end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
7 8 9 |
# File 'lib/csv2other.rb', line 7 def content @content end |
Instance Method Details
#each ⇒ Object
30 31 32 33 34 |
# File 'lib/csv2other.rb', line 30 def each @content.each do |k, v| yield k, v end end |
#list ⇒ Object
21 22 23 |
# File 'lib/csv2other.rb', line 21 def list @content.keys end |
#load_template(template_filename) ⇒ Object
25 26 27 28 |
# File 'lib/csv2other.rb', line 25 def load_template(template_filename) @template_filename = template_filename @template = File.open(@template_filename).readlines.join end |
#parse(csv_filename, key) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/csv2other.rb', line 13 def parse(csv_filename, key) key_symbol = key.to_sym CSV.foreach(csv_filename, :col_sep =>';', :headers => true, :header_converters => :symbol) do |line| current_key = line[key_symbol] @content[current_key] = line end end |
#render(my_binding = binding) ⇒ Object
36 37 38 |
# File 'lib/csv2other.rb', line 36 def render(my_binding = binding) ERB.new(@template).result(my_binding) end |
#validate_with_xsd(xsd_path, binding_from, xpath = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/csv2other.rb', line 40 def validate_with_xsd(xsd_path, binding_from, xpath = nil) content = self.render(binding_from) unless xpath.nil? content = Nokogiri::XML(content).xpath(xpath).to_xml end xsd_path = xsd_path xsddoc = Nokogiri::XML(File.read(xsd_path), xsd_path) xsd = Nokogiri::XML::Schema.from_document(xsddoc) xsd.validate(Nokogiri::XML(content)).each do |error| puts error. raise error end end |