Class: BeerRecipe::Wrapper

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, recipe = nil) ⇒ Wrapper

Returns a new instance of Wrapper.



2
3
4
5
# File 'lib/beer_recipe/wrapper.rb', line 2

def initialize(record, recipe=nil)
  @record = record
  @recipe = recipe
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



7
8
9
# File 'lib/beer_recipe/wrapper.rb', line 7

def method_missing(method, *args, &block)
  @record.send(method, *args, &block)
end

Class Method Details

.set(recipe, set) ⇒ Object



23
24
25
# File 'lib/beer_recipe/wrapper.rb', line 23

def self.set(recipe, set)
  set.map { |record| self.wrap(record, recipe) }
end

.wrap(record, recipe) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/beer_recipe/wrapper.rb', line 27

def self.wrap(record, recipe)
  wrapper = "#{record.record_type.capitalize}Wrapper".to_sym
  begin
    return BeerRecipe.const_get(wrapper).new(record, recipe)
  rescue NameError
    return self.new(record, recipe)
  end
end

Instance Method Details

#blank?(obj) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/beer_recipe/wrapper.rb', line 19

def blank?(obj)
  obj.nil? || (obj.respond_to?(:empty?) && obj.empty?)
end

#format_methodObject



15
16
17
# File 'lib/beer_recipe/wrapper.rb', line 15

def format_method
  "format_#{record_type}".to_sym
end

#respond_to_missing?(name, flag = true) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/beer_recipe/wrapper.rb', line 11

def respond_to_missing?(name, flag = true)
  @record.respond_to?(name) || super
end