dynamics 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift('/Library/RubyMotion/lib')
4
4
 
5
5
  require 'motion/project'
6
6
  require 'rubygems'
7
- require 'bubble-wrap'
7
+ require 'bubble-wrap/core'
8
8
  require 'dynamics'
9
9
 
10
10
  Motion::Project::App.setup do |app|
@@ -7,5 +7,7 @@ class Application < Dynamics::Application
7
7
  # self.layout = 'Navigation'
8
8
  # self.layout = 'Tab Bar'
9
9
  self.layout = 'Tab Nav'
10
+
11
+ # self.authentication = true
10
12
  end
11
13
  end
@@ -0,0 +1,47 @@
1
+
2
+ class AuthenticationForm < Dynamics::Form
3
+
4
+ def on_submit
5
+ super
6
+
7
+ # email = data_source.find('Email')
8
+ # password = data_source.find('Password')
9
+ end
10
+
11
+ protected
12
+
13
+ def viewDidLoad
14
+ super
15
+
16
+ auth_dsl = {
17
+ controller: self,
18
+ sections:
19
+ [
20
+ {
21
+ rows:
22
+ [
23
+ {
24
+ name: "Email",
25
+ type: :string
26
+ },
27
+ {
28
+ name: "Password",
29
+ type: :string
30
+ }
31
+ ]
32
+ },
33
+ {
34
+ rows:
35
+ [
36
+ {
37
+ name: "Login To #{App.name}",
38
+ type: :submit
39
+ }
40
+ ]
41
+ }
42
+ ]
43
+ }
44
+ self.data_source = Dynamics::DataSource.new(auth_dsl)
45
+ end
46
+
47
+ end
@@ -0,0 +1,34 @@
1
+
2
+ module Dynamics
3
+
4
+ class Application
5
+ attr_accessor :authentication, :layout, :window
6
+
7
+ def initialize
8
+ self.layout = ''
9
+ self.authentication = false
10
+ end
11
+
12
+ protected
13
+
14
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
15
+ @window = HomeView.alloc.initWithFrame(UIScreen.mainScreen.bounds)
16
+ @window.makeKeyAndVisible
17
+ case @layout
18
+ when 'Navigation'
19
+ # @@Navigation@@
20
+ # @@End@@
21
+ when 'Tab Bar'
22
+ # @@Tab Bar@@
23
+ # @@End@@
24
+ when 'Tab Nav'
25
+ # @@Tab Nav@@
26
+ # @@End@@
27
+ else
28
+ @window.rootViewController = HomeController.alloc.initWithNibName(nil, bundle: nil)
29
+ end
30
+ true
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,13 @@
1
+
2
+ module Dynamics
3
+
4
+ class Cell
5
+ attr_accessor :control, :row
6
+
7
+ FIELD_BUFFER = Device.iphone? ? 20 : 64
8
+
9
+ def initialize(row)
10
+ self.row = row
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+
2
+ module Dynamics
3
+
4
+ class CellString < Cell
5
+ TEXT_FIELD_TAG = 1000
6
+
7
+ def build_cell(cell)
8
+ self.control = UITextField.alloc.initWithFrame(CGRectZero)
9
+ self.control.tag = TEXT_FIELD_TAG
10
+ self.control.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter
11
+ self.control.textAlignment = UITextAlignmentRight
12
+
13
+ cell.swizzle(:layoutSubviews) do
14
+ def layoutSubviews
15
+ old_layoutSubviews
16
+
17
+ field = viewWithTag(TEXT_FIELD_TAG)
18
+ field.sizeToFit
19
+
20
+ field_frame = field.frame
21
+ field_frame.origin.x = textLabel.frame.origin.x + textLabel.frame.size.width + FIELD_BUFFER
22
+ field_frame.origin.y = ((frame.size.height - field_frame.size.height) / 2.0).round
23
+ field_frame.size.width = frame.size.width - field_frame.origin.x - FIELD_BUFFER
24
+ field.frame = field_frame
25
+ end
26
+ end
27
+
28
+ cell.addSubview(control)
29
+ end
30
+
31
+ def on_select(tableViewDelegate)
32
+ control.becomeFirstResponder
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,20 @@
1
+
2
+ module Dynamics
3
+
4
+ class CellSubmit < Cell
5
+ def build_cell(cell)
6
+ cell.swizzle(:layoutSubviews) do
7
+ def layoutSubviews
8
+ old_layoutSubviews
9
+
10
+ self.textLabel.center = CGPointMake(frame.size.width / 2 - (FIELD_BUFFER / 2), textLabel.center.y)
11
+ self.detailTextLabel.center = CGPointMake(frame.size.width / 2 - (FIELD_BUFFER / 2), detailTextLabel.center.y)
12
+ end
13
+ end
14
+ end
15
+
16
+ def on_select(tableViewDelegate)
17
+ tableViewDelegate.controller.on_submit
18
+ end
19
+ end
20
+ end
@@ -1,31 +1,6 @@
1
1
 
