Class: Trenni::Formatters::Formatter
- Inherits:
-
Mapping::Model
- Object
- Mapping::Model
- Trenni::Formatters::Formatter
- Defined in:
- lib/trenni/formatters/formatter.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #format(object, **options) ⇒ Object (also: #text)
- #format_unspecified(object, **options) ⇒ Object
-
#initialize(**options) ⇒ Formatter
constructor
A new instance of Formatter.
-
#name_for(**options) ⇒ Object
The name of the field, used for the name attribute of an input.
- #nested(name, key = name, klass: self.class) {|formatter| ... } ⇒ Object
- #nested_name(**options) ⇒ Object
- #nested_name_for(**options) ⇒ Object
-
#object ⇒ Object
The target object of the form.
Constructor Details
#initialize(**options) ⇒ Formatter
Returns a new instance of Formatter.
34 35 36 37 38 |
# File 'lib/trenni/formatters/formatter.rb', line 34 def initialize(**) @options = @object = nil end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
82 83 84 |
# File 'lib/trenni/formatters/formatter.rb', line 82 def @options end |
Class Method Details
.for(object, **options) ⇒ Object
30 31 32 |
# File 'lib/trenni/formatters/formatter.rb', line 30 def self.for(object, **) self.new(object: object, **) end |
Instance Method Details
#[](key) ⇒ Object
100 101 102 |
# File 'lib/trenni/formatters/formatter.rb', line 100 def [] key @options[key] end |
#format(object, **options) ⇒ Object Also known as: text
88 89 90 91 92 93 94 95 96 |
# File 'lib/trenni/formatters/formatter.rb', line 88 def format(object, **) method_name = self.method_for_mapping(object) if self.respond_to?(method_name) self.send(method_name, object, **) else format_unspecified(object, **) end end |
#format_unspecified(object, **options) ⇒ Object
84 85 86 |
# File 'lib/trenni/formatters/formatter.rb', line 84 def format_unspecified(object, **) object.to_s end |
#name_for(**options) ⇒ Object
The name of the field, used for the name attribute of an input.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/trenni/formatters/formatter.rb', line 50 def name_for(**) name = [:name] || [:field] if suffix = [:suffix] name = "#{name}#{suffix}" end if nested_name = self.nested_name(**) "#{nested_name}[#{name}]" else name end end |
#nested(name, key = name, klass: self.class) {|formatter| ... } ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/trenni/formatters/formatter.rb', line 68 def nested(name, key = name, klass: self.class) = @options.dup target = self.object.send(name) [:object] = target [:nested_name] = nested_name_for(name: key) formatter = klass.new(**) return formatter unless block_given? yield formatter end |
#nested_name(**options) ⇒ Object
45 46 47 |
# File 'lib/trenni/formatters/formatter.rb', line 45 def nested_name(**) [:nested_name] || @options[:nested_name] end |
#nested_name_for(**options) ⇒ Object
64 65 66 |
# File 'lib/trenni/formatters/formatter.rb', line 64 def nested_name_for(**) name_for(**) end |
#object ⇒ Object
The target object of the form.
41 42 43 |
# File 'lib/trenni/formatters/formatter.rb', line 41 def object @object ||= @options[:object] end |