Class: Serialize::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, block) ⇒ Generator

Returns a new instance of Generator.



9
10
11
12
13
# File 'lib/serialize/generator.rb', line 9

def initialize(object, block)
  @object = object
  @block  = block
  @id = @object.id if @object.respond_to? :id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/serialize/generator.rb', line 22

def method_missing(*args, &block)
  method = args.first

  if @object.respond_to? method
    @object.send *args, &block
  else
    raise NoMethodError
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Instance Method Details

#to_hashObject



15
16
17
18
19
20
# File 'lib/serialize/generator.rb', line 15

def to_hash
  case @block.arity
    when 0 then instance_eval &@block
    when 1 then @block.call(@object)
  end
end