2
2
  module Dynamics
3
3
 
4
- class Application
5
- attr_accessor :layout
6
-
7
- private
8
-
9
- def application(application, didFinishLaunchingWithOptions:launchOptions)
10
- @window = HomeView.alloc.initWithFrame(UIScreen.mainScreen.bounds)
11
- @window.makeKeyAndVisible
12
- case @layout
13
- when 'Navigation'
14
- # @@Navigation@@
15
- # @@End@@
16
- when 'Tab Bar'
17
- # @@Tab Bar@@
18
- # @@End@@
19
- when 'Tab Nav'
20
- # @@Tab Nav@@
21
- # @@End@@
22
- else
23
- @window.rootViewController = HomeController.alloc.initWithNibName(nil, bundle: nil)
24
- end
25
- true
26
- end
27
- end
28
-
29
4
  class Controller < UIViewController
30
5
  attr_accessor :name, :next_controller, :next_view
31
6
 
@@ -33,7 +8,8 @@ module Dynamics
33
8
  super
34
9
 
35
10
  @@index = 0
36
- @name = self.class.name.underscore.split('_')[0].capitalize
11
+ sub_names = self.class.name.underscore.split('_')
12
+ @name = sub_names[0, sub_names.size - 1].collect {|x| x.capitalize }.join
37
13
  case App.delegate.layout
38
14
  when 'Navigation'
39
15
  if @name == 'Home'
@@ -53,7 +29,7 @@ module Dynamics
53
29
  self
54
30
  end
55
31
 
56
- private
32
+ protected
57
33
 
58
34
  def loadView
59
35
  if @next_view.nil?
@@ -62,33 +38,35 @@ module Dynamics
62
38
  self.view = @next_view
63
39
  end
64
40
  end
65
-
41
+
66
42
  def viewDidLoad
67
43
  super
68
44
 
69
45
  case App.delegate.layout
70
46
  when 'Navigation'
71
47
  if !next_controller.nil?
72
- navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithTitle(next_controller.name, style: UIBarButtonItemStyleBordered, target: self, action: 'nextScreen')
48
+ navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithTitle(next_controller.name, style: UIBarButtonItemStyleBordered, target: self, action: 'next_screen')
73
49
  end
74
50
  end
75
51
 
76
52
  case @name
77
53
  when 'Home'
78
- self.view.backgroundColor = UIColor.whiteColor
54
+ self.view.backgroundColor = UIColor.whiteColor
55
+
56
+ if App.delegate.authentication
57
+ auth_controller = AuthenticationForm.alloc.init
58
+ App.delegate.window.addSubview auth_controller.view
59
+ view.removeFromSuperview
60
+ end
79
61
  else
80
62
  self.view.backgroundColor = UIColor.grayColor
81
- end
63
+ end
82
64
  end
83
-
84
- def nextScreen
85
- navigationController.pushViewController(@next_controller, animated: true)
86
- end
87
65
  end
88
66
 
89
- class View < UIView
90
- end
91
-
92
- class Window < UIWindow
93
- end
67
+ private
68
+
69
+ def next_screen
70
+ navigationController.pushViewController(@next_controller, animated: true)
71
+ end
94
72
  end
