Class: ColorPlistGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/KMQToolKitGem/color_plist_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plist_hash, prefix, base_path) ⇒ ColorPlistGenerator

Returns a new instance of ColorPlistGenerator.



7
8
9
10
11
12
13
# File 'lib/KMQToolKitGem/color_plist_generator.rb', line 7

def initialize(plist_hash, prefix, base_path)
  @template_folder = File.expand_path('../../templates', __FILE__)
  @plist_hash = plist_hash
  @prefix = prefix
  @base_path = base_path
  check
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



5
6
7
# File 'lib/KMQToolKitGem/color_plist_generator.rb', line 5

def base_path
  @base_path
end

#plist_hashObject (readonly)

Returns the value of attribute plist_hash.



5
6
7
# File 'lib/KMQToolKitGem/color_plist_generator.rb', line 5

def plist_hash
  @plist_hash
end

#prefixObject (readonly)

Returns the value of attribute prefix.



5
6
7
# File 'lib/KMQToolKitGem/color_plist_generator.rb', line 5

def prefix
  @prefix
end

Instance Method Details

#checkObject



31
32
33
34
35
# File 'lib/KMQToolKitGem/color_plist_generator.rb', line 31

def check
  if !@plist_hash.is_a?(Hash)
    raise RuntimeError, "Invalid plist format"
  end
end

#create_color_name_file(names, type) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/KMQToolKitGem/color_plist_generator.rb', line 23

def create_color_name_file(names, type)
  file_name = "UIColor+#{@prefix}ColorName.#{type}"
  template_path = "#{@template_folder}/UIColor+ColorName.#{type}.erb"
  absolute_file_path = "#{@base_path}/#{file_name}"
  variables = OpenStruct.new(names: names, prefix: @prefix)
  Helper.write_to_file(absolute_file_path, template_path, variables.instance_eval{ binding })
end

#generate_color_namesObject



15
16
17
18
19
20
21
# File 'lib/KMQToolKitGem/color_plist_generator.rb', line 15

def generate_color_names
  color_names = @plist_hash.keys.collect do |name|
    name.slice(0, 1).capitalize + name.slice(1..-1)
  end
  create_color_name_file(color_names, 'h')
  create_color_name_file(color_names, 'm')
end