Class: Moonrope::EvalEnvironment

Inherits:
Object
  • Object
show all
Includes:
EvalHelpers, Moonrope::EvalHelpers::FilterHelper
Defined in:
lib/moonrope/eval_environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Moonrope::EvalHelpers::FilterHelper

#filter

Methods included from EvalHelpers

#error, #paginate, #sort, #structured_error

Constructor Details

#initialize(base, request, action = nil, accessors = {}) ⇒ EvalEnvironment

Initialize a new EvalEnvironment

Parameters:



35
36
37
38
39
40
41
42
# File 'lib/moonrope/eval_environment.rb', line 35

def initialize(base, request, action = nil, accessors = {})
  @base = base
  @request = request
  @action = action
  @accessors = accessors
  @default_params = {}
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Attempts to find an return an accessor from the has

Parameters:

  • name (Symbol)

    the name of the method

  • value (void)

    unused/wnated

Returns:

  • (Object)


108
109
110
111
112
113
114
115
116
# File 'lib/moonrope/eval_environment.rb', line 108

def method_missing(name, *args)
  if @accessors.keys.include?(name.to_sym)
    @accessors[name.to_sym]
  elsif helper = @base.helper(name.to_sym, action ? action.controller : nil)
    instance_exec(*args, &helper.block)
  else
    super
  end
end

Instance Attribute Details

#actionMoonrope::Action

Returns the action which invoked this environment.

Returns:



26
27
28
# File 'lib/moonrope/eval_environment.rb', line 26

def action
  @action
end

#baseMoonrope::Base (readonly)

Returns the base object.

Returns:



11
12
13
# File 'lib/moonrope/eval_environment.rb', line 11

def base
  @base
end

#default_paramsHash

Returns the default params to be merged with request params.

Returns:

  • (Hash)

    the default params to be merged with request params



23
24
25
# File 'lib/moonrope/eval_environment.rb', line 23

def default_params
  @default_params
end

#flagsHash (readonly)

Returns the flags.

Returns:

  • (Hash)

    the flags



20
21
22
# File 'lib/moonrope/eval_environment.rb', line 20

def flags
  @flags
end

#headersHash (readonly)

Returns the headers.

Returns:

  • (Hash)

    the headers



17
18
19
# File 'lib/moonrope/eval_environment.rb', line 17

def headers
  @headers
end

#requestMoonrope::Request (readonly)

Returns the associated request.

Returns:



14
15
16
# File 'lib/moonrope/eval_environment.rb', line 14

def request
  @request
end

Instance Method Details

#copy_params_to(object, *params_to_copy) ⇒ Object

Copy the list of parameters onto the given objectr



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/moonrope/eval_environment.rb', line 228

def copy_params_to(object, *params_to_copy)
  if params_to_copy.first.is_a?(Hash)
    options = params_to_copy.shift
    if options[:from]
      all_params = action.params.select { |_,p| p[:from_shared_action].include?(options[:from]) }
      params_to_copy = params_to_copy + all_params.keys
    end
  end
  params_to_copy.each do |param_name|
    if param_definition = action.params[param_name]
      if params.has?(param_name)
        if param_definition[:apply]
          instance_exec(object, params[param_name], &param_definition[:apply])
        elsif object.respond_to?("#{param_name}=")
          object.send("#{param_name}=", params[param_name])
        end
      end
    else
      raise Moonrope::Errors::Error, "Attempted to copy parameter #{parameter} to object but no definition exists"
    end
  end
end

#has_structure_for?(structure_name) ⇒ Boolean

Return whether or not a given structure name is valid?

Parameters:

  • structure_name (Symbol or String)

    the structure to return

Returns:

  • (Boolean)


221
222
223
# File 'lib/moonrope/eval_environment.rb', line 221

def has_structure_for?(structure_name)
  self.structure_for(structure_name).is_a?(Moonrope::Structure)
end

#identityObject

Returns the authenticated object.

Returns:

  • (Object)

    the authenticated object



54
55
56
# File 'lib/moonrope/eval_environment.rb', line 54

def identity
  request ? request.identity : nil
end

#paramsHash

Returns all parameters sent for this request including defaults.

Returns:

  • (Hash)

    all parameters sent for this request including defaults



61
62
63
64
65
66
67
# File 'lib/moonrope/eval_environment.rb', line 61

def params
  @params ||= begin
    params = request ? request.params : ParamSet.new
    params._defaults = @default_params
    params
  end
