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.



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
73
74
# File 'lib/roxml/definition.rb', line 21

def initialize(sym, opts = {}, &block)
  opts.assert_valid_keys(:from, :in, :as, :namespace,
                         :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)
  @namespace = opts.delete(:namespace)

  @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)
    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 ContradictoryNamespaces if @name.include?(':') && (@namespace.present? || @namespace == false)

  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.



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

def accessor
  @accessor
end

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



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

def attr_name
  @attr_name
end

#blocksObject (readonly)

Returns the value of attribute blocks.



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

def blocks
  @blocks
end

#hashObject (readonly)

Returns the value of attribute hash.



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

def hash
  @hash
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#to_xmlObject (readonly)

Returns the value of attribute to_xml.



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

def to_xml
  @to_xml
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#wrapperObject (readonly)

Returns the value of attribute wrapper.



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

def wrapper
  @wrapper
end

Instance Method Details

#content?Boolean

Returns:

  • (Boolean)


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

def content?
  @name == '.'
end

#defaultObject



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

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

#hash?Boolean

Returns:

  • (Boolean)


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

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

#instance_variable_nameObject



76
77
78
# File 'lib/roxml/definition.rb', line 76

def instance_variable_name
  :"@#{attr_name}"
end

#name?Boolean

Returns:

  • (Boolean)


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

def name?
  @name == '*'
end

#setterObject



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

def setter
  :"#{attr_name}="
end

#to_ref(inst) ⇒ Object



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

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