Class: Jsup

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

Defined Under Namespace

Classes: Attribute

Constant Summary collapse

VERSION =
'0.1.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJsup

Returns a new instance of Jsup.



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

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)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Class Method Details

.produceObject



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

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

Instance Method Details

#ojifyObject



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

def ojify
  Oj.dump(attributes)
end