Class: LcpRuby::Export::ValueFormatter
- Inherits:
-
Object
- Object
- LcpRuby::Export::ValueFormatter
- Defined in:
- lib/lcp_ruby/export/value_formatter.rb
Overview
Formats field values for export output based on field type and user-selected options.
Constant Summary collapse
- ENUM_MODES =
%w[label key both].freeze
- BOOLEAN_MODES =
%w[true_false yes_no one_zero].freeze
- DATE_MODES =
%w[iso locale custom].freeze
Instance Method Summary collapse
-
#format(value, field_definition: nil, model_name: nil, raw: false, output: :json) ⇒ Object?
Formatted value (String in label mode, native type in raw mode).
-
#initialize(options = {}) ⇒ ValueFormatter
constructor
A new instance of ValueFormatter.
Constructor Details
#initialize(options = {}) ⇒ ValueFormatter
Returns a new instance of ValueFormatter.
15 16 17 |
# File 'lib/lcp_ruby/export/value_formatter.rb', line 15 def initialize( = {}) @options = ( || {}).transform_keys(&:to_s) end |
Instance Method Details
#format(value, field_definition: nil, model_name: nil, raw: false, output: :json) ⇒ Object?
Returns formatted value (String in label mode, native type in raw mode).
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lcp_ruby/export/value_formatter.rb', line 28 def format(value, field_definition: nil, model_name: nil, raw: false, output: :json) return nil if value.nil? type = field_definition&.type || infer_type(value) return format_raw(value, type, output) if raw case type when "enum" format_enum(value, field_definition, model_name) when "date" format_date(value) when "datetime" format_datetime(value) when "boolean" format_boolean(value) when "decimal", "float" format_decimal(value) when "json" value.is_a?(String) ? value : value.to_json else value.to_s end end |