Class: InJson::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/in_json.rb

Overview

A Definition is a proxy class primarily for evaluating InJson blocks into a Hash definition.

class Post in_json do name email end # => :name=>nil

in_json(:with_posts_and_comments_named) do
 posts do
   title
   comments :only_approved
 end
end # => {:posts=>{:title=>nil, :comments=>:only_approved}}

end

Instance Method Summary collapse

Constructor Details

#initializeDefinition

Returns a new instance of Definition.



35
36
37
# File 'lib/in_json.rb', line 35

def initialize
  @hash = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Hash

Stores any method calls as Hash keys

Returns:

  • the evaluated definition



41
42
43
44
45
46
# File 'lib/in_json.rb', line 41

def method_missing(method, *args, &block)
  method_s = method.to_s
  method = method_s.sub(/^_/, '').to_sym if method_s =~ /^_.*/
  @hash[method] = block_given? ? Definition.new.instance_eval(&block) : args.first
  @hash
end