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, &block) ⇒ Formatter
constructor
Initializes a new Formatter.
-
#to_h(service, obj) ⇒ Hash
Formats an object into a ruby hash.
Constructor Details
#initialize(attrs, attr_options, &block) ⇒ Formatter
Initializes a new Formatter.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/compass/json_formatter.rb', line 65 def initialize(attrs, , &block) @attrs = {} @attr_options = [ *attrs, *.keys ].each do |attr| @attrs[attr] = nil define_singleton_method(attr) do |&block| @attrs[attr] = block end end instance_eval(&block) 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 ? service.instance_exec(obj, &formatter) : service.try(method) validate!(method, value) [ method, value ] end].compact end |