Module: OpenXml::HasProperties::ClassMethods

Defined in:
lib/openxml/has_properties.rb

Instance Method Summary collapse

Instance Method Details

#choice_groupsObject



77
78
79
80
81
82
83
84
85
# File 'lib/openxml/has_properties.rb', line 77

def choice_groups
  @choice_groups ||= begin
    if superclass.respond_to?(:choice_groups)
      superclass.choice_groups.map(&:dup)
    else
      []
    end
  end
end

#current_groupObject



63
64
65
# File 'lib/openxml/has_properties.rb', line 63

def current_group
  @current_group ||= nil
end

#default_properties_tagObject



107
108
109
# File 'lib/openxml/has_properties.rb', line 107

def default_properties_tag
  :"#{tag}Pr"
end

#propertiesObject



67
68
69
70
71
72
73
74
75
# File 'lib/openxml/has_properties.rb', line 67

def properties
  @properties ||= begin
    if superclass.respond_to?(:properties)
      Hash[superclass.properties.map { |key, klass_name| [ key, klass_name.dup ] }]
    else
      {}
    end
  end
end

#properties_attribute(name, **args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/openxml/has_properties.rb', line 87

def properties_attribute(name, **args)
  properties_element.attribute name, **args
  define_method "#{name}=" do |value|
    properties_element.public_send :"#{name}=", value
  end

  define_method name.to_s do
    properties_element.public_send name.to_sym
  end
end

#properties_elementObject



98
99
100
101
102
103
104
105
# File 'lib/openxml/has_properties.rb', line 98

def properties_element
  this = self
  parent_klass = superclass.respond_to?(:properties_element) ? superclass.properties_element : OpenXml::Element
  @properties_element ||= Class.new(parent_klass) do
    tag :"#{this.properties_tag || this.default_properties_tag}"
    namespace :"#{this.namespace}"
  end
end

#properties_tag(*args) ⇒ Object



12
13
14
15
# File 'lib/openxml/has_properties.rb', line 12

def properties_tag(*args)
  @properties_tag = args.first if args.any?
  @properties_tag ||= nil
end

#property(name, as: nil, klass: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/openxml/has_properties.rb', line 36

def property(name, as: nil, klass: nil)
  properties[name] = (as || name).to_s
  classified_name = properties[name].split("_").map(&:capitalize).join
  class_name = klass.name unless klass.nil?
  class_name ||= (to_s.split("::")[0...-2] + ["Properties", classified_name]).join("::")

  (choice_groups[current_group] ||= []).push(name) unless current_group.nil?

  class_eval "  def \#{name}(*args)\n    if instance_variable_get(\"@\#{name}\").nil?\n      group_index = \#{@current_group.inspect}\n      ensure_unique_in_group(:\#{name}, group_index) unless group_index.nil?\n      instance_variable_set \"@\#{name}\", \#{class_name}.new(*args)\n    end\n\n    instance_variable_get \"@\#{name}\"\n  end\n  CODE\nend\n", __FILE__, __LINE__ + 1

#property_choiceObject



57
58
59
60
61
# File 'lib/openxml/has_properties.rb', line 57

def property_choice
  @current_group = choice_groups.length
  yield
  @current_group = nil
end

#value_property(name, as: nil, klass: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/openxml/has_properties.rb', line 17

def value_property(name, as: nil, klass: nil)
  attr_reader name

  properties[name] = (as || name).to_s
  classified_name = properties[name].split("_").map(&:capitalize).join
  class_name = klass.name unless klass.nil?
  class_name ||= (to_s.split("::")[0...-2] + ["Properties", classified_name]).join("::")

  (choice_groups[current_group] ||= []).push(name) unless current_group.nil?

  class_eval "  def \#{name}=(value)\n    group_index = \#{@current_group.inspect}\n    ensure_unique_in_group(:\#{name}, group_index) unless group_index.nil?\n    instance_variable_set \"@\#{name}\", \#{class_name}.new(value)\n  end\n  CODE\nend\n", __FILE__, __LINE__ + 1