Class: Moonrope::EvalEnvironment
- Inherits:
-
Object
- Object
- Moonrope::EvalEnvironment
- Includes:
- EvalHelpers, Moonrope::EvalHelpers::FilterHelper
- 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
-
#copy_params_to(object, *params_to_copy) ⇒ Object
Copy the list of parameters onto the given objectr.
-
#has_structure_for?(structure_name) ⇒ Boolean
Return whether or not a given structure name is valid?.
-
#identity ⇒ Object
The authenticated object.
-
#initialize(base, request, action = nil, 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 Moonrope::EvalHelpers::FilterHelper
Methods included from EvalHelpers
#error, #paginate, #sort, #structured_error
Constructor Details
#initialize(base, request, action = nil, accessors = {}) ⇒ EvalEnvironment
Initialize a new EvalEnvironment
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
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
#action ⇒ Moonrope::Action
Returns the action which invoked this environment.
26 27 28 |
# File 'lib/moonrope/eval_environment.rb', line 26 def action @action end |
#base ⇒ Moonrope::Base (readonly)
Returns the base object.
11 12 13 |
# File 'lib/moonrope/eval_environment.rb', line 11 def base @base end |
#default_params ⇒ Hash
Returns 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 |
#flags ⇒ Hash (readonly)
Returns the flags.
20 21 22 |
# File 'lib/moonrope/eval_environment.rb', line 20 def flags @flags end |
#headers ⇒ Hash (readonly)
Returns the headers.
17 18 19 |
# File 'lib/moonrope/eval_environment.rb', line 17 def headers @headers end |
#request ⇒ Moonrope::Request (readonly)
Returns the associated request.
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) = params_to_copy.shift if [:from] all_params = action.params.select { |_,p| p[:from_shared_action].include?([: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], ¶m_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?
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 |
#identity ⇒ Object
Returns the authenticated object.
54 55 56 |
# File 'lib/moonrope/eval_environment.rb', line 54 def identity request ? request.identity : nil end |
#params ⇒ Hash
Returns 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 |
#reset ⇒ void
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.
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.
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.
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, = {}, = {}) 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 if .delete(:return) if .empty? && action && action.returns && action.returns[:structure_opts].is_a?(Hash) = action.returns[:structure_opts] end end if request if [:paramable] if [:paramable].is_a?(Hash) [:expansions] = [:paramable][:expansions] [:full] = [:paramable][:full] end if [:paramable] == true || [:paramable].is_a?(Hash) && [:paramable].has_key?(:expansions) if request.params["_expansions"].is_a?(Array) [:expansions] = request.params["_expansions"].map(&:to_sym) if [:paramable].is_a?(Hash) && [:paramable][:expansions].is_a?(Array) whitelist = [:paramable][:expansions] [:expansions].reject! { |e| !whitelist.include?(e) } end end if request.params["_expansions"] == true if [:paramable].is_a?(Hash) if [:paramable][:expansions].is_a?(Array) [:expansions] = [:paramable][:expansions] elsif [:paramable].has_key?(:expansions) [:expansions] = true end else [:expansions] = true end end if request.params["_expansions"] == false [:expansions] = nil end end if request.params.has?("_full") if [:paramable] == true || ([:paramable].is_a?(Hash) && [:paramable].has_key?(:full)) [:full] = !!request.params["_full"] end end end end structure.hash(object, .merge(:request => @request)) end |
#structure_for(structure_name) ⇒ Object
Return a Moonrope::Structure object for the provided name
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 |
#version ⇒ Integer
Returns the requested API version.
47 48 49 |
# File 'lib/moonrope/eval_environment.rb', line 47 def version request ? request.version : 1 end |