Module: ActiveFacets::Serializer::Base::ClassMethods
- Defined in:
- lib/active_facets/serializer/base.rb
Instance Method Summary collapse
-
#config ⇒ Config
Memoized class getter.
-
#expose(field_set_name, options = {}) ⇒ Object
DSL Defines an alias that can be used instead of a Field Set.
-
#expose_timestamps ⇒ Object
DSL Defines an alias for common ActiveRecord attributes.
-
#extension(api_attribute) ⇒ Object
DSL Defines an attribute extension available for decoration and serialization.
-
#new ⇒ Serializer::Base
Singleton.
-
#resource_class(klass) ⇒ Object
DSL Registers the class type to be serialized.
-
#transform(api_attribute, options = {}) ⇒ Object
DSL Defines a transform to rename or reformat an attribute.
Instance Method Details
#config ⇒ Config
Memoized class getter
88 89 90 |
# File 'lib/active_facets/serializer/base.rb', line 88 def config @config ||= ActiveFacets::Config.new end |
#expose(field_set_name, options = {}) ⇒ Object
DSL Defines an alias that can be used instead of a Field Set
52 53 54 55 56 57 |
# File 'lib/active_facets/serializer/base.rb', line 52 def expose(field_set_name, = {}) field_set_name = field_set_name.to_sym raise ActiveFacets::Errors::ConfigurationError.new(ActiveFacets::Errors::ConfigurationError::ALL_ATTRIBUTES_ERROR_MSG) if field_set_name == :all_attributes raise ActiveFacets::Errors::ConfigurationError.new(ActiveFacets::Errors::ConfigurationError::ALL_FIELDS_ERROR_MSG) if field_set_name == :all config.alias_field_set(field_set_name, .key?(:as) ? [:as] : field_set_name) end |
#expose_timestamps ⇒ Object
DSL Defines an alias for common ActiveRecord attributes
60 61 62 63 64 |
# File 'lib/active_facets/serializer/base.rb', line 60 def transform :created_at, with: :time transform :updated_at, with: :time expose :timestamps, as: [:id, :created_at, :updated_at] end |
#extension(api_attribute) ⇒ Object
DSL Defines an attribute extension available for decoration and serialization
42 43 44 45 46 |
# File 'lib/active_facets/serializer/base.rb', line 42 def extension(api_attribute) config.extensions[api_attribute] = true config.serializers[api_attribute] = api_attribute.to_sym expose api_attribute end |
#new ⇒ Serializer::Base
Singleton
77 78 79 |
# File 'lib/active_facets/serializer/base.rb', line 77 def new @instance ||= super end |
#resource_class(klass) ⇒ Object
DSL Registers the class type to be serialized
67 68 69 |
# File 'lib/active_facets/serializer/base.rb', line 67 def resource_class(klass) config.resource_class = klass end |
#transform(api_attribute, options = {}) ⇒ Object
DSL Defines a transform to rename or reformat an attribute
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_facets/serializer/base.rb', line 22 def transform(api_attribute, = {}) if [:as].present? config.transforms_from[api_attribute] = [:as] config.transforms_to[api_attribute] = [:as] end if [:from].present? config.transforms_from[api_attribute] = [:from] config.transforms_to[api_attribute] ||= [:from] end if [:to].present? config.transforms_from[api_attribute] ||= [:to] config.transforms_to[api_attribute] = [:to] end config.serializers[api_attribute] = [:with] if [:with].present? config.namespaces[api_attribute] = [:within] if [:within].present? expose api_attribute end |