Class: Compass::JsonFormatter::Formatter
- Inherits:
-
Object
- Object
- Compass::JsonFormatter::Formatter
- Defined in:
- lib/compass/json_formatter.rb
Overview
Formats an object into a JSON hash.
Instance Method Summary collapse
-
#initialize(attrs, attr_options) ⇒ Formatter
constructor
Initializes a new Formatter.
-
#to_h(service, obj) ⇒ Hash
Formats an object into a ruby hash.
Constructor Details
#initialize(attrs, attr_options) ⇒ Formatter
Initializes a new Formatter.
68 69 70 71 72 73 74 75 76 |
# File 'lib/compass/json_formatter.rb', line 68 def initialize(attrs, ) @attrs = attrs.index_with { nil } @attr_options = attrs.each do |attr| define_singleton_method(attr) do |&block| @attrs[attr] = block end end end |
Instance Method Details
#to_h(service, obj) ⇒ Hash
Formats an object into a ruby hash.
82 83 84 85 86 87 88 89 |
# File 'lib/compass/json_formatter.rb', line 82 def to_h(service, obj) Hash[@attrs.map do |method, formatter| value = formatter ? formatter.call(obj) : service.try(method) validate!(method, value) [ method, value ] end].compact end |