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
75
# 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])

  @sought_type = extract_type(opts[:as])
  if @sought_type.respond_to?(:roxml_tag_name)
    opts[:from] ||= @sought_type.roxml_tag_name
  end

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

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

#sought_typeObject (readonly)

Returns the value of attribute sought_type.



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

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

#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)


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

def content?
  @name == '.'
end

#defaultObject



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

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

#hash?Boolean

Returns:

  • (Boolean)


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

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

#instance_variable_nameObject



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

def instance_variable_name
  :"@#{attr_name}"
end

#name?Boolean

Returns:

  • (Boolean)


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

def name?
  @name == '*'
end

#setterObject



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

def setter
  :"#{attr_name}="
end

#to_ref(inst) ⇒ Object



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

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