Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/turbostreamer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TurboStreamer
autoload :Template, 'turbostreamer/template'
autoload :KeyFormatter, 'turbostreamer/key_formatter'
autoload :Errors, 'turbostreamer/errors'

BLANK = ::Object.new

ENCODERS = {
Expand Down Expand Up @@ -99,11 +99,7 @@ def pluck!(object, *attributes)
#
# { "name": David", "age": 32 }, { "name": Jamie", "age": 31 }
def extract!(object, *attributes)
if ::Hash === object
attributes.each{ |key| _set_value key, object.fetch(key) }
else
attributes.each{ |key| _set_value key, object.public_send(key) }
end
_extract(object, attributes)
end

# Turns the current element into an array and iterates over the passed
Expand Down Expand Up @@ -169,7 +165,7 @@ def set!(key, value = BLANK, *args, &block)
else
# json.author @post.creator, :name, :email_address
# { "author": { "name": "David", "email_address": "[email protected]" } }
object!{ extract!(value, *args) }
object!{ _extract(value, args) }
end
end

Expand Down Expand Up @@ -242,11 +238,11 @@ def self.set_default_encoder(mime, encoder, default_options=nil)

@@encoder_options[encoder] = default_options if default_options
end

def self.set_default_encoder_options(encoder, options)
@@encoder_options[encoder] = options
end

def self.has_default_encoder_options?(encoder)
@@encoder_options.has_key?(encoder)
end
Expand Down Expand Up @@ -327,7 +323,7 @@ def child!(value = BLANK, *args, &block)
elsif _eachable_arguments?(value, *args)
_scope{ array!(value, *args) }
else
object!{ extract!(value, *args) }
object!{ _extract(value, args) }
end

end
Expand All @@ -345,6 +341,14 @@ def target!

private

def _extract(object, attributes)
if ::Hash === object
attributes.each{ |key| _set_value key, object.fetch(key) }
else
attributes.each{ |key| _set_value key, object.public_send(key) }
end
end

def _write(key, value)
@encoder.key(_key(key))
@encoder.value(value)
Expand Down