Module: OpenXml::HasProperties::ClassMethods

Defined in:
lib/openxml/has_properties.rb

Instance Method Summary collapse

Instance Method Details

#choice_groupsObject



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

def choice_groups
  @choice_groups ||= [].tap do |choices|
    choices.push(*superclass.choice_groups.map(&:dup)) if superclass.respond_to?(:choice_groups)
  end
end

#current_groupObject



68
69
70
# File 'lib/openxml/has_properties.rb', line 68

def current_group
  @current_group ||= nil
end

#default_properties_tagObject



118
119
120
# File 'lib/openxml/has_properties.rb', line 118

def default_properties_tag
  :"#{tag}Pr"
end

#propertiesObject



72
73
74
75
76
# File 'lib/openxml/has_properties.rb', line 72

def properties
  @properties ||= {}.tap do |props|
    props.merge!(superclass.properties) if superclass.respond_to?(:properties)
  end
end

#properties_attribute(name, **args) ⇒ Object



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

def properties_attribute(name, **args)
  properties_element.attribute name, **args
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def #{name}=(value)
      properties_element.#{name} = value
    end

    def #{name}
      properties_element.#{name}
    end
  RUBY
end

#properties_elementObject



109
110
111
112
113
114
115
116
# File 'lib/openxml/has_properties.rb', line 109

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



14
15
16
17
# File 'lib/openxml/has_properties.rb', line 14

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

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



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

def property(name, as: nil, klass: nil, required: false)
  properties[name] = (as || name).to_s
  required_properties[name] = true if required
  classified_name = properties[name].split("_").map(&:capitalize).join
  class_name = klass.to_s 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 <<-CODE, __FILE__, __LINE__ + 1
  def #{name}(*args)
    unless instance_variable_defined?("@#{name}")
      group_index = #{@current_group.inspect}
      ensure_unique_in_group(:#{name}, group_index) unless group_index.nil?
      instance_variable_set "@#{name}", #{class_name}.new(*args)
    end

    instance_variable_get "@#{name}"
  end
  CODE
end

#property_choice(required: false) ⇒ Object



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

def property_choice(required: false)
  @current_group = choice_groups.length
  required_choices << @current_group if required
  yield
  @current_group = nil
end

#required_choicesObject



90
91
92
93
94
# File 'lib/openxml/has_properties.rb', line 90

def required_choices
  @required_choices ||= [].tap do |choices|
    choices.push(*superclass.required_choices) if superclass.respond_to?(:required_choices)
  end
end

#required_propertiesObject



84
85
86
87
88
# File 'lib/openxml/has_properties.rb', line 84

def required_properties
  @required_properties ||= {}.tap do |props|
    props.merge!(superclass.required_properties) if superclass.respond_to?(:required_properties)
  end
end

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



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

def value_property(name, as: nil, klass: nil, required: false, default_value: nil)
  attr_reader name

  properties[name] = (as || name).to_s
  required_properties[name] = default_value if required
  classified_name = properties[name].split("_").map(&:capitalize).join
  class_name = klass.to_s 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 <<-CODE, __FILE__, __LINE__ + 1
  def #{name}=(value)
    group_index = #{@current_group.inspect}
    ensure_unique_in_group(:#{name}, group_index) unless group_index.nil?
    instance_variable_set "@#{name}", #{class_name}.new(value)
  end
  CODE
end