Module: OpenXml::HasAttributes::ClassMethods

Defined in:
lib/openxml/has_attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, expects: nil, one_of: nil, in_range: nil, displays_as: nil, namespace: nil, matches: nil, deprecated: false) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/openxml/has_attributes.rb', line 9

def attribute(name, expects: nil, one_of: nil, in_range: nil, displays_as: nil, namespace: nil, matches: nil, deprecated: false)
  bad_names = %w(tag name namespace properties_tag)
  raise ArgumentError if bad_names.member? name

  attr_reader name

  define_method "#{name}=" do |value|
    valid_in?(value, one_of) unless one_of.nil?
    send(expects, value) unless expects.nil?
    matches?(value, matches) unless matches.nil?
    in_range?(value, in_range) unless in_range.nil?
    instance_variable_set "@#{name}", value
  end

  camelized_name = name.to_s.gsub(/_([a-z])/i) { $1.upcase }.to_sym
  attributes[name] = [displays_as || camelized_name, namespace || @attribute_namespace]
end

#attributesObject



27
28
29
# File 'lib/openxml/has_attributes.rb', line 27

def attributes
  @attributes ||= {}
end

#with_namespace(namespace, &block) ⇒ Object



31
32
33
34
# File 'lib/openxml/has_attributes.rb', line 31

def with_namespace(namespace, &block)
  @attribute_namespace = namespace
  instance_eval(&block)
end