Class: Moonrope::EvalEnvironment
- Inherits:
-
Object
- Object
- Moonrope::EvalEnvironment
- Includes:
- EvalHelpers
- Defined in:
- lib/moonrope/eval_environment.rb
Instance Attribute Summary collapse
-
#action ⇒ Moonrope::Action
The action which invoked this environment.
-
#base ⇒ Moonrope::Base
readonly
The base object.
-
#default_params ⇒ Hash
The default params to be merged with request params.
-
#flags ⇒ Hash
readonly
The flags.
-
#headers ⇒ Hash
readonly
The headers.
-
#request ⇒ Moonrope::Request
readonly
The associated request.
Instance Method Summary collapse
-
#auth ⇒ Object
The authenticated object.
-
#has_structure_for?(structure_name) ⇒ Boolean
Return whether or not a given structure name is valid?.
-
#initialize(base, request, accessors = {}) ⇒ EvalEnvironment
constructor
Initialize a new EvalEnvironment.
-
#method_missing(name, *args) ⇒ Object
Attempts to find an return an accessor from the has.
-
#params ⇒ Hash
All parameters sent for this request including defaults.
-
#reset ⇒ void
Clear all flags & headers from this environment.
-
#set_flag(name, value) ⇒ void
Set a flag which should be returned to the client.
-
#set_header(name, value) ⇒ void
Set a header which should be returned to the client.
-
#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.
-
#structure_for(structure_name) ⇒ Object
Return a Moonrope::Structure object for the provided name.
-
#version ⇒ Integer
The requested API version.
Methods included from EvalHelpers
#error, #paginate, #structured_error
Constructor Details
#initialize(base, request, accessors = {}) ⇒ EvalEnvironment
Initialize a new EvalEnvironment
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
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
#action ⇒ Moonrope::Action
Returns the action which invoked this environment.
22 23 24 |
# File 'lib/moonrope/eval_environment.rb', line 22 def action @action end |
#base ⇒ Moonrope::Base (readonly)
Returns the base object.
7 8 9 |
# File 'lib/moonrope/eval_environment.rb', line 7 def base @base end |
#default_params ⇒ Hash
Returns 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 |
#flags ⇒ Hash (readonly)
Returns the flags.
16 17 18 |
# File 'lib/moonrope/eval_environment.rb', line 16 def flags @flags end |
#headers ⇒ Hash (readonly)
Returns the headers.
13 14 15 |
# File 'lib/moonrope/eval_environment.rb', line 13 def headers @headers end |
#request ⇒ Moonrope::Request (readonly)
Returns the associated request.
10 11 12 |
# File 'lib/moonrope/eval_environment.rb', line 10 def request @request end |
Instance Method Details
#auth ⇒ Object
Returns 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?
168 169 170 |
# File 'lib/moonrope/eval_environment.rb', line 168 def has_structure_for?(structure_name) self.structure_for(structure_name).is_a?(Moonrope::Structure) end |
#params ⇒ Hash
Returns 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 |
#reset ⇒ void
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.
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.
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_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.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/moonrope/eval_environment.rb', line 122 def structure(structure_name_or_object, = {}, = {}) 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 = = 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 = 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 structure.hash(object, .merge(:request => @request)) end |
#structure_for(structure_name) ⇒ Object
Return a Moonrope::Structure object for the provided name
154 155 156 157 158 159 160 161 |
# File 'lib/moonrope/eval_environment.rb', line 154 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 ⇒ Integer
Returns the requested API version.
42 43 44 |
# File 'lib/moonrope/eval_environment.rb', line 42 def version request ? request.version : 1 end |