Class: Jsup

Inherits:
Object
  • Object
show all
Defined in:
lib/jsup.rb,
lib/version.rb

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJsup

Returns a new instance of Jsup.



23
24
25
# File 'lib/jsup.rb', line 23

def initialize
  @attributes = Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jsup.rb', line 29

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]
    attrs.each do |attr|
      add_attribute(attr.to_s, object.public_send(attr)) if object.respond_to?(attr)
    end
  elsif block_given?
    nested_attributes = Jsup.new.tap { |json| yield json }.attributes
    add_attribute(method.to_s, nested_attributes)
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/jsup.rb', line 6

def attributes
  @attributes
end

Class Method Details

.produceObject



13
14
15
16
17
# File 'lib/jsup.rb', line 13

def self.produce
  new.tap do |json|
    yield json
  end.ojify
end

Instance Method Details

#ojifyObject



19
20
21
# File 'lib/jsup.rb', line 19

def ojify
  Oj.dump(attributes)
end