Class: GenericBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tabry/config_builder/generic_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, *args) ⇒ GenericBuilder

Returns a new instance of GenericBuilder.



43
44
45
46
47
48
# File 'lib/tabry/config_builder/generic_builder.rb', line 43

def initialize(opts, *args)
  @_opts = opts
  includes, not_includes = args.partition { |arg| (arg.is_a?(Symbol) || arg.is_a?(String)) && arg.to_s.start_with?("@") }
  includes.each { |arg| include(arg) }
  _init(*not_includes) if defined?(:_init)
end

Instance Attribute Details

#_optsObject (readonly)

Returns the value of attribute _opts.



4
5
6
# File 'lib/tabry/config_builder/generic_builder.rb', line 4

def _opts
  @_opts
end

Class Method Details

.build(opts = {}, *args, &blk) ⇒ Object



7
8
9
10
11
# File 'lib/tabry/config_builder/generic_builder.rb', line 7

def self.build(opts = {}, *args, &blk)
  builder = new(opts, *args)
  builder.instance_eval(&blk) if blk
  builder._obj.compact
end

.builder_appender(name, key, builder, merge: {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/tabry/config_builder/generic_builder.rb', line 57

def self.builder_appender(name, key, builder, merge: {})
  define_method name.to_sym do |*args, &blk|
    value = _build(builder, *args, &blk).merge(merge.transform_keys(&:to_s))
    _append key, value
  end
end

.init_setters(*setters, split_name_to_aliases: false) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/tabry/config_builder/generic_builder.rb', line 86

def self.init_setters(*setters, split_name_to_aliases: false)
  define_method :_init do |*args|
    args.zip(setters) do |arg, setter|
      _set setter, arg
      _split_name_to_aliases if split_name_to_aliases
    end
  end
end

.simple_setter(name, key = nil) ⇒ Object



50
51
52
53
54
55
# File 'lib/tabry/config_builder/generic_builder.rb', line 50

def self.simple_setter(name, key = nil)
  define_method name do |value|
    value = value.chomp if value.is_a?(String)
    _set (key || name), value
  end
end

Instance Method Details

#_append(list_name, *value_or_values) ⇒ Object



22
23
24
25
26
# File 'lib/tabry/config_builder/generic_builder.rb', line 22

def _append(list_name, *value_or_values)
  _obj[list_name.to_s] ||= []
  _obj[list_name.to_s].concat([value_or_values].flatten)
  value_or_values
end

#_augment_list_item(list_name, item) {|item| ... } ⇒ Object

Yields:

  • (item)


80
81
82
83
84
# File 'lib/tabry/config_builder/generic_builder.rb', line 80

def _augment_list_item(list_name, item)
  item = _obj[list_name].find { |i| i.equal?(item) }
  yield item
  item
end

#_build(builder, *args, &blk) ⇒ Object

Should be used instead of MyBuilder.build to pass in opts



14
15
16
# File 'lib/tabry/config_builder/generic_builder.rb', line 14

def _build(builder, *args, &blk)
  builder.build(_opts, *args, &blk)
end

#_include(_inc) ⇒ Object



76
77
78
# File 'lib/tabry/config_builder/generic_builder.rb', line 76

def _include(_inc)
  raise "_include not implemented for #{self.class}"
end

#_objObject



18
19
20
# File 'lib/tabry/config_builder/generic_builder.rb', line 18

def _obj
  @_obj ||= {}
end

#_set(key, val) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/tabry/config_builder/generic_builder.rb', line 33

def _set(key, val)
  val = val.chomp if val.is_a?(String)
  val = val.to_s if val.is_a?(Symbol)
  if _opts[:names_underscores_to_dashes] && key.to_s == "name"
    val = val.gsub("_", "-") if val.is_a?(String)
    val = val.map { |name| name.to_s.gsub("_", "-") } if val.is_a?(Array)
  end
  _obj[key.to_s] = val
end

#_set_hash(hash_name, key, value) ⇒ Object



28
29
30
31
# File 'lib/tabry/config_builder/generic_builder.rb', line 28

def _set_hash(hash_name, key, value)
  _obj[hash_name.to_s] ||= {}
  _obj[hash_name.to_s][key.to_s] = value
end

#_split_name_to_aliasesObject



95
96
97
98
99
100
101
102
# File 'lib/tabry/config_builder/generic_builder.rb', line 95

def _split_name_to_aliases
  name = _obj["name"]
  if name
    name = name.split(/[ ,]{1,2}/) unless name.is_a?(Array)
    _obj["name"], *aliases = name
    aliases.each { |a| _append :aliases, a }
  end
end

#include(inc) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tabry/config_builder/generic_builder.rb', line 64

def include(inc)
  inc_str = inc.to_s
  raise "include must be a start with @" unless inc_str.start_with?("@")

  include_name = inc_str[1..]
  if _opts[:names_underscores_to_dashes]
    include_name = include_name.gsub("_", "-")
  end

  _include include_name
end