class BreezyTemplate

Constants

BLANK
DependencyTracker
NON_ENUMERABLES

Attributes

template_lookup_options[RW]

Public Class Methods

configuration() click to toggle source
# File lib/breezy_template/configuration.rb, line 13
def self.configuration
  @configuration ||= Configuration.new
end
configuration=(config) click to toggle source
# File lib/breezy_template/configuration.rb, line 17
def self.configuration=(config)
  @configuration = config
end
configure() { |configuration| ... } click to toggle source
# File lib/breezy_template/configuration.rb, line 21
def self.configure
  yield configuration
end
encode(*args, &block) click to toggle source

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

# File lib/breezy_template.rb, line 53
def self.encode(*args, &block)
  new(*args, &block).target!
end
ignore_nil(value = true) click to toggle source

Same as instance method ignore_nil! except sets the default.

# File lib/breezy_template.rb, line 114
def self.ignore_nil(value = true)
  @@ignore_nil = value
end
new(context, options = {}) { |self| ... } click to toggle source
# 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

Public Instance Methods

_result(value, *args) { |self| ... } click to toggle source
# 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) click to toggle source
# 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!() click to toggle source
# File lib/breezy_template.rb, line 160
def attributes!
  @attributes
end
child!() { |self| ... } click to toggle source
# File lib/breezy_template.rb, line 118
def child!
  @attributes = [] unless ::Array === @attributes
  @attributes << _scope{ yield self }
end
empty!() click to toggle source
# File lib/breezy_template.rb, line 103
def empty!
  attributes = @attributes
  @attributes = {}
  attributes
end
extract!(object, *attributes) click to toggle source
# 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) click to toggle source
# File lib/breezy_template.rb, line 109
def ignore_nil!(value = true)
  @ignore_nil = value
end
merge!(hash_or_array) click to toggle source

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

# File lib/breezy_template.rb, line 173
def merge!(hash_or_array)
  @attributes = _merge_values(@attributes, hash_or_array)
end
method_missing(*args) click to toggle source
# 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
nil!() click to toggle source

Returns the nil JSON.

# File lib/breezy_template.rb, line 154
def nil!
  @attributes = nil
end
Also aliased as: null!
null!()
Alias for: nil!
set!(key, value = BLANK, *args) click to toggle source
# 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!() click to toggle source
# 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

Private Instance Methods

_args_for_set(*args) click to toggle source
# File lib/breezy_template.rb, line 276
def _args_for_set(*args)
  return args
end
_args_for_set_with_block(*args) click to toggle source
# File lib/breezy_template.rb, line 265
def _args_for_set_with_block(*args)
  key = args[0]

  if ::Hash === args[1] && _extended_options?(args[1])
    options = args[1]
    [key, BLANK, options]
  else
    [key, BLANK]
  end
end
_blank() click to toggle source
# File lib/breezy_template.rb, line 257
def _blank
  BLANK
end
_blank?(value=@attributes) click to toggle source
# File lib/breezy_template.rb, line 253
def _blank?(value=@attributes)
  BLANK == value
end
_breezy_return(results) click to toggle source
# File lib/breezy_template.rb, line 284
def _breezy_return(results)
  "return (#{_dump(results)});"
end
_dump(value) click to toggle source
# File lib/breezy_template.rb, line 288
def _dump(value)
  ::JSON.dump(value)
end
_extended_options?(value) click to toggle source
# File lib/breezy_template.rb, line 280
def _extended_options?(value)
  false
end
_extract_hash_values(object, attributes) click to toggle source
# File lib/breezy_template.rb, line 179
def _extract_hash_values(object, attributes)
  attributes.each{ |key| _set_value key, object.fetch(key) }
end
_extract_method_values(object, attributes) click to toggle source
# File lib/breezy_template.rb, line 183
def _extract_method_values(object, attributes)
  attributes.each{ |key| _set_value key, object.public_send(key) }
end
_is_collection?(object) click to toggle source
# File lib/breezy_template.rb, line 249
def _is_collection?(object)
  _object_respond_to?(object, :map, :count) && NON_ENUMERABLES.none?{ |klass| klass === object }
end
_key(key) click to toggle source
# File lib/breezy_template.rb, line 202
def _key(key)
  @key_formatter ? @key_formatter.format(key) : key.to_s
end
_logger() click to toggle source
# File lib/breezy_template.rb, line 292
def _logger
  ::ActionView::Base.logger
end
_map_collection(collection, options = {}) click to toggle source
# File lib/breezy_template.rb, line 218
def _map_collection(collection, options = {})
  @path.push(nil)
  collection.map.with_index do |element, i|
    if options.has_key? :key
      id_name = options[:key]
      id_val = element[id_name]
      @path[-1] = "#{id_name}=#{id_val}"
    else
      @path[-1] = i
    end

    _mapping_element(element, options, &::Proc.new)

  end - [BLANK]
ensure
  @path.pop
end
_mapping_element(element, options) { |element| ... } click to toggle source
# File lib/breezy_template.rb, line 236
def _mapping_element(element, options)
  _scope { yield element }
end
_merge_block(key) { |self| ... } click to toggle source
# File lib/breezy_template.rb, line 187
def _merge_block(key)
  current_value = _blank? ? BLANK : @attributes.fetch(_key(key), BLANK)
  raise NullError.build(key) if current_value.nil?
  new_value = _scope{ yield self }
  _merge_values(current_value, new_value)
end
_merge_values(current_value, updates) click to toggle source
# File lib/breezy_template.rb, line 194
def _merge_values(current_value, updates)
  # Always use the new values. This is because cached values
  # are no longer a Ruby object. They are JS values and can't
  # be merged.

  updates
end
_object_respond_to?(object, *methods) click to toggle source
# File lib/breezy_template.rb, line 261
def _object_respond_to?(object, *methods)
  methods.all?{ |m| object.respond_to?(m) }
end
_prepare_collection_for_map(collection) click to toggle source
# File lib/breezy_template.rb, line 214
def _prepare_collection_for_map(collection)
  collection
end
_scope() { || ... } click to toggle source
# File lib/breezy_template.rb, line 240
def _scope
  parent_attributes, parent_formatter = @attributes, @key_formatter
  @attributes = BLANK
  yield
  @attributes
ensure
  @attributes, @key_formatter = parent_attributes, parent_formatter
end
_set_value(key, value) click to toggle source
# File lib/breezy_template.rb, line 206
def _set_value(key, value)
  raise NullError.build(key) if @attributes.nil?
  raise ArrayError.build(key) if ::Array === @attributes
  return if @ignore_nil && value.nil? or _blank?(value)
  @attributes = {} if _blank?
  @attributes[_key(key)] = value
end