Module: Serega::SeregaPlugins::Formatters

Defined in:
lib/serega/plugins/formatters/formatters.rb

Overview

Plugin :formatters

Allows to define value formatters one time and apply them on any attributes.

Config option ‘config.formatters.add()` can be used to add formatters.

Attribute option ‘:format` now can be used with name of formatter or with callable instance.

Formatters can accept up to 2 parameters (formatted object, context)

Examples:

class AppSerializer < Serega
  plugin :formatters, formatters: {
    iso8601: ->(value) { time.iso8601.round(6) },
    on_off: ->(value) { value ? 'ON' : 'OFF' },
    money: ->(value) { value.round(2) }
    date: DateTypeFormatter # callable
  }
end

class UserSerializer < Serega
  # Additionally we can add formatters via config in subclasses
  config.formatters.add(
    iso8601: ->(value) { time.iso8601.round(6) },
    on_off: ->(value) { value ? 'ON' : 'OFF' },
    money: ->(value) { value.round(2) }
  )

  # Using predefined formatter
  attribute :commission, format: :money
  attribute :is_logined, format: :on_off
  attribute :created_at, format: :iso8601
  attribute :updated_at, format: :iso8601

  # Using `callable` formatter
  attribute :score_percent, format: PercentFormmatter # callable class
  attribute :score_percent, format: proc { |percent| "#{percent.round(2)}%" }
end

Defined Under Namespace

Modules: AttributeInstanceMethods, AttributeNormalizerInstanceMethods, CheckAttributeParamsInstanceMethods, ConfigInstanceMethods Classes: CheckFormatter, CheckOptFormat, FormattersConfig

Class Method Summary collapse

Class Method Details

.after_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Adds config options and runs other callbacks after plugin was loaded

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    Plugin options



92
93
94
95
96
97
# File 'lib/serega/plugins/formatters/formatters.rb', line 92

def self.after_load_plugin(serializer_class, **opts)
  config = serializer_class.config
  config.opts[:formatters] = {}
  config.formatters.add(opts[:formatters] || {})
  config.attribute_keys << :format
end

.before_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Checks requirements and loads additional plugins

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    Plugin options



58
59
60
61
62
63
64
65
66
67
# File 'lib/serega/plugins/formatters/formatters.rb', line 58

def self.before_load_plugin(serializer_class, **opts)
  allowed_keys = %i[formatters]
  opts.each_key do |key|
    next if allowed_keys.include?(key)

    raise SeregaError,
      "Plugin #{plugin_name.inspect} does not accept the #{key.inspect} option. Allowed options:\n" \
      "  - :formatters [Hash<Symbol, #call>] - Formatters (names and according callable values)"
  end
end

.load_plugin(serializer_class, **_opts) ⇒ void

This method returns an undefined value.

Applies plugin code to specific serializer

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • _opts (Hash)

    Plugin options



77
78
79
80
81
82
# File 'lib/serega/plugins/formatters/formatters.rb', line 77

def self.load_plugin(serializer_class, **_opts)
  serializer_class::SeregaConfig.include(ConfigInstanceMethods)
  serializer_class::SeregaAttributeNormalizer.include(AttributeNormalizerInstanceMethods)
  serializer_class::SeregaAttribute.include(AttributeInstanceMethods)
  serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
end

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



47
48
49
# File 'lib/serega/plugins/formatters/formatters.rb', line 47

def self.plugin_name
  :formatters
end