Class: Jsup
- Inherits:
-
Object
- Object
- Jsup
- Defined in:
- lib/jsup.rb,
lib/version.rb
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Jsup
constructor
A new instance of Jsup.
- #ojify ⇒ Object
Constructor Details
#initialize ⇒ Jsup
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/jsup.rb', line 6 def attributes @attributes end |
Class Method Details
.produce ⇒ Object
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
#ojify ⇒ Object
19 20 21 |
# File 'lib/jsup.rb', line 19 def ojify Oj.dump(attributes) end |