class Moonrope::EvalEnvironment

Attributes

action[RW]

@return [Moonrope::Action] the action which invoked this environment

base[R]

@return [Moonrope::Base] the base object

default_params[RW]

@return [Hash] the default params to be merged with request params

flags[R]

@return [Hash] the flags

headers[R]

@return [Hash] the headers

request[R]

@return [Moonrope::Request] the associated request

Public Class Methods

new(base, request, action = nil, accessors = {}) click to toggle source

Initialize a new EvalEnvironment

@param base [Moonrope::Base] @param request [Moonrope::Request] @param accessors [Hash] additional variables which can be made available

# 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

Public Instance Methods

copy_params_to(object, *params_to_copy) click to toggle source

Copy the list of parameters onto the given objectr

# 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) click to toggle source

Return whether or not a given structure name is valid?

@param structure_name [Symbol or String] the structure to return

# File lib/moonrope/eval_environment.rb, line 221
def has_structure_for?(structure_name)
  self.structure_for(structure_name).is_a?(Moonrope::Structure)
end
identity() click to toggle source

@return [Object] the authenticated object

# File lib/moonrope/eval_environment.rb, line 54
def identity
  request ? request.identity : nil
end
method_missing(name, *args) click to toggle source

Attempts to find an return an accessor from the has

@param name [Symbol] the name of the method @param value [void] unused/wnated @return [Object]

Calls superclass method
# 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
params() click to toggle source

@return [Hash] all parameters sent for this request including defaults

# 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
reset() click to toggle source

Clear all flags & headers from this environment.

@return [void]

# File lib/moonrope/eval_environment.rb, line 96
def reset
  @flags = {}
  @headers = {}
end
set_flag(name, value) click to toggle source

Set a flag which should be returned to the client.

@param name [Symbol] the key @param value [String] the value @return [void]

# File lib/moonrope/eval_environment.rb, line 87
def set_flag(name, value)
  @flags[name] = value
end
set_header(name, value) click to toggle source

Set a header which should be returned to the client.

@param name [String] the key @param value [String] the value @return [void]

# 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 = {}) click to toggle source

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

@param structure_name [Moonrope::Structure or Symbol] the structure to be used @param object [Object] the object to pass through the structure @param options [Hash] options to pass to the strucutre hash generator

# 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) click to toggle source

Return a Moonrope::Structure object for the provided name

@param structure_name [Symbol or String] the structure to return

# 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
version() click to toggle source

@return [Integer] the requested API version

# File lib/moonrope/eval_environment.rb, line 47
def version
  request ? request.version : 1
end