Class: SData::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/s_data/payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Payload

Returns a new instance of Payload.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/s_data/payload.rb', line 7

def initialize(params)
  self.builder = Builder::XmlMarkup.new
  self.included = params[:included]
  self.selected = params[:selected]
  self.maximum_precedence = params[:maximum_precedence]
  self.sync = params[:sync]
  self.contract = params[:contract]
  self.expand = params[:expand]
  self.entity = params[:entity]
  self.dataset = params[:dataset]
end

Instance Attribute Details

#builderObject

Returns the value of attribute builder.



4
5
6
# File 'lib/s_data/payload.rb', line 4

def builder
  @builder
end

#contractObject

Returns the value of attribute contract.



4
5
6
# File 'lib/s_data/payload.rb', line 4

def contract
  @contract
end

#datasetObject

Returns the value of attribute dataset.



5
6
7
# File 'lib/s_data/payload.rb', line 5

def dataset
  @dataset
end

#entityObject

Returns the value of attribute entity.



5
6
7
# File 'lib/s_data/payload.rb', line 5

def entity
  @entity
end

#expandObject

Returns the value of attribute expand.



5
6
7
# File 'lib/s_data/payload.rb', line 5

def expand
  @expand
end

#includedObject

Returns the value of attribute included.



4
5
6
# File 'lib/s_data/payload.rb', line 4

def included
  @included
end

#maximum_precedenceObject

Returns the value of attribute maximum_precedence.



4
5
6
# File 'lib/s_data/payload.rb', line 4

def maximum_precedence
  @maximum_precedence
end

#root_node_nameObject

Returns the value of attribute root_node_name.



4
5
6
# File 'lib/s_data/payload.rb', line 4

def root_node_name
  @root_node_name
end

#selectedObject

Returns the value of attribute selected.



4
5
6
# File 'lib/s_data/payload.rb', line 4

def selected
  @selected
end

#syncObject

Returns the value of attribute sync.



4
5
6
# File 'lib/s_data/payload.rb', line 4

def sync
  @sync
end

#xml_nodeObject

Returns the value of attribute xml_node.



5
6
7
# File 'lib/s_data/payload.rb', line 5

def xml_node
  @xml_node
end

Class Method Details

.is_sync?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/s_data/payload.rb', line 24

def self.is_sync?
  false
end

.parse(xml) ⇒ Object



32
33
34
35
36
# File 'lib/s_data/payload.rb', line 32

def self.parse(xml)
  returning new do |payload|
    payload.xml_node = xml
  end
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
22
# File 'lib/s_data/payload.rb', line 19

def ==(other)
  return false unless other.is_a?(SData::Payload)
  [:root_node_name, :included, :selected, :maximum_precedence, :sync, :contract, :entity, :expand].all?{|attr| self.send(attr) == other.send(attr)}
end

#construct_from_array(node_name, node_value, expand, element_precedence, resource_collection) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/s_data/payload.rb', line 91

def construct_from_array(node_name, node_value, expand, element_precedence, resource_collection)
  if resource_collection && resource_collection[:type]
    construct_from_sdata_array(node_name, node_value, expand, element_precedence, resource_collection)
  else
    construct_from_non_sdata_array(node_name, node_value, expand, element_precedence, resource_collection)
  end
end

#construct_from_hash(node_name, node_value, expand, element_precedence, resource_collection) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/s_data/payload.rb', line 119

def construct_from_hash(node_name, node_value, expand, element_precedence, resource_collection)
  expand = :none if (expand == :immediate_children) 
  node = XML::Node.new(qualified(node_name))
  node_value.each_pair do |child_node_name, child_node_data|
    node << generate(formatted(child_node_name), child_node_data, expand, element_precedence, (child_node_data.is_a?(Hash) ? child_node_data[:resource_collection] : nil))
  end
  node
end

#construct_from_non_sdata_array(node_name, node_value, expand, element_precedence, resource_collection) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/s_data/payload.rb', line 110

def construct_from_non_sdata_array(node_name, node_value, expand, element_precedence, resource_collection)
  expand = :none if (expand == :immediate_children) 
  node = XML::Node.new(qualified(node_name))
  node_value.each do |item|
    node << generate(formatted(node_name).singularize, item, expand, element_precedence, (item.is_a?(Hash) ? item[:resource_collection] : nil))
  end
  node      
end

#construct_from_sdata_array(node_name, node_value, expand, element_precedence, resource_collection) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/s_data/payload.rb', line 99

