Module: Reality

Defined in:
lib/reality/options.rb,
lib/reality/logging.rb,
lib/reality/base_element.rb

Overview

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: Logging, Options Classes: BaseElement

Class Method Summary collapse

Class Method Details

.base_element(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/reality/base_element.rb', line 40

def self.base_element(options = {})
  type = Class.new(BaseElement)

  container_key = options[:container_key]
  pre_config_code = options[:pre_config_code]
  post_config_code = options[:post_config_code]
  has_name = !!options[:name]
  has_key = !!options[:key]

  code = ''

  parameters = []
  initializers = ''

  if container_key
    code += "attr_reader :#{container_key}\n"
    parameters << container_key
    initializers += <<-RUBY
      @#{container_key} = #{container_key}
      raise "Nil #{container_key} parameter passed to #{self.class} instance" if @#{container_key}.nil?
    RUBY
  end

  if has_key
    code += "attr_reader :key\n"
    parameters << 'key'
    initializers += <<-RUBY
      @key = key
      raise "Nil key parameter passed to #{self.class} instance" if @key.nil?
    RUBY
  end

  if has_name
    code += "attr_reader :name\n"
    parameters << 'name'
    initializers += <<-RUBY
      @name = name
      raise "Nil name parameter passed to #{self.class} instance" if @name.nil?
    RUBY
  end


  parameters = parameters.join(', ')
  parameters += ', ' if parameters.size > 0
  code += <<RUBY
  def initialize(#{parameters}options = {}, &block)
    #{initializers}
    #{pre_config_code}
    super(options, &block)
    #{post_config_code}
  end
RUBY
  type.class_eval(code)
  type
end