Class: Letterpress::Blueprint

Inherits:
Object
  • Object
show all
Defined in:
lib/letterpress/blueprint.rb,
lib/letterpress/blueprint.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Blueprint

Returns a new instance of Blueprint.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/letterpress/blueprint.rb', line 56

def initialize(*args)
  symbol = args.first.is_a?(Symbol) ? args.first : :default
  options = args.last.is_a?(Hash) ? args.pop : {}
  klass_names = self.class.name.sub('Letterpress::', '').split('::')
  @object = klass_names.inject(Kernel) do |klass_level, name|
    klass_level.const_get(name)
  end.new

  self.class.send(symbol, options).each do |key, value|
    @object.send("#{key}=", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



73
74
75
# File 'lib/letterpress/blueprint.rb', line 73

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

Class Method Details

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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/letterpress/blueprint.rb', line 12

def default(options={}, &block)
  if block_given?
    ghosts[:default] = Ghost.new(serial_number).instance_eval(&block)
  else
    hash = options.merge({})
    default = ghosts[:default]
    (default.keys - options.keys).each do |key|
      hash[key] = default.send(key)
    end
    hash
  end
end

.define(method, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/letterpress/blueprint.rb', line 25

def define(method, &block)
  ghosts[method] = Ghost.new(serial_number).instance_eval(&block)
  self.class.send(:define_method, method) do |options={}|
    hash = options.merge({})
    blueprint = ghosts[method]
    blueprint_keys = blueprint.keys
    (blueprint_keys - options.keys).each do |key|
      hash[key] = blueprint.send(key)
    end

    default = ghosts.fetch(:default, {})
    (default.keys - blueprint_keys - options.keys).each do |key|
      hash[key] = default.send(key)
    end

    hash
  end
end

.make(*args) ⇒ Object



8
9
10
# File 'lib/letterpress/blueprint.rb', line 8

def make(*args)
  new(*args)
end

Instance Method Details

#newObject



69
70
71
# File 'lib/letterpress/blueprint.rb', line 69

def new
  @object
end

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/letterpress/blueprint.rb', line 77

def respond_to_missing?(method, *)
  @object.respond_to?(method)
end