class Jsup

Constants

VERSION

Attributes

attributes[R]
object[R]

Public Class Methods

new() click to toggle source
# File lib/jsup.rb, line 24
def initialize
  @attributes = Hash.new
end
produce() { |json| ... } click to toggle source
# File lib/jsup.rb, line 14
def self.produce
  new.tap do |json|
    yield json
  end.ojify
end

Public Instance Methods

ojify() click to toggle source
# File lib/jsup.rb, line 20
def ojify
  Oj.dump(attributes)
end

Private Instance Methods

add_attribute(method, attribute) click to toggle source
# File lib/jsup.rb, line 59
def add_attribute(method, attribute)
  attributes[method] = Attribute.new(attribute).value
end
extract_from_hash(attrs) click to toggle source
# File lib/jsup.rb, line 53
def extract_from_hash(attrs)
  attrs.each do |attr|
    add_attribute(attr.to_s, object[attr])
  end
end
extract_from_object(attrs) click to toggle source
# File lib/jsup.rb, line 47
def extract_from_object(attrs)
  attrs.each do |attr|
    add_attribute(attr.to_s, object.public_send(attr)) if object.respond_to?(attr)
  end
end
method_missing(method, *args) { |json| ... } click to toggle source
# File lib/jsup.rb, line 30
def method_missing(method, *args, &block)
  if args.length == 1
    add_attribute(method.to_s, args.first)
  elsif args.length > 1
    @object = args.first
    attrs = args[1..args.length]
    if object.is_a?(Hash)
      extract_from_hash(attrs)
    else
      extract_from_object(attrs)
    end
  elsif block_given?
    nested_attributes = Jsup.new.tap { |json| yield json }.attributes
    add_attribute(method.to_s, nested_attributes)
  end
end