Module: AsJson

Extended by:
ActiveSupport::Concern
Defined in:
lib/as_json.rb,
lib/as_json/version.rb,
lib/as_json/json_encoder.rb,
lib/as_json/scoped_json_mapper.rb

Defined Under Namespace

Classes: JsonEncoder, ScopedJsonMapper

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.merge_default_attributes(existing, new_attributes) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/as_json.rb', line 33

def self.merge_default_attributes(existing, new_attributes)
  existing.deep_merge(new_attributes) do |_k, oldval, newval|
    if [oldval, newval].all? { |value| value.is_a?(Array) }
      oldval + newval
    elsif oldval.is_a?(Array) && newval.is_a?(Hash)
      oldval = oldval.map { |k| [k, true] }.to_h
      oldval.deep_merge newval
    elsif newval.is_a?(Array) && oldval.is_a?(Hash)
      newval = newval.map { |k| [k, true] }.to_h
      oldval.deep_merge newval
    else
      newval
    end
  end
end

Instance Method Details

#as_json(opts = {}) ⇒ Object



49
50
51
# File 'lib/as_json.rb', line 49

def as_json(opts = {})
  JsonEncoder.new(self, opts).json
end