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/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, 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_options, #_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

#_filter_by_path, #_found!

Methods included from DefermentExtension

#_breezy_visit_current, #_deferment_auto?, #_deferment_options, #_deferment_options?, #_set_request_url

Constructor Details

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

Returns a new instance of BreezyTemplate.

Yields:

  • (_self)

Yield Parameters:



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

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

  @attributes = {}
  @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



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/breezy_template.rb', line 92

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.



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

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



51
52
53
# File 'lib/breezy_template.rb', line 51

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.



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

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

Instance Method Details

#_result(value, *args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/breezy_template.rb', line 77

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



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

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 #remove this depednacy
end

#attributes!Object



171
172
173
# File 'lib/breezy_template.rb', line 171

def attributes!
  @attributes
end

#child!Object



121
122
123
124
# File 'lib/breezy_template.rb', line 121

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

#empty!Object



106
107
108
109
110
# File 'lib/breezy_template.rb', line 106

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

#extract!(object, *attributes) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/breezy_template.rb', line 148

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



112
113
114
# File 'lib/breezy_template.rb', line 112

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



184
185
186
# File 'lib/breezy_template.rb', line 184

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

#nil!Object Also known as: null!

Returns the nil JSON.



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

def nil!
  @attributes = nil
end

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



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

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 }
    # elsif args.empty?
    #   _result(value, *args)
    # elsif !args.last.is_a? ::Hash
    #   _merge_block(key){ extract! value, *args }
    else
      _result(value, *args)
      # value
    end
  end

  _set_value key, result
end

#target!Object



175
176
177
178
179
180
# File 'lib/breezy_template.rb', line 175

def target!
  js = _breezy_return(@attributes)

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