Class: ROXML::Opts

Inherits:
Object show all
Defined in:
lib/roxml/options.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sym, *args, &block) ⇒ Opts

Returns a new instance of Opts.

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/roxml/options.rb', line 73

def initialize(sym, *args, &block)
  @accessor = sym
  @opts = extract_options!(args)
  @default = @opts.delete(:else)
  @to_xml = @opts.delete(:to_xml)

  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

#accessorObject (readonly)

Returns the value of attribute accessor.



61
62
63
# File 'lib/roxml/options.rb', line 61

def accessor
  @accessor
end

#blocksObject (readonly)

Returns the value of attribute blocks.



61
62
63
# File 'lib/roxml/options.rb', line 61

def blocks
  @blocks
end

#hashObject (readonly)

Returns the value of attribute hash.



61
62
63
# File 'lib/roxml/options.rb', line 61

def hash
  @hash
end

#nameObject (readonly)

Returns the value of attribute name.



61
62
63
# File 'lib/roxml/options.rb', line 61

def name
  @name
end

#to_xmlObject (readonly)

Returns the value of attribute to_xml.



61
62
63
# File 'lib/roxml/options.rb', line 61

def to_xml
  @to_xml
end

#typeObject (readonly)

Returns the value of attribute type.



61
62
63
# File 'lib/roxml/options.rb', line 61

def type
  @type
end

Class Method Details

.silence_xml_name_warning!Object



68
69
70
# File 'lib/roxml/options.rb', line 68

def silence_xml_name_warning!
  @silence_xml_name_warning = true
end

.silence_xml_name_warning?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/roxml/options.rb', line 64

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

Returns:

  • (Boolean)


132
133
134
# File 'lib/roxml/options.rb', line 132

def array?
  @opts[:as].include? :array
end

#cdata?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/roxml/options.rb', line 136

def cdata?
  @opts[:as].include? :cdata
end

#content?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/roxml/options.rb', line 128

def content?
  @type == :content
end

#defaultObject



152
153
154
155
156
# File 'lib/roxml/options.rb', line 152

def default
  @default ||= [] if array?
  @default ||= {} if hash?
  @default.duplicable? ? @default.dup : @default
end

#freeze?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/roxml/options.rb', line 148

def freeze?
  @opts[:frozen]
end

#hash?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/roxml/options.rb', line 120

def hash?
  @type == :hash
end

#name?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/roxml/options.rb', line 124

def name?
  @name == '*'
end

#required?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/roxml/options.rb', line 144

def required?
  @opts[:required]
end

#to_ref(inst) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/roxml/options.rb', line 158

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_nameObject



112
113
114
# File 'lib/roxml/options.rb', line 112

def variable_name
  accessor.to_s.ends_with?('?') ? accessor.to_s.chomp('?') : accessor.to_s
end

#wrapperObject



140
141
142
# File 'lib/roxml/options.rb', line 140

def wrapper
  @opts[:in]
end