Class: Enterprisifier::Marshalling::InstantiatableElement

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

Defined Under Namespace

Classes: AssociationProxy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



29
30
31
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 29

def parent
  @parent
end

Class Method Details

.create(*args, &block) ⇒ Object



35
36
37
38
39
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 35

def create(*args, &block)
  new(*args, &block).tap do |inst|
    inst.run_callbacks(:after_create)
  end
end

.has_attribute(namespace, name, attribute_klass) ⇒ Object



67
68
69
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 67

def has_attribute(namespace, name, attribute_klass)
  (attribute_definitions[namespace] ||= {})[name.to_sym] = attribute_klass
end

.has_many(name, element_klass) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 45

def has_many(name, element_klass)
  define_method("#{name}") do
    next(instance_variable_get("@#{name}")) if instance_variable_set("@#{name}")
    proxy = AssociationProxy.new(self, element_klass)
    instance_variable_set("@#{name}", proxy)
    children << proxy
    proxy
  end
end

.has_one(name, element_klass) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 55

def has_one(name, element_klass)
  attr_reader name
  define_method("build_#{name}") do
    next(send(name)) if send(name)
    element_klass.new.tap do |e|
      instance_variable_set("@#{name}", e)
      e.parent = self
      children << e
    end
  end
end

.inspectObject



41
42
43
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 41

def inspect
  "<Element Class #{element_name} (#{object_id}) ns: #{element_namespace.inspect}>"
end

Instance Method Details

#attribute(name, val = nil) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 81

def attribute(name, val = nil)
  if val
    find_or_initialize_attribute(name).value = val
  else
    find_or_initialize_attribute(name).try(:value)
  end
end

#attributesObject

<< self



73
74
75
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 73

def attributes
  @attributes ||= []
end

#childrenObject



77
78
79
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 77

def children
  @children ||= []
end

#inspectObject



106
107
108
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 106

def inspect
  "<Element #{element_name} (#{object_id}) ns: #{element_namespace.inspect}>"
end

#namespaced_attribute(namespace, name, val = nil) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 89

def namespaced_attribute(namespace, name, val = nil)
  if val
    find_or_initialize_attribute(name, namespace).value = val
  else
    find_or_initialize_attribute(name, namespace).try(:value)
  end
end

#root?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 122

def root?
  !parent
end

#to_xml(builder = nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 97

def to_xml(builder = nil)
  builder ||= Nokogiri::XML::Builder.new { |builder| }
  builder.tag(element_name, value_to_string) do
    set_root_ns(builder) if root?
    flattened_children.each { |c| c.to_xml(builder) }
  end
  builder.to_xml
end

#update(message, obj) ⇒ Object



118
119
120
# File 'lib/enterprisifier/marshalling/instantiatable_element.rb', line 118

def update(message, obj)
  send(:"observe_#{message}", obj) if respond_to?(:"observe_#{message}", true)
end