Skip to content

Commit

Permalink
Codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
rwz committed May 30, 2014
1 parent 002e15c commit 7fb7185
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 71 deletions.
75 changes: 37 additions & 38 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
require 'multi_json'

class Jbuilder
# Yields a builder and automatically turns the result into a JSON string
def self.encode(*args, &block)
new(*args, &block).target!
end

@@key_formatter = KeyFormatter.new
@@ignore_nil = false

Expand All @@ -17,13 +12,18 @@ def initialize(options = {}, &block)

@key_formatter = options.fetch(:key_formatter){ @@key_formatter.clone }
@ignore_nil = options.fetch(:ignore_nil, @@ignore_nil)

yield self if block
end

# Yields a builder and automatically turns the result into a JSON string
def self.encode(*args, &block)
new(*args, &block).target!
end

BLANK = ::Object.new

def set!(key, value = BLANK, *args, &block)

result = if block
if BLANK != value
# json.comments @post.comments { |comment| ... }
Expand All @@ -32,7 +32,7 @@ def set!(key, value = BLANK, *args, &block)
else
# json.comments { ... }
# { "comments": ... }
_scope { yield self }
_scope{ yield self }
end
elsif args.empty?
if ::Jbuilder === value
Expand All @@ -52,7 +52,7 @@ def set!(key, value = BLANK, *args, &block)
else
# json.author @post.creator, :name, :email_address
# { "author": { "name": "David", "email_address": "[email protected]" } }
_scope { extract! value, *args }
_scope{ extract! value, *args }
end

_set_value key, result
Expand All @@ -61,7 +61,6 @@ def set!(key, value = BLANK, *args, &block)
alias_method :method_missing, :set!
private :method_missing


# Specifies formatting to be applied to the key. Passing in a name of a function
# will cause that function to be called on the key. So :upcase will upper case
# the key. You can also pass in lambdas for more complex transformations.
Expand Down Expand Up @@ -141,7 +140,7 @@ def self.ignore_nil(value = true)
# end
def child!
@attributes = [] unless ::Array === @attributes
@attributes << _scope { yield self }
@attributes << _scope{ yield self }
end

# Turns the current element into an array and iterates over the passed collection, adding each iteration as
Expand Down Expand Up @@ -246,42 +245,42 @@ def target!

private

def _extract_hash_values(object, *attributes)
attributes.each{ |key| _set_value key, object.fetch(key) }
end
def _extract_hash_values(object, *attributes)
attributes.each{ |key| _set_value key, object.fetch(key) }
end

def _extract_method_values(object, *attributes)
attributes.each{ |key| _set_value key, object.public_send(key) }
end
def _extract_method_values(object, *attributes)
attributes.each{ |key| _set_value key, object.public_send(key) }
end

def _set_value(key, value)
raise NullError.build(key) if @attributes.nil?
def _set_value(key, value)
raise NullError.build(key) if @attributes.nil?
return if @ignore_nil && value.nil?

unless @ignore_nil && value.nil?
@attributes[@key_formatter.format(key)] = value
end
end
key = @key_formatter.format(key)
@attributes[key] = value
end

def _map_collection(collection)
return [] if collection.nil?
def _map_collection(collection)
return [] if collection.nil?

collection.map do |element|
_scope { yield element }
end
collection.map do |element|
_scope{ yield element }
end
end

def _scope
parent_attributes, parent_formatter = @attributes, @key_formatter
@attributes = {}
yield
@attributes
ensure
@attributes, @key_formatter = parent_attributes, parent_formatter
end
def _scope
parent_attributes, parent_formatter = @attributes, @key_formatter
@attributes = {}
yield
@attributes
ensure
@attributes, @key_formatter = parent_attributes, parent_formatter
end

def _mapable_arguments?(value, *args)
value.respond_to?(:map)
end
def _mapable_arguments?(value, *args)
value.respond_to?(:map)
end
end

require 'jbuilder/jbuilder_template' if defined?(ActionView::Template)
Expand Down
67 changes: 34 additions & 33 deletions lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,48 +74,49 @@ def cache_if!(condition, *args, &block)
end

protected
def _handle_partial_options(options)
options.reverse_merge! locals: {}
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
as = options[:as]

if as && options.key?(:collection)
collection = options.delete(:collection) || []
array!(collection) do |member|
options[:locals].merge! as => member
options[:locals].merge! collection: collection
_render_partial options
end
else

def _handle_partial_options(options)
options.reverse_merge! locals: {}
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
as = options[:as]

if as && options.key?(:collection)
collection = options.delete(:collection) || []
array!(collection) do |member|
options[:locals].merge! as => member
options[:locals].merge! collection: collection
_render_partial options
end
else
_render_partial options
end
end

def _render_partial(options)
options[:locals].merge! json: self
@context.render options
end
def _render_partial(options)
options[:locals].merge! json: self
@context.render options
end

def _cache_key(key, options)
if @context.respond_to?(:cache_fragment_name)
# Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
# should be used instead.
@context.cache_fragment_name(key, options)
elsif @context.respond_to?(:fragment_name_with_digest)
# Backwards compatibility for period of time when fragment_name_with_digest was made public.
@context.fragment_name_with_digest(key)
else
::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split('://').last : key, :jbuilder)
end
def _cache_key(key, options)
if @context.respond_to?(:cache_fragment_name)
# Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
# should be used instead.
@context.cache_fragment_name(key, options)
elsif @context.respond_to?(:fragment_name_with_digest)
# Backwards compatibility for period of time when fragment_name_with_digest was made public.
@context.fragment_name_with_digest(key)
else
::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split('://').last : key, :jbuilder)
end
end

private

def _mapable_arguments?(value, *args)
return true if super
options = args.last
::Hash === options && options.key?(:as)
end
def _mapable_arguments?(value, *args)
return true if super
options = args.last
::Hash === options && options.key?(:as)
end
end

class JbuilderHandler
Expand Down

0 comments on commit 7fb7185

Please sign in to comment.