Class: SBConstants::ObjcConstantWriter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/sbconstants/objc_constant_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_source, int_out, imp_out, templates_dir) ⇒ ObjcConstantWriter



8
9
10
11
12
13
# File 'lib/sbconstants/objc_constant_writer.rb', line 8

def initialize data_source, int_out, imp_out, templates_dir
  super(data_source)
  @int_out       = int_out
  @imp_out       = imp_out
  @templates_dir = templates_dir
end

Instance Attribute Details

#templates_dirObject (readonly)

Returns the value of attribute templates_dir.



6
7
8
# File 'lib/sbconstants/objc_constant_writer.rb', line 6

def templates_dir
  @templates_dir
end

Instance Method Details

#default_templates_dirObject



20
21
22
# File 'lib/sbconstants/objc_constant_writer.rb', line 20

def default_templates_dir
  @default_templates_dir ||= File.dirname(__FILE__) + '/templates'
end

#headerObject



24
25
26
# File 'lib/sbconstants/objc_constant_writer.rb', line 24

def header
  template_with_file "#import <Foundation/Foundation.h>\n\n", File.open(template_file_path("objc_header.erb")).read
end

#implementationObject



28
29
30
31
# File 'lib/sbconstants/objc_constant_writer.rb', line 28

def implementation
  head = %Q{#import "<%= File.basename(options.output_path) %>.h"\n\n}
  template_with_file head, File.open(template_file_path("objc_implementation.erb")).read
end

#present_constants(section) ⇒ Object



40
41
42
# File 'lib/sbconstants/objc_constant_writer.rb', line 40

def present_constants(section)
  section.constants.map { |constant| [ sanitise_key(constant), constant ] }
end

#template_file_path(basename) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/sbconstants/objc_constant_writer.rb', line 44

def template_file_path basename
  if templates_dir && File.exist?("#{templates_dir}/#{basename}")
    "#{templates_dir}/#{basename}"
  else
    "#{default_templates_dir}/#{basename}"
  end
end

#template_with_file(head, body) ⇒ Object



33
34
35
36
37
38
# File 'lib/sbconstants/objc_constant_writer.rb', line 33

def template_with_file head, body
  @head = head
  @body = body
  pre_processed_template = ERB.new(File.open(template_file_path("objc_body.erb")).read, nil, '<>').result(binding)
  ERB.new(pre_processed_template, nil, '<>').result(binding)
end

#writeObject



15
16
17
18
# File 'lib/sbconstants/objc_constant_writer.rb', line 15

def write
  @int_out.puts header
  @imp_out.puts implementation
end