Class: SuperAwesomeResourceSerializer::SerializedFieldInfo
- Inherits:
-
Object
- Object
- SuperAwesomeResourceSerializer::SerializedFieldInfo
- Defined in:
- lib/super_awesome_resource_serializer.rb
Overview
Encapulation of information about a serialized field.
Instance Attribute Summary collapse
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#excluded?(action) ⇒ Boolean
Return true if the field is excluded from the specified action by default.
-
#get(serializer) ⇒ Object
Get the field value in the specified serializer.
-
#getter? ⇒ Boolean
Check if the field has a getter.
-
#initialize(name, options) ⇒ SerializedFieldInfo
constructor
A new instance of SerializedFieldInfo.
-
#set(serializer, value) ⇒ Object
Set the field value in the specified serializer.
-
#setter? ⇒ Boolean
Check if the field has a setter.
Constructor Details
#initialize(name, options) ⇒ SerializedFieldInfo
Returns a new instance of SerializedFieldInfo.
288 289 290 291 292 293 294 |
# File 'lib/super_awesome_resource_serializer.rb', line 288 def initialize (name, ) @name = name.to_sym @getter = [:getter] @setter = [:setter] @element = ([:element] || @name).to_sym @exclude = [:exclude] || [] end |
Instance Attribute Details
#element ⇒ Object (readonly)
Returns the value of attribute element.
286 287 288 |
# File 'lib/super_awesome_resource_serializer.rb', line 286 def element @element end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
286 287 288 |
# File 'lib/super_awesome_resource_serializer.rb', line 286 def name @name end |
Instance Method Details
#excluded?(action) ⇒ Boolean
Return true if the field is excluded from the specified action by default.
339 340 341 342 343 344 345 346 347 |
# File 'lib/super_awesome_resource_serializer.rb', line 339 def excluded? (action) if @exclude == true return true elsif @exclude.is_a?(Array) return @exclude.include?(action) else return @exclude == action end end |
#get(serializer) ⇒ Object
Get the field value in the specified serializer.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/super_awesome_resource_serializer.rb', line 297 def get (serializer) if getter? if @getter.is_a?(Proc) @getter.call(serializer.object) if @getter.is_a?(Proc) else method_name = @getter || name if serializer.respond_to?(method_name) serializer.send(method_name) else serializer.object.send(method_name) end end end end |
#getter? ⇒ Boolean
Check if the field has a getter.
329 330 331 |
# File 'lib/super_awesome_resource_serializer.rb', line 329 def getter? return @getter != false end |
#set(serializer, value) ⇒ Object
Set the field value in the specified serializer.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/super_awesome_resource_serializer.rb', line 313 def set (serializer, value) if setter? if @setter.is_a?(Proc) @setter.call(serializer.object, value) if @setter.is_a?(Proc) else method_name = @setter || "#{name}=" if serializer.respond_to?(method_name) serializer.send(method_name, value) else serializer.object.send(method_name, value) end end end end |
#setter? ⇒ Boolean
Check if the field has a setter.
334 335 336 |
# File 'lib/super_awesome_resource_serializer.rb', line 334 def setter? return @setter != false end |