lib/representable/definition.rb in representable-0.0.2 vs lib/representable/definition.rb in representable-0.9.0

- old
+ new

@@ -1,58 +1,29 @@ -class Module - def bool_attr_reader(*attrs) - attrs.each do |attr| - define_method :"#{attr}?" do - instance_variable_get(:"@#{attr}") || false - end - end - end -end - module Representable - class ContradictoryNamespaces < StandardError - end class Definition # :nodoc: - attr_reader :name, :sought_type, :wrapper, :accessor, :namespace - bool_attr_reader :name_explicit, :array, :cdata + attr_reader :name, :sought_type, :wrapper, :accessor, :from + def initialize(sym, opts={}) - @accessor = sym.to_s - @namespace = opts.delete(:namespace) + @accessor = @name = sym.to_s + @array = true if opts[:as].is_a?(Array) # DISCUSS: move to ArrayDefinition. + @from = (opts[:from] || name).to_s + @sought_type = extract_type(opts[:as]) - if opts[:as].is_a?(Array) # DISCUSS: move to ArrayDefinition. - @array = true - @name = (opts[:tag] || @accessor).to_s - else - @name = accessor - @name = (opts[:from] || @name).to_s - end - - @sought_type = extract_type(opts[:as]) - if @sought_type.respond_to?(:roxml_tag_name) - opts[:from] ||= @sought_type.roxml_tag_name - end - + # FIXME: move me to xml. if opts[:from] == :content opts[:from] = '.' elsif opts[:from] == :name opts[:from] = '*' elsif opts[:from] == :attr @sought_type = :attr opts[:from] = nil - elsif opts[:from] == :namespace - opts[:from] = '*' - @sought_type = :namespace - elsif opts[:from].to_s =~ /^@/ # FIXME: move me to xml. + elsif opts[:from].to_s =~ /^@/ @sought_type = :attr opts[:from].sub!('@', '') end - - - #raise ContradictoryNamespaces if @name.include?(':') && (@namespace.present? || @namespace == false) - end def instance_variable_name :"@#{accessor}" end @@ -70,9 +41,18 @@ end def content? @name == '.' end + + def array? + @array + end + + def cdata? # FIXME: move to XML! + @cdata + end + # Applies the block to +value+ which might also be a collection. def apply(value) return value unless value # DISCUSS: is that ok here?