Class: Safrano::UrlParameters4Coll

Inherits:
UrlParametersBase show all
Defined in:
lib/odata/url_parameters.rb

Overview

url parameters for a collection expand/select + filter/order

Instance Attribute Summary collapse

Attributes inherited from UrlParametersBase

#expand, #select

Instance Method Summary collapse

Methods inherited from UrlParametersBase

#check_expand, #check_select

Constructor Details

#initialize(dataset, params = {}) ⇒ UrlParameters4Coll

Returns a new instance of UrlParameters4Coll.



56
57
58
59
60
61
62
63
64
65
# File 'lib/odata/url_parameters.rb', line 56

def initialize(dataset, params = {})
  super
  # join helper is only needed for odering or filtering
  @jh = @model.join_by_paths_helper if params['$orderby'] || params['$filter']
  @params = params
  @ordby = OrderBase.factory(@params['$orderby'], @jh)
  @filt = FilterBase.factory(@params['$filter'])
  @expand = ExpandBase.factory(@params['$expand'], @model)
  @select = SelectBase.factory(@params['$select'], @model)
end

Instance Attribute Details

#filtObject (readonly)

Returns the value of attribute filt.



53
54
55
# File 'lib/odata/url_parameters.rb', line 53

def filt
  @filt
end

#ordbyObject (readonly)

Returns the value of attribute ordby.



54
55
56
# File 'lib/odata/url_parameters.rb', line 54

def ordby
  @ordby
end

Instance Method Details

#apply_expand_to_dataset(dtcx) ⇒ Object



104
105
106
107
108
# File 'lib/odata/url_parameters.rb', line 104

def apply_expand_to_dataset(dtcx)
  return Contract.valid(dtcx) if @expand.empty?

  @expand.apply_to_dataset(dtcx)
end

#apply_filter_order_to_dataset(dtcx) ⇒ Object

Warning, the @ordby and @filt objects are coupled by way of the join helper



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/odata/url_parameters.rb', line 111

def apply_filter_order_to_dataset(dtcx)
  return Contract.valid(dtcx) if @filt.empty? && @ordby.empty?

  # filter object and join-helper need to be finalized after filter
  # has been parsed and checked
  @filt.finalize(@jh).if_valid do
    # start with the join
    dtcx = @jh.dataset(dtcx)

    @filt.apply_to_dataset(dtcx).map_result! do |dataset|
      dtcx = dataset
      dtcx = @ordby.apply_to_dataset(dtcx)
      dtcx.select_all(@jh.start_model.table_name)
    end
  end
end

#apply_to_dataset(dtcx) ⇒ Object



98
99
100
101
102
# File 'lib/odata/url_parameters.rb', line 98

def apply_to_dataset(dtcx)
  apply_expand_to_dataset(dtcx).if_valid do |dataset|
    apply_filter_order_to_dataset(dataset)
  end
end

#check_allObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/odata/url_parameters.rb', line 129

def check_all
  return Contract::OK unless @params

  # lazy nested proc evaluation.
  # if one check fails, it will be passed up the chain and the ones
  # below will not be evaluated
  check_top.if_valid do
    check_skip.if_valid do
      check_orderby.if_valid do
        check_filter.if_valid do
          check_expand.if_valid do
            check_select.if_valid do
              check_inlinecount
            end
          end
        end
      end
    end
  end
end

#check_filterObject



87
88
89
# File 'lib/odata/url_parameters.rb', line 87

def check_filter
  (err = @filt.parse_error?) ? err : Contract::OK
end

#check_inlinecountObject



81
82
83
84
85
# File 'lib/odata/url_parameters.rb', line 81

def check_inlinecount
  return Contract::OK unless (icp = @params['$inlinecount'])

  (icp == 'allpages') || (icp == 'none') ? Contract::OK : BadRequestInlineCountParamError
end

#check_orderbyObject



91
92
93
94
95
96
# File 'lib/odata/url_parameters.rb', line 91

def check_orderby
  return Contract::OK if @ordby.empty?
  return BadRequestOrderParseError if @ordby.parse_error?

  Contract::OK
end

#check_skipObject



74
75
76
77
78
79
# File 'lib/odata/url_parameters.rb', line 74

def check_skip
  return Contract::OK unless @params['$skip']

  iskip = number_or_nil(@params['$skip'])
  iskip.nil? || iskip.negative? ? BadRequestError : Contract::OK
end

#check_topObject



67
68
69
70
71
72
# File 'lib/odata/url_parameters.rb', line 67

def check_top
  return Contract::OK unless @params['$top']

  itop = number_or_nil(@params['$top'])
  itop.nil? || itop.zero? ? BadRequestError : Contract::OK
end