Class: Hypertemplate::Builder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertemplate/builder/base.rb

Direct Known Subclasses

Json, Xml

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

adds a key and value pair to the representation example:

name ‘guilherme’



81
82
83
84
85
# File 'lib/hypertemplate/builder/base.rb', line 81

def method_missing(sym, *args, &block)
  values do |v|
    v.send sym, *args, &block
  end
end

Class Method Details

.build(obj, options = {}, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hypertemplate/builder/base.rb', line 36

def build(obj, options = {}, &block)
  recipe = block_given? ? block : options.delete(:recipe)
  raise Hypertemplate::BuilderError.new("Recipe required to build representation.") unless recipe.respond_to?(:call)

  builder = self.new(obj, options)

  if recipe.arity==-1
    builder.instance_exec(&recipe)
  else
    recipe.call(*[builder, obj, options][0, recipe.arity])
  end

  builder.representation
end

.build_dsl(obj, options = {}, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hypertemplate/builder/base.rb', line 10

def build_dsl(obj, options = {}, &block)
  recipe = block_given? ? block : options.delete(:recipe)
  raise Hypertemplate::BuilderError.new("Recipe required to build representation.") unless recipe.respond_to?(:call)

  builder = self.new(nil, options)
  copy_internal_variables(builder, obj)
  builder.instance_variable_set :@view, obj
  def builder.method_missing(name, *args, &block)
    begin
      @view.send name, *args, &block
    rescue
      super
    end
  end
  builder.instance_exec(obj, &recipe)

  builder.representation
end

.collection_helper_default_options(options = {}, &block) ⇒ Object



55
56
57
# File 'lib/hypertemplate/builder/base.rb', line 55

def collection_helper_default_options(options = {}, &block)
  generic_helper(:collection, options, &block)
end

.copy_internal_variables(builder, obj) ⇒ Object



29
30
31
32
33
34
# File 'lib/hypertemplate/builder/base.rb', line 29

def copy_internal_variables(builder, obj)
  # TODO this is nasty. i am sorry.
  obj.instance_variables.each do |name|
    builder.instance_variable_set name, obj.instance_variable_get(name)
  end
end

.generic_helper(section, options = {}, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hypertemplate/builder/base.rb', line 63

def generic_helper(section, options = {}, &block)
  helper.send(:remove_method, section)
  var_name = "@@more_options_#{section.to_s}".to_sym
  helper.send(:class_variable_set, var_name, options)
  helper.module_eval <<-EOS
    def #{section.to_s}(obj, *args, &block)
      #{var_name}.merge!(args.shift)
      args.unshift(#{var_name})
      #{self.name}.build(obj, *args, &block)
    end
  EOS
end

.helperObject



51
52
53
# File 'lib/hypertemplate/builder/base.rb', line 51

def helper
  @helper_module ||= Hypertemplate::Builder.helper_module_for(self)
end

.member_helper_default_options(type, options = {}, &block) ⇒ Object



59
60
61
# File 'lib/hypertemplate/builder/base.rb', line 59

def member_helper_default_options(type, options = {}, &block)
  generic_helper(:member, options, &block)
end

Instance Method Details

#each(collection, options = {}, &block) ⇒ Object



104
105
106
107
# File 'lib/hypertemplate/builder/base.rb', line 104

def each(collection, options = {}, &block)
  options[:collection] = collection
  members(options, &block)
end

#ns(*args, &block) ⇒ Object



87
88
89
90
91
# File 'lib/hypertemplate/builder/base.rb', line 87

def ns(*args, &block)
  values do |v|
    v.send(:[], *args, &block)
  end
end

#write(sym, val) ⇒ Object

writes a key and value pair to the representation example:

write :name, “guilherme”



97
98
99
100
101
# File 'lib/hypertemplate/builder/base.rb', line 97

def write(sym, val)
  values do |v|
    v.send sym, val
  end
end