@@ -0,0 +1,80 @@
1
+
2
+ module Dynamics
3
+
4
+ class DataSource
5
+ attr_accessor :controller, :sections, :table
6
+
7
+ def initialize(params = {})
8
+ self.sections = []
9
+ self.controller = params[:controller]
10
+
11
+ index = 0
12
+ sections = params[:sections] || params["sections"]
13
+ for section in sections
14
+ section = create_section(section.merge({index: index}))
15
+ index += 1
16
+ end
17
+
18
+ self.table = controller.respond_to?(:table_view) ? controller.table_view : controller.tableView
19
+ self.table.delegate = self
20
+ self.table.dataSource = self
21
+ self.table.reloadData
22
+ end
23
+
24
+ def find(name)
25
+ value = nil
26
+ for section in sections
27
+ value = section.find(name)
28
+ if !value.nil?
29
+ break
30
+ end
31
+ end
32
+ value
33
+ end
34
+
35
+ protected
36
+
37
+ def numberOfSectionsInTableView(tableView)
38
+ sections.count
39
+ end
40
+
41
+ def tableView(tableView, cellForRowAtIndexPath: indexPath)
42
+ row = sections[indexPath.section].rows[indexPath.row]
43
+ tableView.dequeueReusableCellWithIdentifier(row.identifier) || row.make_cell
44
+ end
45
+
46
+ def tableView(tableView, commitEditingStyle: editingStyle, forRowAtIndexPath: indexPath)
47
+ row = row_for_index_path(indexPath)
48
+ case editingStyle
49
+ when UITableViewCellEditingStyleInsert
50
+ row.object.on_insert(tableView, self)
51
+ when UITableViewCellEditingStyleDelete
52
+ row.object.on_delete(tableView, self)
53
+ end
54
+ end
55
+
56
+ def tableView(tableView, didSelectRowAtIndexPath: indexPath)
57
+ tableView.deselectRowAtIndexPath(indexPath, animated: true)
58
+ row = sections[indexPath.section].rows[indexPath.row]
59
+ row.cell.on_select(self)
60
+ end
61
+
62
+ def tableView(tableView, numberOfRowsInSection: section)
63
+ @sections[section].rows.count
64
+ end
65
+
66
+ def tableView(tableView, titleForHeaderInSection: section)
67
+ section = @sections[section].name
68
+ end
69
+
70
+ private
71
+
72
+ def create_section(hash = {})
73
+ section = Section.new(hash)
74
+ section.form = self
75
+ self.sections << section
76
+ section
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,16 @@
1
+
2
+ module Dynamics
3
+
4
+ class Form < UITableViewController
5
+ attr_accessor :data_source
6
+
7
+ def init
8
+ self.initWithStyle(UITableViewStyleGrouped)
9
+ self
10
+ end
11
+
12
+ def on_submit
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,7 @@
1
+
2
+ class Object
3
+ def swizzle(method, &block)
4
+ self.class.send(:alias_method, "old_#{method.to_s}".to_sym, method)
5
+ self.instance_eval &block
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+
2
+ module Dynamics
3
+
4
+ class Row
5
+ attr_accessor :cell, :identifier, :index, :name, :section, :type, :value
6
+
7
+ def initialize(params = {})
8
+ self.index = params[:index]
9
+ self.name = params[:name]
10
+ self.type = params[:type]
11
+
12
+ case params[:type]
13
+ when :string
14
+ self.cell = CellString.new(self)
15
+ when :submit
16
+ self.cell = CellSubmit.new(self)
17
+ end
18
+ end
19
+
20
+ def identifier
21
+ section.index.to_s + index.to_s
22
+ end
23
+
24
+ def make_cell
25
+ cell_control = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier: identifier)
26
+ cell_control.accessoryType = UITableViewCellAccessoryNone
27
+ cell_control.editingAccessoryType = UITableViewCellAccessoryNone
28
+ cell_control.textLabel.text = name
29
+
30
+ cell.build_cell(cell_control)
31
+ cell_control
32
+ end
33
+
34
+ def value
35
+ cell.control.text
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+
2
+ module Dynamics
3
+
4
+ class Section
5
+ attr_accessor :form, :index, :rows, :name
6
+
7
+ def initialize(params = {})
8
+ self.rows = []
9
+ self.index = params[:index]
10
+ self.name = params[:name]
11
+
12
+ index = 0
13
+ rows = params[:rows] || params["rows"]
14
+ for row in rows
15
+ row = create_row(row.merge({index: index}))
16
+ index += 1
17
+ end
18
+ end
19
+
20
+ def find(name)
21
+ value = nil
22
+ for row in rows
23
+ if row.name == name
24
+ value = row.value
25
+ break
26
+ end
27
+ end
28
+ value
29
+ end
30
+
31
+ private
32
+
33
+ def create_row(hash = {})
34
+ row = Row.new(hash)
35
+ row.section = self
36
+ self.rows << row
37
+ row
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module Dynamics
3
+
4
+ class View < UIView
5
+ end
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module Dynamics
3
+
4
+ class Window < UIWindow
5
+ end
6
+
7
+ end
@@ -13,12 +13,14 @@ module Dynamics
13
13
  build_dir = File.join(name, 'build')
14
14
  resources_dir = File.join(name, 'resources')
15
15
  lib_dir = File.join(name, 'lib')
16
-
16
+ dynamics_dir = File.join(lib_dir, 'dynamics')
17
+
17
18
  Dir.mkdir(app_dir)
18
19
  Dir.mkdir(build_dir)
19
20
  Dir.mkdir(resources_dir)
20
21
  Dir.mkdir(lib_dir)
21
-
22
+ Dir.mkdir(dynamics_dir)
23
+
22
24
  base_dir = File.join(path, '..', 'base')
