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, validation: nil, deprecated: false) ⇒ Object

Raises:



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

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

  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?
    validation.call(value) if validation.respond_to? :call
    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

#attribute_namespaceObject



46
47
48
# File 'lib/openxml/has_attributes.rb', line 46

def attribute_namespace
  @attribute_namespace ||= nil
end

#attributesObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openxml/has_attributes.rb', line 29

def attributes
  @attributes ||= begin
    if superclass.respond_to?(:attributes)
      Hash[superclass.attributes.map { |name, attribute|
        [ name, attribute.dup ]
      }]
    else
      {}
    end
  end
end

#with_namespace(namespace, &block) ⇒ Object



41
42
43
44
# File 'lib/openxml/has_attributes.rb', line 41

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