Module: OpenXml::HasAttributes::ClassMethods
- Defined in:
- lib/openxml/has_attributes.rb
Constant Summary collapse
- RESERVED_NAMES =
%w{ tag name namespace properties_tag }.freeze
Instance Method Summary collapse
- #attribute(name, expects: nil, one_of: nil, in_range: nil, displays_as: nil, namespace: nil, matches: nil, validation: nil, required: false, deprecated: false) ⇒ Object
- #attribute_namespace ⇒ Object
- #attributes ⇒ Object
- #required_attributes ⇒ Object
- #with_namespace(namespace, &block) ⇒ Object
Instance Method Details
#attribute(name, expects: nil, one_of: nil, in_range: nil, displays_as: nil, namespace: nil, matches: nil, validation: nil, required: false, deprecated: false) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/openxml/has_attributes.rb', line 14 def attribute(name, expects: nil, one_of: nil, in_range: nil, displays_as: nil, namespace: nil, matches: nil, validation: nil, required: false, deprecated: false) raise ArgumentError if RESERVED_NAMES.member? name.to_s required_attributes.push(name) if required 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_namespace ⇒ Object
55 56 57 |
# File 'lib/openxml/has_attributes.rb', line 55 def attribute_namespace @attribute_namespace ||= nil end |
#attributes ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/openxml/has_attributes.rb', line 34 def attributes @attributes ||= {}.tap do |attrs| if superclass.respond_to?(:attributes) superclass.attributes.each do |key, value| attrs[key] = value.dup end end end end |
#required_attributes ⇒ Object
44 45 46 47 48 |
# File 'lib/openxml/has_attributes.rb', line 44 def required_attributes @required_attributes ||= [].tap do |attrs| attrs.push(*superclass.required_attributes) if superclass.respond_to?(:required_attributes) end end |
#with_namespace(namespace, &block) ⇒ Object
50 51 52 53 |
# File 'lib/openxml/has_attributes.rb', line 50 def with_namespace(namespace, &block) @attribute_namespace = namespace instance_eval(&block) end |