Class: BreezyTemplate

Inherits:
Object
  • Object
show all
Includes:
CacheExtension, DefermentExtension, PartialDigestor, PartialExtension, SearchExtension
Defined in:
lib/breezy_template.rb,
lib/breezy_template/var.rb,
lib/breezy_template/blank.rb,
lib/breezy_template/errors.rb,
lib/breezy_template/handler.rb,
lib/breezy_template/core_ext.rb,
lib/breezy_template/digestor.rb,
lib/breezy_template/configuration.rb,
lib/breezy_template/key_formatter.rb,
lib/breezy_template/cache_extension.rb,
lib/breezy_template/search_extension.rb,
lib/breezy_template/partial_extension.rb,
lib/breezy_template/dependency_tracker.rb,
lib/breezy_template/deferment_extension.rb

Defined Under Namespace

Modules: CacheExtension, DefermentExtension, DependencyTrackerMethods, Extensions, PartialDigestor, PartialExtension, SearchExtension Classes: ArrayError, Blank, Configuration, Engine, Handler, KeyFormatter, LeafTraversalError, MergeError, NotFoundError, NullError, Var

Constant Summary collapse

BLANK =
Blank.new
NON_ENUMERABLES =
[ ::Struct, ::OpenStruct ].to_set
DependencyTracker =
Class.new(dependency_tracker::ERBTracker)
@@ignore_nil =
false

Constants included from DefermentExtension

DefermentExtension::ACTIVE_MODES

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PartialDigestor

#_partial_digestor

Methods included from PartialExtension

#_normalize_options_for_partial, #_partial_digest, #_partial_options?, #_render_partial, #_render_partial_with_collection, #_set_inline_partial

Methods included from CacheExtension

#_breezy_set_cache, #_cache, #_cache_key, #_cache_options, #_cache_options?, #_fragment_name_with_digest, #_normalize_with_cache_options

Methods included from SearchExtension

#_found!, #_set_search_path_once

Methods included from DefermentExtension

#_breezy_visit_current, #_deferment_auto?, #_deferment_options, #_deferment_options?, #_set_request_url_once

Constructor Details

#initialize(context, options = {}) {|_self| ... } ⇒ BreezyTemplate

Returns a new instance of BreezyTemplate.

Yields:

  • (_self)

Yield Parameters:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/breezy_template.rb', line 39

def initialize(context, options = {})
  @context = context
  @js = []
  @path = []
  @fragments = {}

  @attributes = {}
  @key_formatter = KeyFormatter.new({camelize: :lower})
  @ignore_nil = options.fetch(:ignore_nil, @@ignore_nil)

  yield self if ::Kernel.block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/breezy_template.rb', line 89

def method_missing(*args)
  key = args[0]
  @path.push(key)
  if ::Kernel.block_given?
    args = _args_for_set_with_block(*args)
    set!(*args, &::Proc.new)
  else
    args = _args_for_set(*args)
    set!(*args)
  end
ensure
  @path.pop
end

Class Attribute Details

.template_lookup_optionsObject

Returns the value of attribute template_lookup_options.



32
33
34
# File 'lib/breezy_template.rb', line 32

def template_lookup_options
  @template_lookup_options
end

Class Method Details

.configurationObject



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

def self.configuration
  @configuration ||= Configuration.new
end

.configuration=(config) ⇒ Object



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

def self.configuration=(config)
  @configuration = config
end

.configure {|configuration| ... } ⇒ Object

Yields:



21
22
23
# File 'lib/breezy_template/configuration.rb', line 21

def self.configure
  yield configuration
end

.encode(*args, &block) ⇒ Object

Yields a builder and automatically turns the result into a JSON string



53
54
55
# File 'lib/breezy_template.rb', line 53

def self.encode(*args, &block)
  new(*args, &block).target!
end

.ignore_nil(value = true) ⇒ Object

Same as instance method ignore_nil! except sets the default.



114
115
116
# File 'lib/breezy_template.rb', line 114

def self.ignore_nil(value = true)
  @@ignore_nil = value
end

Instance Method Details

#_result(value, *args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/breezy_template.rb', line 74

def _result(value, *args)
  if ::Kernel.block_given?
    _scope { yield self }
  elsif ::BreezyTemplate === value
    # json.age 32
    # json.person another_jbuilder
    # { "age": 32, "person": { ...  }
    value.attributes!
  else
    # json.age 32
    # { "age": 32 }
    value
  end
end

#array!(collection, *attributes) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/breezy_template.rb', line 123

def array!(collection, *attributes)
  options = attributes.first || {}

  if !collection.respond_to? :member_by
    raise ::NotImplementedError, 'collection must implement member_by(attr, value)'
  end

  if !collection.respond_to? :member_at
    raise ::NotImplementedError, 'collection must implement member_at(index)'
  end


  collection = _prepare_collection_for_map(collection)
  array = if ::Kernel.block_given?
    _map_collection(collection, options, &::Proc.new)
  else
    collection.to_a
  end

  merge! array
end

#attributes!Object



160
161
162
# File 'lib/breezy_template.rb', line 160

def attributes!
  @attributes
end

#child!Object



118
119
120
121
# File 'lib/breezy_template.rb', line 118

def child!
  @attributes = [] unless ::Array === @attributes
  @attributes << _scope{ yield self }
end

#empty!Object



103
104
105
106
107
# File 'lib/breezy_template.rb', line 103

def empty!
  attributes = @attributes
  @attributes = {}
  attributes
end

#extract!(object, *attributes) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/breezy_template.rb', line 145

def extract!(object, *attributes)
  if ::Hash === object
    _extract_hash_values(object, attributes)
  else
    _extract_method_values(object, attributes)
  end
end

#ignore_nil!(value = true) ⇒ Object



109
110
111
# File 'lib/breezy_template.rb', line 109

def ignore_nil!(value = true)
  @ignore_nil = value
end

#merge!(hash_or_array) ⇒ Object

Merges hash or array into current builder. No longer works on Breezy



173
174
175
# File 'lib/breezy_template.rb', line 173

def merge!(hash_or_array)
  @attributes = _merge_values(@attributes, hash_or_array)
end

#nil!Object Also known as: null!

Returns the nil JSON.



154
155
156
# File 'lib/breezy_template.rb', line 154

def nil!
  @attributes = nil
end

#set!(key, value = BLANK, *args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/breezy_template.rb', line 60

def set!(key, value = BLANK, *args)
  result = if ::Kernel.block_given?
    _result(value, *args, &::Proc.new)
  else
    if _is_collection?(value) && !args.last.is_a?(::Hash)
      _scope{ array! value, *args }
    else
      _result(value, *args)
    end
  end

  _set_value key, result
end

#target!Object



164
165
166
167
168
169
# File 'lib/breezy_template.rb', line 164

def target!
  js = _breezy_return(@attributes)

  @js.push(js)
  "(function(){var fragments={};var lastFragmentName;var lastFragmentPath;var cache={};var defers=[];#{@js.join}})()"
end