Class: APIQL::Context
- Inherits:
-
Object
- Object
- APIQL::Context
- Defined in:
- lib/apiql.rb
Instance Method Summary collapse
-
#initialize(binder, *fields) ⇒ Context
constructor
A new instance of Context.
- #inject_delegators(target) ⇒ Object
- #parse_params(list) ⇒ Object
- #render_value(value, schema) ⇒ Object
Constructor Details
#initialize(binder, *fields) ⇒ Context
243 244 245 246 247 248 249 250 251 |
# File 'lib/apiql.rb', line 243 def initialize(binder, *fields) @fields = fields fields.each do |field| instance_variable_set("@#{field}", binder.send(field)) class_eval do attr_accessor field end end end |
Instance Method Details
#inject_delegators(target) ⇒ Object
253 254 255 256 257 258 259 |
# File 'lib/apiql.rb', line 253 def inject_delegators(target) @fields.each do |field| target.class_eval do delegate field, to: :@context end end end |
#parse_params(list) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/apiql.rb', line 261 def parse_params(list) list&.split(',')&.map(&:strip)&.map do |name| if reg = name.match(/\A[a-zA-Z]\w*\z/) params[name] else begin Float(name) rescue name end end end end |
#render_value(value, schema) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/apiql.rb', line 275 def render_value(value, schema) if value.is_a? Hash HashEntity.new(value, self).render(schema) elsif value.respond_to?(:each) && value.respond_to?(:map) value.map do |object| render_value(object, schema) end elsif APIQL::simple_class?(value) value else "#{value.class.name}::Entity".constantize.new(value, self).render(schema) end end |