class Pbind::Command::View

Public Class Methods

new(argv) click to toggle source
Calls superclass method Pbind::Command::new
# File lib/pbind/command/view.rb, line 18
def initialize(argv)
  super
  @plist = argv.shift_argument
end

Public Instance Methods

run() click to toggle source
Calls superclass method Pbind::Command#run
# File lib/pbind/command/view.rb, line 28
def run
  parse_plist(@plist)
  super
end
validate!() click to toggle source
# File lib/pbind/command/view.rb, line 23
def validate!
  help! 'The plist is required.' unless @plist
  help! 'The plist is not exists.' unless File.exists?(@plist)
end

Private Instance Methods

parse_plist(plist_path) click to toggle source

Parse the plist and generate code

@return [void]

# File lib/pbind/command/view.rb, line 43
      def parse_plist(plist_path)
        plist = Xcodeproj::Plist.read_from_path(plist_path)
        names = []
        parents = []
        
        puts '// Create views'
        plist["views"].each { |name, view|
          clazz = view['clazz']
          properties = view['properties']

          puts "#{clazz} *#{name} = [[#{clazz} alloc] init]; {"

          properties.each { |key, value|
            puts "    #{name}.#{key} = [PBValueParser valueWithString:@\"#{value}\"];"
          }
          puts "    [self addSubview:#{name}];"

          puts '}'
          names.push name
        }

        puts ''
        puts '// Auto layout'
        puts 'NSDictionary *views = @{'
        names.each { |name|
          puts "    @\"#{name}\": #{name},"
        }
        puts '};'
        puts 'NSArray *formats = @['
        plist["formats"].each { |format|
          puts "    @\"#{format}\","
        }
        puts "];"
        puts <<-CODE
for (NSString *format in formats) {
    NSArray *constraints = [PBLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
    [view addConstraints:constraints];
}
CODE
      end