Class: ROXML::Definition
Overview
:nodoc:
Instance Attribute Summary collapse
-
#accessor ⇒ Object
readonly
Returns the value of attribute accessor.
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#to_xml ⇒ Object
readonly
Returns the value of attribute to_xml.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #array? ⇒ Boolean
- #cdata? ⇒ Boolean
- #content? ⇒ Boolean
- #default ⇒ Object
- #freeze? ⇒ Boolean
- #hash? ⇒ Boolean
-
#initialize(sym, *args, &block) ⇒ Definition
constructor
A new instance of Definition.
- #name? ⇒ Boolean
- #name_explicit? ⇒ Boolean
- #required? ⇒ Boolean
- #to_ref(inst) ⇒ Object
- #variable_name ⇒ Object
- #wrapper ⇒ Object
Constructor Details
#initialize(sym, *args, &block) ⇒ Definition
Returns a new instance of Definition.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/roxml/definition.rb', line 17 def initialize(sym, *args, &block) @accessor = sym @opts = (args) @default = @opts.delete(:else) @to_xml = @opts.delete(:to_xml) @name_explicit = @opts.has_key?(:from) if @opts.has_key?(:readonly) raise ArgumentError, "There is no 'readonly' option. You probably mean to use :frozen => true" end @opts.reverse_merge!(:as => [], :in => nil) @opts[:as] = [*@opts[:as]] @type = extract_type(args) @opts[:as] << :bool if @accessor.to_s.ends_with?('?') if @type.try(:xml_name_without_deprecation?) unless self.class.silence_xml_name_warning? warn "WARNING: As of 2.3, a breaking change has been in the naming of sub-objects. " + "ROXML now considers the xml_name of the sub-object before falling back to the accessor name of the parent. " + "Use :from on the parent declaration to override this behavior. Set ROXML::SILENCE_XML_NAME_WARNING to avoid this message." self.class.silence_xml_name_warning! end @opts[:from] ||= @type.tag_name else @opts[:from] ||= variable_name end @blocks = collect_blocks(block, @opts[:as]) @name = @opts[:from].to_s @name = @name.singularize if hash? || array? if hash? && (hash.key.name? || hash.value.name?) @name = '*' end raise ArgumentError, "Can't specify both :else default and :required" if required? && @default end |
Instance Attribute Details
#accessor ⇒ Object (readonly)
Returns the value of attribute accessor.
5 6 7 |
# File 'lib/roxml/definition.rb', line 5 def accessor @accessor end |
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
5 6 7 |
# File 'lib/roxml/definition.rb', line 5 def blocks @blocks end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
5 6 7 |
# File 'lib/roxml/definition.rb', line 5 def hash @hash end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/roxml/definition.rb', line 5 def name @name end |
#to_xml ⇒ Object (readonly)
Returns the value of attribute to_xml.
5 6 7 |
# File 'lib/roxml/definition.rb', line 5 def to_xml @to_xml end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/roxml/definition.rb', line 5 def type @type end |
Class Method Details
.silence_xml_name_warning! ⇒ Object
12 13 14 |
# File 'lib/roxml/definition.rb', line 12 def silence_xml_name_warning! @silence_xml_name_warning = true end |
.silence_xml_name_warning? ⇒ Boolean
8 9 10 |
# File 'lib/roxml/definition.rb', line 8 def silence_xml_name_warning? @silence_xml_name_warning || (ROXML.const_defined?('SILENCE_XML_NAME_WARNING') && ROXML::SILENCE_XML_NAME_WARNING) end |
Instance Method Details
#array? ⇒ Boolean
81 82 83 |
# File 'lib/roxml/definition.rb', line 81 def array? @opts[:as].include? :array end |
#cdata? ⇒ Boolean
85 86 87 |
# File 'lib/roxml/definition.rb', line 85 def cdata? @opts[:as].include? :cdata end |
#content? ⇒ Boolean
77 78 79 |
# File 'lib/roxml/definition.rb', line 77 def content? @type == :content end |
#default ⇒ Object
101 102 103 104 105 |
# File 'lib/roxml/definition.rb', line 101 def default @default ||= [] if array? @default ||= {} if hash? @default.duplicable? ? @default.dup : @default end |
#freeze? ⇒ Boolean
97 98 99 |
# File 'lib/roxml/definition.rb', line 97 def freeze? @opts[:frozen] end |
#hash? ⇒ Boolean
65 66 67 |
# File 'lib/roxml/definition.rb', line 65 def hash? @type == :hash end |
#name? ⇒ Boolean
69 70 71 |
# File 'lib/roxml/definition.rb', line 69 def name? @name == '*' end |
#name_explicit? ⇒ Boolean
73 74 75 |
# File 'lib/roxml/definition.rb', line 73 def name_explicit? @name_explicit end |
#required? ⇒ Boolean
93 94 95 |
# File 'lib/roxml/definition.rb', line 93 def required? @opts[:required] end |
#to_ref(inst) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/roxml/definition.rb', line 107 def to_ref(inst) case type when :attr then XMLAttributeRef when :content then XMLTextRef when :text then XMLTextRef when :hash then XMLHashRef when Symbol then raise ArgumentError, "Invalid type argument #{opts.type}" else XMLObjectRef end.new(self, inst) end |
#variable_name ⇒ Object
57 58 59 |
# File 'lib/roxml/definition.rb', line 57 def variable_name accessor.to_s.ends_with?('?') ? accessor.to_s.chomp('?') : accessor.to_s end |
#wrapper ⇒ Object
89 90 91 |
# File 'lib/roxml/definition.rb', line 89 def wrapper @opts[:in] end |