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.

Examples:

class AppSerializer < Serega
  plugin :formatters, formatters: {
    iso8601: ->(value) { time.iso8601.round(6) },
    on_off: ->(value) { value ? 'ON' : 'OFF' },
    money: ->(value) { value.round(2) }
  }
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: proc { |percent| "#{percent.round(2)}%" }
end

Defined Under Namespace

Modules: AttributeInstanceMethods, ConfigInstanceMethods Classes: 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)

    loaded plugins opts



81
82
83
84
85
86
# File 'lib/serega/plugins/formatters/formatters.rb', line 81

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)

    loaded plugins opts



54
55
56
57
58
# File 'lib/serega/plugins/formatters/formatters.rb', line 54

def self.before_load_plugin(serializer_class, **opts)
  if serializer_class.plugin_used?(:batch)
    raise SeregaError, "Plugin `formatters` must be loaded before `batch`"
  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)

    Loaded plugins options



68
69
70
71
# File 'lib/serega/plugins/formatters/formatters.rb', line 68

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

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



43
44
45
# File 'lib/serega/plugins/formatters/formatters.rb', line 43

def self.plugin_name
  :formatters
end