lib/argonaut/generator.rb in argonaut-0.0.1 vs lib/argonaut/generator.rb in argonaut-0.0.2
- old
+ new
@@ -1,20 +1,25 @@
+require 'blankslate' unless defined? BlankSlate
require 'json'
module Argonaut
- class Generator
- instance_methods.each {|m| undef_method(m) unless %w(__id__ __send__ to_json to_xml instance_eval nil? is_a? class).include?(m.to_s)}
+ class Generator < BlankSlate
+ # instance_methods.each {|m| undef_method(m) unless %w(__id__ __send__ to_json to_xml instance_eval nil? is_a? class).include?(m.to_s)}
def initialize(array_keys=[], &block)
@array_keys = array_keys.map(&:to_sym)
@hash = {}
block.call(self)
end
def array_keys!(array_keys)
@array_keys = array_keys.map(&:to_sym)
end
+
+ def key(key, value)
+ method_missing(key.to_sym, value)
+ end
def method_missing(key, *values)
value = if block_given?
Generator.new(@array_keys) { |a| yield a }
else
@@ -36,11 +41,12 @@
def respond_to?(method)
true
end
- def to_json(extra=false)
- @hash.to_json(extra)
+ # Sometimes Rails adds a parameter
+ def to_json(filler=nil)
+ JSON.generate(@hash)
end
def to_s
@hash.to_s
end
\ No newline at end of file