Module: Alba::Resource::InstanceMethods
- Included in:
- Alba::Resource
- Defined in:
- lib/alba/resource.rb
Overview
Instance methods
Instance Attribute Summary collapse
-
#_key ⇒ Object
readonly
Returns the value of attribute _key.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #initialize(object, params: {}) ⇒ Object
- #key ⇒ Symbol
-
#serializable_hash ⇒ Hash
(also: #to_hash)
A Hash for serialization.
-
#serialize(with: nil) ⇒ String
Get serializer with ‘with` argument and serialize self with it.
Instance Attribute Details
#_key ⇒ Object (readonly)
Returns the value of attribute _key.
28 29 30 |
# File 'lib/alba/resource.rb', line 28 def _key @_key end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
28 29 30 |
# File 'lib/alba/resource.rb', line 28 def object @object end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
28 29 30 |
# File 'lib/alba/resource.rb', line 28 def params @params end |
Instance Method Details
#initialize(object, params: {}) ⇒ Object
32 33 34 35 36 |
# File 'lib/alba/resource.rb', line 32 def initialize(object, params: {}) @object = object @params = params.freeze DSLS.each_key { |name| instance_variable_set("@#{name}", self.class.public_send(name)) } end |
#key ⇒ Symbol
65 66 67 |
# File 'lib/alba/resource.rb', line 65 def key @_key || self.class.name.delete_suffix('Resource').downcase.gsub(/:{2}/, '_').to_sym end |
#serializable_hash ⇒ Hash Also known as: to_hash
A Hash for serialization
59 60 61 |
# File 'lib/alba/resource.rb', line 59 def serializable_hash collection? ? @object.map(&converter) : converter.call(@object) end |
#serialize(with: nil) ⇒ String
Get serializer with ‘with` argument and serialize self with it
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/alba/resource.rb', line 42 def serialize(with: nil) serializer = case with when nil @_serializer || empty_serializer when ->(obj) { obj.is_a?(Class) && obj <= Alba::Serializer } with when Proc inline_extended_serializer(with) else raise ArgumentError, 'Unexpected type for with, possible types are Class or Proc' end serializer.new(self).serialize end |