Class: Liquidum::Drop

Inherits:
Liquid::Drop
  • Object
show all
Defined in:
lib/liquidum/drop.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Drop

Returns a new instance of Drop.



7
8
9
# File 'lib/liquidum/drop.rb', line 7

def initialize(object)
  @object = object
end

Class Method Details

.association(*associations) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/liquidum/drop.rb', line 43

def association(*associations)
  associations.each do |name|
    define_method(name) do
      if @object.class.respond_to?(:reflect_on_all_associations)
        relation = @object.class.reflect_on_all_associations.find do |a|
          a.name == name
        end
      end
      return unless relation

      klass = "#{relation.class_name}Drop".classify.safe_constantize

      Rails.logger.warn "No #{klass}" and return unless klass

      if relation.collection?
        # FIXME: The LazyDrop has unexpected side effects for exisiting templates, this needs to be discussed further
        # Enumerator::LazyDrop.new(@object.send(name).lazy.map { |o| klass.new(o) })
        @object.send(name).map { |o| klass.new(o) }
      else
        klass.new(@object.send(name)) unless @object.send(name).nil?
      end
    end
  end
end

Instance Method Details

#is_for_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/liquidum/drop.rb', line 11

def is_for_a?(klass)
  @object.is_a?(klass)
end

#to_gidObject



15
16
17
# File 'lib/liquidum/drop.rb', line 15

def to_gid
  @object.to_gid
end

#to_h(max_level = 3, current_level: 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/liquidum/drop.rb', line 23

def to_h(max_level = 3, current_level: 0)
  return if current_level > max_level

  h = {}

  public_methods(false).each do |pm|
    result = send(pm)

    h[pm] = if result.is_a? ApplicationDrop
              result.to_h(max_level, current_level: current_level + 1)
            elsif result.is_a? Enumerator::Lazy
              result.map { |r| r.to_h(max_level, current_level: current_level + 1) }.force
            else
              result
            end
  end
  h
end

#to_sgidObject



19
20
21
# File 'lib/liquidum/drop.rb', line 19

def to_sgid
  @object.to_sgid
end