end

#resetvoid

This method returns an undefined value.

Clear all flags & headers from this environment.



96
97
98
99
# File 'lib/moonrope/eval_environment.rb', line 96

def reset
  @flags = {}
  @headers = {}
end

#set_flag(name, value) ⇒ void

This method returns an undefined value.

Set a flag which should be returned to the client.

Parameters:

  • name (Symbol)

    the key

  • value (String)

    the value



87
88
89
# File 'lib/moonrope/eval_environment.rb', line 87

def set_flag(name, value)
  @flags[name] = value
end

#set_header(name, value) ⇒ void

This method returns an undefined value.

Set a header which should be returned to the client.

Parameters:

  • name (String)

    the key

  • value (String)

    the value



76
77
78
# File 'lib/moonrope/eval_environment.rb', line 76

def set_header(name, value)
  @headers[name.to_s] = value
end

#structure(structure_name_or_object, object_or_options = {}, options_if_structure_name = {}) ⇒ Object

Generate a new structure from the core DSL for the given object and return a hash or nil if the structure doesn’t exist.

Parameters:

  • structure_name (Moonrope::Structure or Symbol)

    the structure to be used

  • object (Object)

    the object to pass through the structure

  • options (Hash)

    options to pass to the strucutre hash generator



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/moonrope/eval_environment.rb', line 127

def structure(structure_name_or_object, object_or_options = {}, options_if_structure_name = {})

  if structure_name_or_object.is_a?(Symbol) || structure_name_or_object.is_a?(String) || structure_name_or_object.is_a?(Moonrope::Structure)
    structure_name = structure_name_or_object
    object = object_or_options
    options = options_if_structure_name
  elsif structure_name_or_object.class.name.respond_to?(:underscore)
    structure_name = structure_name_or_object.class.name.underscore.to_sym
    object = structure_name_or_object
    options = object_or_options
  else
    raise Moonrope::Errors::Error, "Could not determine structure name"
  end

  if object.nil?
    return nil
  end

  structure = structure_for(structure_name)

  unless structure.is_a?(Moonrope::Structure)
    raise Moonrope::Errors::Error, "No structure found named '#{structure_name}'"
  end

  if options.delete(:return)
    if options.empty? && action && action.returns && action.returns[:structure_opts].is_a?(Hash)
      options = action.returns[:structure_opts]
    end
  end

  if request
    if options[:paramable]
      if options[:paramable].is_a?(Hash)
        options[:expansions] = options[:paramable][:expansions]
        options[:full] = options[:paramable][:full]
      end

      if options[:paramable] == true || options[:paramable].is_a?(Hash) && options[:paramable].has_key?(:expansions)
        if request.params["_expansions"].is_a?(Array)
          options[:expansions] = request.params["_expansions"].map(&:to_sym)
          if options[:paramable].is_a?(Hash) && options[:paramable][:expansions].is_a?(Array)
            whitelist = options[:paramable][:expansions]
            options[:expansions].reject! { |e| !whitelist.include?(e) }
          end
        end

        if request.params["_expansions"] == true
          if options[:paramable].is_a?(Hash)
            if options[:paramable][:expansions].is_a?(Array)
              options[:expansions] = options[:paramable][:expansions]
            elsif options[:paramable].has_key?(:expansions)
              options[:expansions] = true
            end
          else
            options[:expansions] = true
          end
        end

        if request.params["_expansions"] == false
          options[:expansions] = nil
        end

      end

      if request.params.has?("_full")
        if options[:paramable] == true || (options[:paramable].is_a?(Hash) && options[:paramable].has_key?(:full))
          options[:full] = !!request.params["_full"]
        end
      end
    end
  end

  structure.hash(object, options.merge(:request => @request))
end

#structure_for(structure_name) ⇒ Object

Return a Moonrope::Structure object for the provided name

Parameters:

  • structure_name (Symbol or String)

    the structure to return



207
208
209
210
211
212
213
214
# File 'lib/moonrope/eval_environment.rb', line 207

def structure_for(structure_name)
  structure = case structure_name
  when Symbol, String       then @base.structure(structure_name.to_sym)
  when Moonrope::Structure  then structure_name
  else
    false
  end
end

#versionInteger

Returns the requested API version.

Returns:

  • (Integer)

    the requested API version



47
48
49
# File 'lib/moonrope/eval_environment.rb', line 47

def version
  request ? request.version : 1
end