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’



89
90
91
92
93
# File 'lib/hypertemplate/builder/base.rb', line 89

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

Class Method Details

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hypertemplate/builder/base.rb', line 44

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hypertemplate/builder/base.rb', line 18

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



63
64
65
# File 'lib/hypertemplate/builder/base.rb', line 63

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

.copy_internal_variables(builder, obj) ⇒ Object



37
38
39
40
41
42
# File 'lib/hypertemplate/builder/base.rb', line 37

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

.extend_media_types(media_types) ⇒ Object



14
15
16
# File 'lib/hypertemplate/builder/base.rb', line 14

def extend_media_types(media_types)
  @media_types.push(*media_types)
end

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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hypertemplate/builder/base.rb', line 71

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



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

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

.media_typesObject



10
11
12
# File 'lib/hypertemplate/builder/base.rb', line 10

def media_types
  @media_types
end

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



67
68
69
# File 'lib/hypertemplate/builder/base.rb', line 67

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

Instance Method Details

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



112
113
114
115
# File 'lib/hypertemplate/builder/base.rb', line 112

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

#ns(*args, &block) ⇒ Object



95
96
97
98
99
# File 'lib/hypertemplate/builder/base.rb', line 95

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”



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

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