23
25
 
24
26
  f = File.new(File.join(name, 'Rakefile'), 'w+')
@@ -40,88 +42,87 @@ module Dynamics
40
42
 
41
43
  controllers_dir = File.join(app_dir, 'controllers')
42
44
  Dir.mkdir(controllers_dir)
45
+
46
+ base_controllers_dir = File.join(base_dir, 'app', 'controllers')
47
+ Dir.foreach(base_controllers_dir) do |controller|
48
+ if controller.include?('.rb')
49
+ f = File.new(File.join(controllers_dir, controller), 'w+')
50
+ f.write(render_code(File.join(base_controllers_dir, controller)))
51
+ f.close
52
+ end
53
+ end
43
54
 
44
- f = File.new(File.join(controllers_dir, 'home_controller.rb'), 'w+')
45
- f.write(render_code(File.join(base_dir, 'app', 'controllers', 'home_controller.rb')))
46
- f.close
47
-
48
- f = File.new(File.join(controllers_dir, 'sub1_controller.rb'), 'w+')
49
- f.write(render_code(File.join(base_dir, 'app', 'controllers', 'sub1_controller.rb')))
50
- f.close
51
-
52
- f = File.new(File.join(controllers_dir, 'sub2_controller.rb'), 'w+')
53
- f.write(render_code(File.join(base_dir, 'app', 'controllers', 'sub2_controller.rb')))
54
- f.close
55
+ # Forms
55
56
 
57
+ forms_dir = File.join(app_dir, 'forms')
58
+ Dir.mkdir(forms_dir)
59
+
60
+ base_forms_dir = File.join(base_dir, 'app', 'forms')
61
+ Dir.foreach(base_forms_dir) do |form|
62
+ if form.include?('.rb')
63
+ f = File.new(File.join(forms_dir, form), 'w+')
64
+ f.write(render_code(File.join(base_forms_dir, form)))
65
+ f.close
66
+ end
67
+ end
68
+
56
69
  # Views
57
70
 
58
71
  views_dir = File.join(app_dir, 'views')
59
72
  Dir.mkdir(views_dir)
60
-
61
- f = File.new(File.join(views_dir, 'home_view.rb'), 'w+')
62
- f.write(render_code(File.join(base_dir, 'app', 'views', 'home_view.rb')))
63
- f.close
64
-
65
- f = File.new(File.join(views_dir, 'sub1_view.rb'), 'w+')
66
- f.write(render_code(File.join(base_dir, 'app', 'views', 'sub1_view.rb')))
67
- f.close
68
-
69
- f = File.new(File.join(views_dir, 'sub2_view.rb'), 'w+')
70
- f.write(render_code(File.join(base_dir, 'app', 'views', 'sub2_view.rb')))
71
- f.close
72
73
 
73
- # Resources
74
-
75
- f = File.new(File.join(resources_dir, 'icon-57.png'), 'w+')
76
- f.write(render_code(File.join(base_dir, 'resources', 'icon@57.png')))
77
- f.close
78
-
79
- f = File.new(File.join(resources_dir, 'icon-74.png'), 'w+')
80
- f.write(render_code(File.join(base_dir, 'resources', 'icon@74.png')))
81
- f.close
82
-
83
- f = File.new(File.join(resources_dir, 'icon-114.png'), 'w+')
84
- f.write(render_code(File.join(base_dir, 'resources', 'icon@114.png')))
85
- f.close
86
-
87
- f = File.new(File.join(resources_dir, 'icon-144.png'), 'w+')
88
- f.write(render_code(File.join(base_dir, 'resources', 'icon@144.png')))
89
- f.close
90
-
91
- f = File.new(File.join(resources_dir, 'home.png'), 'w+')
92
- f.write(render_code(File.join(base_dir, 'resources', 'home.png')))
93
- f.close
74
+ base_views_dir = File.join(base_dir, 'app', 'views')
75
+ Dir.foreach(base_views_dir) do |view|
76
+ if view.include?('.rb')
77
+ f = File.new(File.join(views_dir, view), 'w+')
78
+ f.write(render_code(File.join(base_views_dir, view)))
79
+ f.close
80
+ end
81
+ end
94
82
 
95
- f = File.new(File.join(resources_dir, 'sub1.png'), 'w+')
96
- f.write(render_code(File.join(base_dir, 'resources', 'sub1.png')))
97
- f.close
83
+ # Resources
98
84
 
