Class: JsonDumper::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/json_dumper/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity = nil) ⇒ Base

Returns a new instance of Base.



5
6
7
8
# File 'lib/json_dumper/base.rb', line 5

def initialize(entity = nil)
  self.return_nils = entity.nil?
  self.entity = entity
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/json_dumper/base.rb', line 63

def method_missing(name, *args, &block)
  if entity.respond_to? name
    entity.send(name, *args, &block)
  else
    super name, *args, &block
  end
end

Instance Attribute Details

#entityObject

Returns the value of attribute entity.



3
4
5
# File 'lib/json_dumper/base.rb', line 3

def entity
  @entity
end

#return_nilsObject

Returns the value of attribute return_nils.



3
4
5
# File 'lib/json_dumper/base.rb', line 3

def return_nils
  @return_nils
end

Class Method Details

.instanceObject



75
76
77
# File 'lib/json_dumper/base.rb', line 75

def self.instance
  @instance ||= new
end

.method_missing(name, *args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/json_dumper/base.rb', line 10

def self.method_missing(name, *args, &block)
  name_sym = name
  name = name.to_s
  value = args[0]
  if name.start_with?('fetch_')
    return Delayed.new(name.gsub('fetch_', ''), value, args[1..-1], self)
  end
  if instance.respond_to?(name)
    if value.respond_to?(:each) && !value.respond_to?(:each_pair)
      value.map do |ent|
        new_dumper = new(ent)
        if new_dumper.return_nils
          return nil
        end
        result = new_dumper.send(name, *(args[1..-1]), &block)
        if result.respond_to?(:each) && !result.respond_to?(:each_pair)
          result = DumperArray.new(result)
        else
          result = DumperHash.new(result)
        end
        preload_method_name = "#{name}_preload"
        result.preload = instance.respond_to?(preload_method_name) ? instance.send(preload_method_name) : {}
        result
      end
    elsif name.end_with?('_preload')
      instance.send(name)
    else
      new_dumper = new(value)
      if new_dumper.return_nils
        return nil
      end
      result = new_dumper.send(name, *(args[1..-1]), &block)
      if result.respond_to?(:each) && !result.respond_to?(:each_pair)
        result = DumperArray.new(result)
      else
        result = DumperHash.new(result)
      end

      preload_method_name = "#{name}_preload"
      result.preload = instance.respond_to?(preload_method_name) ? instance.send(preload_method_name) : {}
      result
    end
  elsif name.end_with?('_preload') && instance.respond_to?(name.gsub('_preload', ''))
    return {}
  else
    super name_sym, *args, &block
  end
end

.respond_to_missing?(method_name, _ = false) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/json_dumper/base.rb', line 59

def self.respond_to_missing?(method_name, _ = false)
  new.respond_to? method_name
end

Instance Method Details

#respond_to_missing?(method_name, _ = false) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/json_dumper/base.rb', line 71

def respond_to_missing?(method_name, _ = false)
  entity.respond_to? method_name
end