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, *args1, **args2, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/json_dumper/base.rb', line 75

def method_missing(name, *args1, **args2, &block)
  if entity.respond_to? name
    if args2.empty?
      entity.send(name, *args1, &block)
    else
      entity.send(name, *args1, **args2, &block)
    end
  else
    super
  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



91
92
93
# File 'lib/json_dumper/base.rb', line 91

def self.instance
  @instance ||= new
end

.method_missing(name, *args1, **args2, &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
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/json_dumper/base.rb', line 10

def self.method_missing(name, *args1, **args2, &block)
  name_sym = name
  name = name.to_s
  value = args1[0]
  if name.start_with?('fetch_')
    return Delayed.new(name.gsub('fetch_', ''), value, args1[1..-1], args2, 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 = if args2.empty?
                   new_dumper.send(name, *(args1[1..-1]), &block)
                 else
                   new_dumper.send(name, *(args1[1..-1]), **args2, &block)
                 end
        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 = if args2.empty?
                 new_dumper.send(name, *(args1[1..-1]), &block)
               else
                 new_dumper.send(name, *(args1[1..-1]), **args2, &block)
               end
      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
    if args2.empty?
      super name_sym, *args1, &block
    else
      super name_sym, *args1, *args2, &block
    end
  end
end

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

Returns:

  • (Boolean)


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

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)


87
88
89
# File 'lib/json_dumper/base.rb', line 87

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