Class: ROXML::Definition

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

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Definition.

Raises:

  • (ArgumentError)


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/roxml/definition.rb', line 28

def initialize(sym, *args, &block)
  @accessor = sym
  if @accessor.to_s.ends_with?('_on')
    ActiveSupport::Deprecation.warn "In 3.0, attributes with names ending with _on will default to Date type, rather than :text"
  end
  if @accessor.to_s.ends_with?('_at')
    ActiveSupport::Deprecation.warn "In 3.0, attributes with names ending with _at will default to DateTime type, rather than :text"
  end

  opts = extract_options!(args)
  opts[:as] ||= :bool if @accessor.to_s.ends_with?('?')

  @array = opts[:as].is_a?(Array) || extract_from_as(opts, :array, "Please use [] around your usual type declaration")
  @blocks = collect_blocks(block, opts[:as])

  if opts.has_key?(:readonly)
    raise ArgumentError, "There is no 'readonly' option. You probably mean to use :frozen => true"
  end

  @type = extract_type(args, opts)
  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
  end

  if opts[:from] == :content
    opts[:from] = '.'
  elsif opts[:from] == :name
    opts[:from] = '*'
  elsif opts[:from] == :attr
    @type = :attr
    opts[:from] = nil
  elsif opts[:from].to_s.starts_with?('@')
    @type = :attr
    opts[:from].sub!('@', '')
  end

  @name = (opts[:from] || variable_name).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.



15
16
17
# File 'lib/roxml/definition.rb', line 15

def accessor
  @accessor
end

#blocksObject (readonly)

Returns the value of attribute blocks.



15
16
17
# File 'lib/roxml/definition.rb', line 15

def blocks
  @blocks
end

#hashObject (readonly)

Returns the value of attribute hash.



15
16
17
# File 'lib/roxml/definition.rb', line 15

def hash
  @hash
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/roxml/definition.rb', line 15

def name
  @name
end

#to_xmlObject (readonly)

Returns the value of attribute to_xml.



15
16
17
# File 'lib/roxml/definition.rb', line 15

def to_xml
  @to_xml
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/roxml/definition.rb', line 15

def type
  @type
end

#wrapperObject (readonly)

Returns the value of attribute wrapper.



15
16
17
# File 'lib/roxml/definition.rb', line 15

def wrapper
  @wrapper
end

Class Method Details

.silence_xml_name_warning!Object



23
24
25
# File 'lib/roxml/definition.rb', line 23

def silence_xml_name_warning!
  @silence_xml_name_warning = true
end

.silence_xml_name_warning?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/roxml/definition.rb', line 19

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

#content?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/roxml/definition.rb', line 98

def content?
  @name == '.'
end

#defaultObject



102
103
104
105
106
107
108
# File 'lib/roxml/definition.rb', line 102

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

#hash?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/roxml/definition.rb', line 90

def hash?
  @type.is_a?(HashDefinition)
end

#name?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/roxml/definition.rb', line 94

def name?
  @name == '*'
end

#to_ref(inst) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/roxml/definition.rb', line 110

def to_ref(inst)
  case type
  when :attr          then XMLAttributeRef
  when :text          then XMLTextRef
  when HashDefinition then XMLHashRef
  when Symbol         then raise ArgumentError, "Invalid type argument #{type}"
  else                     XMLObjectRef
  end.new(self, inst)
end

#variable_nameObject



79
80
81
# File 'lib/roxml/definition.rb', line 79

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