Module: ArcWeld::Resource

Included in:
Asset, AssetCategory, Customer, Group, Location, Network, Zone
Defined in:
lib/arc_weld/resource.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/arc_weld/resource.rb', line 3

def self.included(klass)
  klass.class_eval '@@RESOURCE_PROPERTIES = []'

  klass.class_eval do
    extend ClassMethods, ArcWeld::Helpers

    attr_accessor :name, :id, :externalID, :action

    def initialize(*args)
      Hash[*args].each {|k,v| self.send("#{k}=",v)}
      @action ||= 'insert'
      yield self if block_given?
      self
    end

  end
end

Instance Method Details

#identityObject



52
53
54
55
56
57
58
# File 'lib/arc_weld/resource.rb', line 52

def identity
  if id.nil?
    {:externalID => externalID}
  else
    {:id => id}
  end
end

#identity_hashObject



44
45
46
47
48
49
50
# File 'lib/arc_weld/resource.rb', line 44

def identity_hash
  if id.nil?
    {'@externalID' => externalID}
  else
    {'@id' => id}
  end
end

#parent_refObject



29
30
31
# File 'lib/arc_weld/resource.rb', line 29

def parent_ref
  @parent_ref ||= self.class.toplevel
end

#parent_ref=(containing_group) ⇒ Object



33
34
35
# File 'lib/arc_weld/resource.rb', line 33

def parent_ref=(containing_group)
  @parent_ref = containing_group.ref if containing_group.ref.type=='Group'
end

#parent_uri=(uri) ⇒ Object



37
38
39
40
41
42
# File 'lib/arc_weld/resource.rb', line 37

def parent_uri=(uri)
  top_root = Regexp.new(format('\A%s',self.class.toplevel.uri))
  if top_root.match(uri)
    @parent_ref = ArcWeld::Reference.new(uri: uri)
  end
end

#property_hashObject



60
61
62
63
64
65
# File 'lib/arc_weld/resource.rb', line 60

def property_hash
  self.class.class_properties.reduce({}) do |memo, key|
    memo[key] = self.send(key) unless self.send(key).nil?
    memo
  end
end

#refObject



75
76
77
78
79
80
81
# File 'lib/arc_weld/resource.rb', line 75

def ref
  ArcWeld::Reference.new({
    type: resource_type,
    uri:  ref_uri
    }.merge(identity)
  )
end

#ref_uriObject



71
72
73
# File 'lib/arc_weld/resource.rb', line 71

def ref_uri
  self.class.uri_join(parent_ref.uri, name) unless parent_ref.nil?
end

#relationship_hashObject



67
68
69
# File 'lib/arc_weld/resource.rb', line 67

def relationship_hash
  {} # TODO: damn dirty hack
end

#renderObject



95
96
97
# File 'lib/arc_weld/resource.rb', line 95

def render
  Gyoku.xml(to_h, key_converter: :none)
end

#resource_class_idObject



21
22
23
# File 'lib/arc_weld/resource.rb', line 21

def resource_class_id
  self.class.class_variable_get :@@CLASS_ID
end

#resource_typeObject



25
26
27
# File 'lib/arc_weld/resource.rb', line 25

def resource_type
  self.class.send :resource_type
end

#to_hObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/arc_weld/resource.rb', line 83

def to_h
  resource_h = {
    resource_type => {
      'childOf' => { 'list!' => parent_ref.render },
      '@name'   => name,
      '@action' => action
    }.merge(identity_hash)
     .merge(property_hash)
     .merge(relationship_hash)
  }
end