Skip to content

Commit

Permalink
[WIP] Block improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnoukhov committed Jan 3, 2015
1 parent be043d2 commit 3eb5aff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
48 changes: 24 additions & 24 deletions lib/sax-machine/sax_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,16 @@ def attribute(name, options = {}, &block)
create_attr(real_name, &block)
end

def value(name, options = {})
def value(name, options = {}, &block)
real_name = (options[:as] ||= name).to_s
sax_config.add_top_level_element_value(self.class.to_s, options.merge(name: name))
create_attr(real_name)
create_attr(real_name, &block)
end

def ancestor(name, options = {})
def ancestor(name, options = {}, &block)
real_name = (options[:as] ||= name).to_s
sax_config.add_ancestor(name, options)
create_attr(real_name)
end

def columns
sax_config.columns
end

def column(sym)
columns.select { |c| c.column == sym }[0]
end

def data_class(sym)
column(sym).data_class
end

def required?(sym)
column(sym).required?
end

def column_names
columns.map { |e| e.column }
create_attr(real_name, &block)
end

def elements(name, options = {})
Expand Down Expand Up @@ -108,6 +88,26 @@ def #{options[:as]}
attr_writer(options[:as]) unless method_defined?("#{options[:as]}=")
end

def columns
sax_config.columns
end

def column(sym)
columns.select { |c| c.column == sym }[0]
end

def data_class(sym)
column(sym).data_class
end

def required?(sym)
column(sym).required?
end

def column_names
columns.map { |e| e.column }
end

def sax_config
@sax_config ||= SAXConfig.new
end
Expand Down
33 changes: 24 additions & 9 deletions spec/sax-machine/sax_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,19 @@ def title=(val)

describe "the block" do
before do
@klass = Class.new do
class ElementBlockParser
include SAXMachine

ancestor :parent do |parent|
parent.class.to_s
end

value :text do |text|
text.downcase
end
end

class BlockParser
include SAXMachine

element :title do |title|
Expand All @@ -232,25 +244,28 @@ def title=(val)
id.to_i
end

element :link, value: :foo do |link|
link.downcase
end
element :nested, class: ElementBlockParser
end
end

it "uses block for element" do
document = @klass.parse("<title>SAX</title>")
document = BlockParser.parse("<title>SAX</title>")
expect(document.title).to eq("SAX!!!")
end

it 'uses block for attribute' do
document = @klass.parse("<title id='345'>SAX</title>")
document = BlockParser.parse("<title id='345'>SAX</title>")
expect(document.id).to eq(345)
end

it "uses block for attribute value" do
document = @klass.parse("<link foo='tEst'>hello</link>")
expect(document.link).to eq("test")
it "uses block for value" do
document = BlockParser.parse("<title><nested>tEst</nested></title>")
expect(document.nested.text).to eq("test")
end

it "uses block for ancestor" do
document = BlockParser.parse("<title><nested>SAX</nested></title>")
expect(document.nested.parent).to eq("BlockParser")
end
end
end
Expand Down

0 comments on commit 3eb5aff

Please sign in to comment.