99
- f = File.new(File.join(resources_dir, 'sub2.png'), 'w+')
100
- f.write(render_code(File.join(base_dir, 'resources', 'sub2.png')))
101
- f.close
85
+ base_resources_dir = File.join(base_dir, 'resources')
86
+ Dir.foreach(base_resources_dir) do |resource|
87
+ if resource.include?('.png')
88
+ f = File.new(File.join(resources_dir, resource), 'w+')
89
+ f.write(render_code(File.join(base_resources_dir, resource)))
90
+ f.close
91
+ end
92
+ end
102
93
  end
103
94
  end
104
95
 
105
- def self.setup_framework(app, path)
106
- lib_dir = File.join(path, 'lib')
107
- templates_dir = File.join(Gem.bin_path('dynamics', 'dynamics').gsub(File.join('bin', 'dynamics'), ''), 'base', 'templates')
108
-
109
- template_code = lib_code = render_code(File.join(templates_dir, 'dynamics.rb'))
110
- template_code.scan(/# @@.+?@@.+?# @@End@@/m) do |block|
111
- block.scan(/^# @@.+?@@/) do |placeholder|
112
- layout = placeholder.gsub('# @@', '').gsub('@', '')
113
- case layout
114
- when 'Navigation' then lib_code = lib_code.gsub(block, navigation_code(path))
115
- when 'Tab Bar' then lib_code = lib_code.gsub(block, tab_bar_code(path))
116
- when 'Tab Nav' then lib_code = lib_code.gsub(block, tab_nav_code(path))
117
- end
96
+ def self.setup_framework(app, path)
97
+ offset = app.files.find_index("./app/application.rb")
98
+
99
+ lib_dir = File.join(path, 'lib', 'dynamics')
100
+ templates_dir = File.join(Gem.bin_path('dynamics', 'dynamics').gsub(File.join('bin', 'dynamics'), ''), 'base', 'templates')
101
+ Dir.foreach(templates_dir) do |template|
102
+ if template.include?('.rb')
103
+ lib_code = render_code(File.join(templates_dir, template))
104
+ if template == 'application.rb'
105
+ template_code = lib_code
106
+ template_code.scan(/# @@.+?@@.+?# @@End@@/m) do |block|
107
+ block.scan(/^# @@.+?@@/) do |placeholder|
108
+ layout = placeholder.gsub('# @@', '').gsub('@', '')
109
+ case layout
110
+ when 'Navigation' then lib_code = lib_code.gsub(block, navigation_code(path))
111
+ when 'Tab Bar' then lib_code = lib_code.gsub(block, tab_bar_code(path))
112
+ when 'Tab Nav' then lib_code = lib_code.gsub(block, tab_nav_code(path))
113
+ end
114
+ end
115
+ end
116
+ end
117
+ f = File.open(File.join(lib_dir, template), 'w+')
118
+ f.write(lib_code)
119
+ f.close
120
+ app.files.insert(offset, File.join(lib_dir, template))
121
+ if template.include?('cell_')
122
+ app.files_dependencies(File.join(lib_dir, template) => File.join(lib_dir, 'cell.rb'))
123
+ end
118
124
  end
119
125
  end
120
- f = File.open(File.join(lib_dir, 'dynamics.rb'), 'w+')
121
- f.write(lib_code)
122
- f.close
123
-
124
- app.files.unshift(File.join(lib_dir, 'dynamics.rb'))
125
126
  end
126
127
 
127
128
  private
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamics
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ng Say Joe
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-08-19 00:00:00 Z
18
+ date: 2012-09-05 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: A framework for developing RubyMotion applications quickly.
@@ -33,6 +33,7 @@ files:
33
33
  - base/app/controllers/home_controller.rb
34
34
  - base/app/controllers/sub1_controller.rb
35
35
  - base/app/controllers/sub2_controller.rb
36
+ - base/app/forms/authentication_form.rb
36
37
  - base/app/views/home_view.rb
37
38
  - base/app/views/sub1_view.rb
38
39
  - base/app/views/sub2_view.rb
@@ -43,7 +44,18 @@ files:
43
44
  - base/resources/icon@74.png
44
45
  - base/resources/sub1.png
45
46
  - base/resources/sub2.png
46
- - base/templates/dynamics.rb
47
+ - base/templates/application.rb
48
+ - base/templates/cell.rb
49
+ - base/templates/cell_string.rb
50
+ - base/templates/cell_submit.rb
51
+ - base/templates/controller.rb
52
+ - base/templates/data_source.rb
53
+ - base/templates/form.rb
54
+ - base/templates/patches.rb
55
+ - base/templates/row.rb
56
+ - base/templates/section.rb
57
+ - base/templates/view.rb
58
+ - base/templates/window.rb
47
59
  - bin/dynamics
48
60
  homepage: http://www.dynamics.io/
49
61
  licenses: []