Class: Enterprisifier::Schema::BaseElement

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks, Observable
Defined in:
lib/enterprisifier/schema/base_element.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, namespaces) ⇒ BaseElement

Returns a new instance of BaseElement.



40
41
42
43
44
45
# File 'lib/enterprisifier/schema/base_element.rb', line 40

def initialize(attributes, namespaces)
  @attributes = attributes
  @children = []
  @namespaces = namespaces
  @text = ''
end

Class Attribute Details

.registered_asObject (readonly)

Returns the value of attribute registered_as.



18
19
20
# File 'lib/enterprisifier/schema/base_element.rb', line 18

def registered_as
  @registered_as
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



11
12
13
# File 'lib/enterprisifier/schema/base_element.rb', line 11

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/enterprisifier/schema/base_element.rb', line 11

def children
  @children
end

#default_namespaceObject (readonly)

Returns the value of attribute default_namespace.



12
13
14
# File 'lib/enterprisifier/schema/base_element.rb', line 12

def default_namespace
  @default_namespace
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



11
12
13
# File 'lib/enterprisifier/schema/base_element.rb', line 11

def namespaces
  @namespaces
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/enterprisifier/schema/base_element.rb', line 10

def parent
  @parent
end

#textObject (readonly)

Returns the value of attribute text.



11
12
13
# File 'lib/enterprisifier/schema/base_element.rb', line 11

def text
  @text
end

Class Method Details

.map_attribute(*camelized_attrs) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/enterprisifier/schema/base_element.rb', line 20

def map_attribute(*camelized_attrs)
  camelized_attrs.each do |attr|
    underscored = attr.to_s.underscore
    class_eval <<-EOM
      def #{underscored}
        return @#{underscored} if defined?(@#{underscored})
        @#{underscored} = read_attribute('#{attr}')
      end
    EOM
  end
end

.register(element_name) ⇒ Object



32
33
34
35
# File 'lib/enterprisifier/schema/base_element.rb', line 32

def register(element_name)
  @registered_as = element_name.to_sym
  Enterprisifier::Schema::ElementFactory.register(element_name, self)
end

Instance Method Details

#add_child(child_element) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/enterprisifier/schema/base_element.rb', line 63

def add_child(child_element)
  child_element.tap do |child|
    @children << child
    child.parent = self
    changed
    notify_observers(child, :child_added)
  end
end

#add_text(text) ⇒ Object



91
92
93
# File 'lib/enterprisifier/schema/base_element.rb', line 91

def add_text(text)
  @text << text
end

#annotationObject



128
129
130
131
# File 'lib/enterprisifier/schema/base_element.rb', line 128

def annotation
  return @annotation if defined?(@annotation)
  @annotation = children.detect { |c| c.registered_as == :annotation }
end

#baseObject



55
56
57
# File 'lib/enterprisifier/schema/base_element.rb', line 55

def base
  namespaced_name(read_attribute("base")) if read_attribute("base")
end

#documentationObject



123
124
125
126
# File 'lib/enterprisifier/schema/base_element.rb', line 123

def documentation
  return nil if children.empty? || annotation.nil?
  annotation.documentation
end

#each_child(skip_annotations = false) ⇒ Object



72
73
74
75
76
# File 'lib/enterprisifier/schema/base_element.rb', line 72

def each_child(skip_annotations = false)
  children.each do |child|
    yield(child) unless skip_annotations && child.registered_as == :annotation
  end
end

#has_child?(*child_types) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/enterprisifier/schema/base_element.rb', line 78

def has_child?(*child_types)
  child_types = child_types.map(&:to_sym)
  children.any? do |child|
    child_types.include?(child.registered_as)
  end
end

#has_children?(skip_annotations = false) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/enterprisifier/schema/base_element.rb', line 85

def has_children?(skip_annotations = false)
  children.any? do |child|
    skip_annotations ? child.registered_as != :annotation : child
  end
end

#nameObject



47
48
49
# File 'lib/enterprisifier/schema/base_element.rb', line 47

def name
  namespaced_name(read_attribute("name")) if read_attribute("name")
end

#read_attribute(attr_name) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/enterprisifier/schema/base_element.rb', line 115

def read_attribute(attr_name)
  if found = @attributes.detect { |attr| attr.localname == attr_name }
    found.value
  else
    nil
  end
end

#refObject



51
52
53
# File 'lib/enterprisifier/schema/base_element.rb', line 51

def ref
  namespaced_name(read_attribute("ref")) if read_attribute("ref")
end

#registered_asObject



59
60
61
# File 'lib/enterprisifier/schema/base_element.rb', line 59

def registered_as
  self.class.registered_as
end

#rootObject



99
100
101
# File 'lib/enterprisifier/schema/base_element.rb', line 99

def root
  parent ? parent.root : self
end

#rootable?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/enterprisifier/schema/base_element.rb', line 95

def rootable?
  false
end

#update(element, state) ⇒ Object



110
111
112
113
# File 'lib/enterprisifier/schema/base_element.rb', line 110

def update(element, state)
  changed
  notify_observers(element, state)
end