Class: AbstractBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/abstract_builder.rb,
lib/abstract_builder/lazy_cache.rb,
lib/abstract_builder/null_cache.rb

Defined Under Namespace

Classes: LazyCache, NullCache

Constant Summary collapse

@@format_key =
nil
@@ignore_value =
nil
@@cache_store =
NullCache.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbstractBuilder

Returns a new instance of AbstractBuilder.



21
22
23
24
25
26
27
# File 'lib/abstract_builder.rb', line 21

def initialize
  @format_key = @@format_key
  @ignore_value = @@ignore_value
  @cache_store = @@cache_store
  @lazy_cache = LazyCache.new(@@cache_store)
  @stack = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/abstract_builder.rb', line 101

def method_missing(*args, &block)
  if args.length == 2 && block
    array!(args[0], args[1], &block)
  elsif args.length == 1 && block
    block!(args[0], &block)
  elsif args.length == 2
    set!(args[0], args[1])
  else
    raise ArgumentError, <<~EOF.chomp
      Expected 1 argument without a block, 0 arguments with a block or 1 argument with a block.

      This is `AbstractBuilder#set!', `AbstractBuilder#block!' or `AbstractBuilder#array!' signatures, example:

          builder.content post.content

          builder.meta do |meta_builder|
            meta_builder.hashtags post.hashtags
          end

          builder.comments post.comments do |comment_builder, comment|
            comment_builder.content comment.content
          end

      Received `#{args[0]}' with #{args.length - 1} argument#{'s' if args.length > 2} #{block ? "with a block" : "without a block"}.
    EOF
  end
end

Class Method Details

.cache_store!(cache_store) ⇒ Object



9
10
11
# File 'lib/abstract_builder.rb', line 9

def self.cache_store!(cache_store)
  @@cache_store = cache_store
end

.format_key!(&block) ⇒ Object



13
14
15
# File 'lib/abstract_builder.rb', line 13

def self.format_key!(&block)
  @@format_key = block
end

.ignore_value!(&block) ⇒ Object



17
18
19
# File 'lib/abstract_builder.rb', line 17

def self.ignore_value!(&block)
  @@ignore_value = block
end

Instance Method Details

#array!(key, collection, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/abstract_builder.rb', line 66

def array!(key, collection, &block)
  values = collection.map do |item|
    builder = _inherit
    block.call(builder, item)
    builder.data!
  end

  set! key, values
end

#block!(key, &block) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/abstract_builder.rb', line 58

def block!(key, &block)
  builder = _inherit
  block.call(builder)
  value = builder.data!

  set! key, value unless value.empty?
end

#cache!(cache_key, options = {}, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/abstract_builder.rb', line 76

def cache!(cache_key, options = {}, &block)
  cache_key = _compute_cache_key(cache_key)

  @lazy_cache.add(cache_key, options) do
    builder = _inherit
    block.call(builder)
    builder.data!
  end
end

#cache_store!(cache_store) ⇒ Object



37
38
39
40
# File 'lib/abstract_builder.rb', line 37

def cache_store!(cache_store)
  @cache_store = cache_store
  @lazy_cache = LazyCache.new(cache_store)
end

#call(object, *keys) ⇒ Object



52
53
54
55
56
# File 'lib/abstract_builder.rb', line 52

def call(object, *keys)
  keys.each do |key|
    set! key, object.public_send(key)
  end
end

#data!Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/abstract_builder.rb', line 86

def data!
  data = {}

  @stack.each do |(key, value)|
    key = _format_key(key)
    data[key] = value unless _ignore_value?(value)
  end

  @lazy_cache.resolve.each do |value|
    data.merge!(value)
  end

  data
end

#format_key!(&block) ⇒ Object



29
30
31
# File 'lib/abstract_builder.rb', line 29

def format_key!(&block)
  @format_key = block
end

#ignore_value!(&block) ⇒ Object



33
34
35
# File 'lib/abstract_builder.rb', line 33

def ignore_value!(&block)
  @ignore_value = block
end

#merge!(value) ⇒ Object



46
47
48
49
50
# File 'lib/abstract_builder.rb', line 46

def merge!(value)
  value.each_pair do |key, value|
    set! key, value
  end
end

#set!(key, value) ⇒ Object



42
43
44
# File 'lib/abstract_builder.rb', line 42

def set!(key, value)
  @stack << [key, value]
end