Class: Mondrian::SchemaElement

Inherits:
Object
  • Object
show all
Defined in:
lib/mondrian/schema_element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, attributes = {}, &block) ⇒ SchemaElement

Returns a new instance of SchemaElement.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mondrian/schema_element.rb', line 5

def initialize(name = nil, attributes = {}, &block)
  # if just attributes hash provided
  if name.is_a?(Hash) && attributes == {}
    attributes = name
    name = nil
  end
  @attributes = {}
  if name
    if self.class.content
      @content = name
    else
      @attributes[:name] = name
    end
  end
  @attributes.merge!(attributes)
  self.class.elements.each do |element|
    instance_variable_set("@#{pluralize(element)}", [])
  end
  @xml_fragments = []
  instance_eval &block if block
end

Instance Attribute Details

#xml_fragmentsObject (readonly)

Returns the value of attribute xml_fragments.



70
71
72
# File 'lib/mondrian/schema_element.rb', line 70

def xml_fragments
  @xml_fragments
end

Class Method Details

.attributes(*names) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mondrian/schema_element.rb', line 27

def self.attributes(*names)
  names.each do |name|
    class_eval "        def \#{name}(*args)\n          if args.empty?\n            @attributes[:\#{name}]\n          elsif args.size == 1\n            @attributes[:\#{name}] = args[0]\n          else\n            raise ArgumentError, \"too many arguments\"\n          end\n        end\n    RUBY\n  end\nend\n", __FILE__, __LINE__ + 1

.content(type = nil) ⇒ Object



65
66
67
68
# File 'lib/mondrian/schema_element.rb', line 65

def self.content(type=nil)
  return @content if type.nil?
  @content = type
end

.data_dictionary_names(*names) ⇒ Object



43
44
45
46
47
# File 'lib/mondrian/schema_element.rb', line 43

def self.data_dictionary_names(*names)
  return @data_dictionary_names || [] if names.empty?
  @data_dictionary_names ||= []
  @data_dictionary_names.concat(names)
end

.elements(*names) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mondrian/schema_element.rb', line 49

def self.elements(*names)
  return @elements || [] if names.empty?

  @elements ||= []
  @elements.concat(names)

  names.each do |name|
    attr_reader pluralize(name).to_sym
    class_eval "        def \#{name}(name=nil, attributes = {}, &block)\n          @\#{pluralize(name)} << Schema::\#{camel_case(name)}.new(name, attributes, &block)\n        end\n    RUBY\n  end\nend\n", __FILE__, __LINE__ + 1

Instance Method Details

#to_xml(options = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/mondrian/schema_element.rb', line 78

def to_xml(options={})
  options[:upcase_data_dictionary] = @upcase_data_dictionary unless @upcase_data_dictionary.nil?
  Nokogiri::XML::Builder.new do |xml|
    add_to_xml(xml, options)
  end.to_xml
end

#xml(string) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
# File 'lib/mondrian/schema_element.rb', line 71

def xml(string)
  string = string.strip
  fragment = Nokogiri::XML::DocumentFragment.parse(string)
  raise ArgumentError, "Invalid XML fragment:\n#{string}" if fragment.children.empty?
  @xml_fragments << string
end