Class: Moonrope::EvalEnvironment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EvalHelpers

#error, #paginate

Constructor Details

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

Initialize a new EvalEnvironment

Parameters:



31
32
33
34
35
36
37
# File 'lib/moonrope/eval_environment.rb', line 31

def initialize(base, request, accessors = {})
  @base = base
  @request = request
  @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)


103
104
105
106
107
108
109
110
111
# File 'lib/moonrope/eval_environment.rb', line 103

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:



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

def action
  @action
end

#baseMoonrope::Base (readonly)

Returns the base object.

Returns:



7
8
9
# File 'lib/moonrope/eval_environment.rb', line 7

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



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

def default_params
  @default_params
end

#flagsHash (readonly)

Returns the flags.

Returns:

  • (Hash)

    the flags



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

def flags
  @flags
end

#headersHash (readonly)

Returns the headers.

Returns:

  • (Hash)

    the headers



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

def headers
  @headers
end

#requestMoonrope::Request (readonly)

Returns the associated request.

Returns:



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

def request
  @request
end

Instance Method Details

#authObject

Returns the authenticated object.

Returns:

  • (Object)

    the authenticated object



49
50
51
# File 'lib/moonrope/eval_environment.rb', line 49

def auth
  request ? request.authenticated_user : nil
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)


154
155
156
# File 'lib/moonrope/eval_environment.rb', line 154

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

#paramsHash

Returns all parameters sent for this request including defaults.

Returns:

  • (Hash)

    all parameters sent for this request including defaults



56
57
58
59
60
61
62
# File 'lib/moonrope/eval_environment.rb', line 56

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.



91
92
93
94
# File 'lib/moonrope/eval_environment.rb', line 91

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



82
83
84
# File 'lib/moonrope/eval_environment.rb', line 82

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



71
72
73
# File 'lib/moonrope/eval_environment.rb', line 71

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

#structure(structure_name, object, options = {}) ⇒ 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) (defaults to: {})

    options to pass to the strucutre hash generator



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/moonrope/eval_environment.rb', line 122

def structure(structure_name, object, options = {})
  if object
    structure = structure_for(structure_name)
    if structure.is_a?(Moonrope::Structure)
      structure.hash(object, options.merge(:request => @request))
    else
      raise Moonrope::Errors::Error, "No structure found named '#{structure_name}'"
    end
  else
    nil
  end
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



140
141
142
143
144
145
146
147
# File 'lib/moonrope/eval_environment.rb', line 140

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



42
43
44
# File 'lib/moonrope/eval_environment.rb', line 42

def version
  request ? request.version : 1
end