def construct_from_sdata_array(node_name, node_value, expand, element_precedence, resource_collection)
  expand = :none if (expand == :immediate_children)
  node = XML::Node.new(qualified(node_name))
  scoped_children_collection = sdata_collection_url(resource_collection[:parent], resource_collection[:url])
  node['sdata:url'] = scoped_children_collection
  node_value.each do |item|
    node << generate(item.sdata_node_name, item, expand, element_precedence, nil)
  end
  node
end

#construct_from_sdata_model(node_name, node_value, expand, element_precedence, resource_collection) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/s_data/payload.rb', line 64

def construct_from_sdata_model(node_name, node_value, expand, element_precedence, resource_collection)
  node = XML::Node.new(qualified(node_name))
  attributes = node_value.resource_header_attributes(dataset, included)
  attributes.each_pair do |key,value|
    node[key] = value.to_s
  end
  if (node_name == self.root_node_name) || (expand != :none) || included.include?(formatted(node_name))
    expand = :none if (expand == :immediate_children) 
    node_value.payload_map.each_pair do |child_node_name, child_node_data|
      if child_node_data[:type] == :association
        child_expand = :none
      else
        child_expand = (is_sync? ? :all_children : expand)
      end
      collection = ({:parent => node_value, :url => formatted(child_node_name), :type => node_value.payload_map[child_node_name][:type]})
      attribute_method_name = sdata_attribute_method(child_node_data)
      node << generate(formatted(child_node_name), node_value.send(attribute_method_name), child_expand, child_node_data[:precedence], collection)
    end
  end
  node
end

#construct_from_string(node_name, node_value, expand, element_precedence, resource_collection) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/s_data/payload.rb', line 128

def construct_from_string(node_name, node_value, expand, element_precedence, resource_collection)
  node = XML::Node.new(qualified(node_name))
  if !node_value.to_s.blank?
    node << node_value
  else
    node['xsi:nil'] = 'true'
  end
  node
end

#excluded_in_select?(node_name) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/s_data/payload.rb', line 151

def excluded_in_select?(node_name)
  return false if selected.empty?
  return false if node_name == self.root_node_name
  return !selected.include?(node_name.to_s.camelize(:lower))
end

#formatted(node_name) ⇒ Object



142
143
144
# File 'lib/s_data/payload.rb', line 142

def formatted(node_name)
  node_name.to_s.camelize(:lower)
end

#generate(node_name, node_value, expand, element_precedence, resource_collection) ⇒ Object



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

def generate(node_name, node_value, expand, element_precedence, resource_collection)
  self.root_node_name ||= node_name
  return "" if element_precedence > maximum_precedence
  return "" if excluded_in_select?(node_name)
  if node_value.respond_to?(:sdata_options)
    construct_from_sdata_model(node_name, node_value, expand, element_precedence, resource_collection)
  elsif node_value.is_a?(Array)
    construct_from_array(node_name, node_value, expand, element_precedence, resource_collection)
  elsif node_value.is_a?(Hash)
    construct_from_hash(node_name, node_value, expand, element_precedence, resource_collection)
  else
    construct_from_string(node_name, node_value, expand, element_precedence, resource_collection)
  end
end

#generate!Object



45
46
47
# File 'lib/s_data/payload.rb', line 45

def generate!
  @xml_node = generate(entity.sdata_node_name, entity, expand, 1, nil)
end

#is_sync?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/s_data/payload.rb', line 28

def is_sync?
  !sync.nil? ? sync : Payload.is_sync?
end

#qualified(node_name) ⇒ Object



138
139
140
# File 'lib/s_data/payload.rb', line 138

def qualified(node_name)
  "#{contract}:#{(formatted(node_name))}"
end

#sdata_attribute_method(attribute_definition) ⇒ Object

this doesn’t belong here. sdata attribute definitions should be real objects not hashes, so they can figure this stuff out themselves



87
88
89
# File 'lib/s_data/payload.rb', line 87

def sdata_attribute_method(attribute_definition)
  is_sync? ? attribute_definition[:method_name_with_deleted] : attribute_definition[:method_name]
end

#sdata_collection_url(parent, child) ⇒ Object



146
147
148
149
# File 'lib/s_data/payload.rb', line 146

def sdata_collection_url(parent, child)
  #FIXME: adjust for bookkeeper support
  SData.endpoint + "/#{dataset}/" + parent.sdata_node_name + "('#{parent.id}')/" + child
end

#to_xml(*params) ⇒ Object



38
39
40
41
42
43
# File 'lib/s_data/payload.rb', line 38

def to_xml(*params)
  node = XML::Node.new("sdata:payload")
  generate! if @xml_node.nil?
  node << @xml_node
  return node
end