Class: AbideDevUtils::Ppt::NewObjectBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/ppt/new_obj.rb

Constant Summary collapse

DEFAULT_EXT =
'.pp'
VALID_EXT =
/(\.pp|\.rb)\.erb$/.freeze
TMPL_PATTERN =
/^[a-zA-Z][^\s]*\.erb$/.freeze
OBJ_PREFIX =
/^(c-|d-)/.freeze
PREFIX_TEST_PATH =
{ 'c-' => 'classes', 'd-' => 'defines' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj_type, obj_name, opts: {}, vars: {}) ⇒ NewObjectBuilder

Returns a new instance of NewObjectBuilder.



18
19
20
21
22
23
24
25
26
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 18

def initialize(obj_type, obj_name, opts: {}, vars: {})
  @obj_type = obj_type
  @obj_name = namespace_format(obj_name)
  @opts = opts
  @vars = vars
  class_vars
  validate_class_vars
  @tmpl_data = template_data(@opts.fetch(:tmpl_name, @obj_type))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

If a method gets called on the Hiera object which is not defined, this sends that method call to hash, then doc, then super.



40
41
42
43
44
45
46
47
48
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 40

def method_missing(method, *args, &block)
  return true if ['exist?', 'exists?'].include?(method.to_s)

  return @hash.send(method, *args, &block) if @hash.respond_to?(method)

  return @doc.send(method, *args, &block) if @doc.respond_to?(method)

  super(method, *args, &block)
end

Instance Attribute Details

#obj_nameObject (readonly)

Returns the value of attribute obj_name.



28
29
30
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 28

def obj_name
  @obj_name
end

#obj_pathObject (readonly)

Returns the value of attribute obj_path.



28
29
30
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 28

def obj_path
  @obj_path
end

#obj_typeObject (readonly)

Returns the value of attribute obj_type.



28
29
30
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 28

def obj_type
  @obj_type
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



28
29
30
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 28

def root_dir
  @root_dir
end

#tmpl_dataObject (readonly)

Returns the value of attribute tmpl_data.



28
29
30
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 28

def tmpl_data
  @tmpl_data
end

#tmpl_dirObject (readonly)

Returns the value of attribute tmpl_dir.



28
29
30
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 28

def tmpl_dir
  @tmpl_dir
end

#varsObject (readonly)

Returns the value of attribute vars.



28
29
30
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 28

def vars
  @vars
end

Instance Method Details

#buildObject



30
31
32
33
34
35
36
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 30

def build
  force = @opts.fetch(:force, false)
  obj_cont = force ? true : continue?(obj_path)
  spec_cont = force ? true : continue?(@tmpl_data[:spec_path])
  write_file(obj_path, @tmpl_data[:path]) if obj_cont
  write_file(@tmpl_data[:spec_path], @spec_tmpl) if spec_cont
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Checks the respond_to? of hash, doc, or super

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/abide_dev_utils/ppt/new_obj.rb', line 51

def respond_to_missing?(method_name, include_private = false)
  return true if ['exist?', 'exists?'].include?(method_name.to_s)

  @hash || @doc || super
end