Class: Safrano::RelationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/odata/relations.rb

Overview

some usefull stuff

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRelationManager

Returns a new instance of RelationManager.



64
65
66
# File 'lib/odata/relations.rb', line 64

def initialize
  @list = {}
end

Class Method Details

.build_id(arg) ⇒ Object



68
69
70
# File 'lib/odata/relations.rb', line 68

def self.build_id(arg)
  arg.sort.map(&:to_s).join('_')
end

Instance Method Details

#each_relObject

Raises:

  • (ArgumentError)


72
73
74
75
76
# File 'lib/odata/relations.rb', line 72

def each_rel
  raise ArgumentError unless block_given?

  @list.each { |_rid, rel| yield rel }
end

#get(arg) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/odata/relations.rb', line 78

def get(arg)
  rid = Safrano::RelationManager.build_id(arg)
  if @list.key?(rid)
    @list[rid]
  else
    rel = Safrano::Relation.new(arg)
    @list[rid] = rel
  end
end

#get_metadata_xml_attribs(from, to, assoc_type, xnamespace, attrname) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/odata/relations.rb', line 88

def (from, to, assoc_type, xnamespace, attrname)
  rel = get([from, to])
  # use Sequel reflection to get multiplicity (will be used later
  # in 2. Associations below)
  case assoc_type
  # TODO: use multiplicity 1 when needed instead of '0..1'
  when :one_to_one
    rel.set_multiplicity(from, '0..1')
    rel.set_multiplicity(to, '0..1')
  when :one_to_many
    rel.set_multiplicity(from, '0..1')
    rel.set_multiplicity(to, '*')
  when :many_to_one
    rel.set_multiplicity(from, '*')
    rel.set_multiplicity(to, '0..1')
  when :many_to_many
    rel.set_multiplicity(from, '*')
    rel.set_multiplicity(to, '*')
  end
  # <NavigationProperty Name="Supplier"
  # Relationship="ODataDemo.Product_Supplier_Supplier_Products"
  #         FromRole="Product_Supplier" ToRole="Supplier_Products"/>
  { 'Name' => attrname, 'Relationship' => "#{xnamespace}.#{rel.name}",
    'FromRole' => from, 'ToRole' => to }
end