Class: JayZ::Blueprint

Inherits:
Object
  • Object
show all
Defined in:
lib/jay_z/blueprint.rb,
lib/jay_z/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
# File 'lib/jay_z/blueprint.rb', line 56

def initialize(*args)
  symbol = args.first.is_a?(Symbol) ? args.first : :default
  options = args.last.is_a?(Hash) ? args.pop : {}
  @object = Kernel.const_get(self.class.name.sub('JayZ::', '')).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



70
71
72
# File 'lib/jay_z/blueprint.rb', line 70

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/jay_z/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/jay_z/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).each do |key|
      hash[key] = default.send(key)
    end

    hash
  end
end

.make(*args) ⇒ Object



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

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

Instance Method Details

#newObject



66
67
68
# File 'lib/jay_z/blueprint.rb', line 66

def new
  @object
end

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/jay_z/blueprint.rb', line 74

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