Class: ROXML::Definition

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sym, opts = {}, &block) ⇒ Definition

Returns a new instance of Definition.

Raises:

  • (ArgumentError)


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

def initialize(sym, opts = {}, &block)
  opts.assert_valid_keys(:from, :in, :as,
                         :else, :required, :frozen, :cdata, :to_xml)
  @default = opts.delete(:else)
  @to_xml = opts.delete(:to_xml)
  @name_explicit = opts.has_key?(:from) && opts[:from].is_a?(String)
  @cdata = opts.delete(:cdata)
  @required = opts.delete(:required)
  @frozen = opts.delete(:frozen)
  @wrapper = opts.delete(:in)

  @accessor = sym.to_s
  opts[:as] ||=
    if @accessor.ends_with?('?')
      :bool
    elsif @accessor.ends_with?('_on')
      Date
    elsif @accessor.ends_with?('_at')
      DateTime
    end

  @array = opts[:as].is_a?(Array)
  @blocks = collect_blocks(block, opts[:as])

  @type = extract_type(opts[:as])
  if @type.respond_to?(:roxml_tag_name)
    # "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."
    opts[:from] ||= @type.roxml_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] == :name
    opts[:from] = '*'
  elsif opts[:from].to_s.starts_with?('@')
    @type = :attr
    opts[:from].sub!('@', '')
  end

  @attr_name = accessor.to_s.chomp('?')
  @name = (opts[:from] || @attr_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

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



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

def attr_name
  @attr_name
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

Instance Method Details

#content?Boolean

Returns:

  • (Boolean)


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

def content?
  @name == '.'
end

#defaultObject



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

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

#hash?Boolean

Returns:

  • (Boolean)


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

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

#instance_variable_nameObject



74
75
76
# File 'lib/roxml/definition.rb', line 74

def instance_variable_name
  :"@#{attr_name}"
end

#name?Boolean

Returns:

  • (Boolean)


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

def name?
  @name == '*'
end

#setterObject



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

def setter
  :"#{attr_name}="
end

#to_ref(inst) ⇒ Object



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

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