motion-loco 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be786608ca687bfacb8732b95724bdd70a4144c8
4
- data.tar.gz: e64bf30c3cf9a785335f8790e5a077488adcc79f
3
+ metadata.gz: 8365d10e57e456f796f6847f82ed4c65d0ca82c1
4
+ data.tar.gz: aacf5c5adfc39986d23e112c66eb941e900bda0e
5
5
  SHA512:
6
- metadata.gz: 01a24f5e39072fad6e5c2d42c9f5af640418af29c2922695bca657cc8d38fd5532082f4d20adc1c065335d1babfd55d0493c825dc0d0525821f94a9142d8fab0
7
- data.tar.gz: 8da3ec624e6e72a3b99168c4ac4a53f8a4eda39e1c4012d32eafb1cb32cc3fc9be441b997674df2677760e5a94bccecb7301a728f3967e4d701bdb35c77ea096
6
+ metadata.gz: 993356f6d2402a523fcfb23f1d9f5fd95850a8f286dc71c286e857dba84fc8a11ae0702a42f5e3ed70f28effe34766f053afe1f3a41658114beba2e3186afe57
7
+ data.tar.gz: 0935b838d4ef0981bd77d1811419e98181592d86cf8d788cd83cd2f1eba3f1408d0ab4bce5563331ba80015a134f7fb7874813e6d7e91c4db0fb69264d1e8637
data/README.md CHANGED
@@ -106,41 +106,6 @@ PersonController.content = @person
106
106
  @label.text # "Brian Pattison"
107
107
  ```
108
108
 
109
- ### Loco::UI::TableView
110
-
111
- A `Loco::UI::TableView` is used for to easily bind a collection of objects
112
- to a `UITableView` and each item in the collection to a reusable `UITableViewCell`.
113
-
114
- ```ruby
115
- class MyTableViewCell < Loco::UI::TableViewCell
116
- # The `view_setup` method is called the first time the cell is created.
117
- # Bindings can be made to the item assigned to the cell
118
- # by binding to `parentView.content`.
119
- def view_setup
120
- @label = Loco::UI::Label.alloc.initWithFrame(
121
- textBinding: 'parentView.content.first_name',
122
- height: 30,
123
- left: 60,
124
- right: 30,
125
- top: 5
126
- )
127
- self.addSubview(@label)
128
- end
129
- end
130
-
131
- class MyTableView < Loco::UI::TableView
132
- item_view_class MyTableViewCell
133
- end
134
-
135
- @table_view = MyTableView.alloc.initWithFrame(
136
- content: [Person.new(first_name: 'Brian'), Person.new(first_name: 'Kirsten')],
137
- bottom: 0,
138
- left: 0,
139
- right: 0,
140
- top: 0
141
- )
142
- ```
143
-
144
109
  ### Loco::FixtureAdapter
145
110
 
146
111
  ```ruby
@@ -10,20 +10,20 @@ module Loco
10
10
 
11
11
  def initialize_relationships
12
12
  self.class.get_class_relationships.select{|relationship| relationship[:has_many] }.each do |relationship|
13
- has_many_class = relationship[:has_many].to_s.classify.constantize
14
- self.send("#{relationship[:has_many]}=", RecordArray.new(item_class: has_many_class, belongs_to: self))
13
+ has_many_class = relationship[:class_name] ? relationship[:class_name].to_s.classify.constantize : relationship[:has_many].to_s.classify.constantize
14
+ self.send("#{relationship[:has_many]}=", RecordArray.new(relationship: relationship, item_class: has_many_class, belongs_to: self))
15
15
  self.send("#{relationship[:has_many].to_s.singularize}_ids=", [])
16
16
  end
17
17
  end
18
18
 
19
19
  module ClassMethods
20
20
 
21
- def belongs_to(model)
21
+ def belongs_to(model, options={})
22
22
  attr_accessor model
23
23
  attr_accessor "#{model}_id"
24
24
 
25
25
  define_method "#{model}" do |&block|
26
- belongs_to_class = model.to_s.classify.constantize
26
+ belongs_to_class = options[:class_name] ? options[:class_name].to_s.classify.constantize : model.to_s.classify.constantize
27
27
  record = instance_variable_get("@#{model}")
28
28
  if record
29
29
  block.call(record) if block.is_a? Proc
@@ -43,24 +43,26 @@ module Loco
43
43
  end
44
44
 
45
45
  define_method "#{model}=" do |record|
46
- belongs_to_class = model.to_s.classify.constantize
46
+ belongs_to_class = options[:class_name] ? options[:class_name].to_s.classify.constantize : model.to_s.classify.constantize
47
47
  raise TypeError, "Expecting a #{belongs_to_class} as defined by #belongs_to :#{model}" unless record.is_a? belongs_to_class
48
48
  instance_variable_set("@#{model}", record)
49
49
  self.send("#{model}_id=", (record.nil? ? nil : record.id))
50
50
  record
51
51
  end
52
52
 
53
+ alias_method model.to_s.camelize(:lower), model
54
+ alias_method "#{model.to_s.camelize(:lower)}=", "#{model}="
55
+
53
56
  relationships = get_class_relationships
54
- relationships << { belongs_to: model }
57
+ relationships << { belongs_to: model, class_name: options[:class_name] }
55
58
  end
56
59
 
57
- def has_many(model)
60
+ def has_many(model, options={})
58
61
  attr_accessor model
59
62
  attr_accessor "#{model.to_s.singularize}_ids"
60
63
 
61
64
  define_method "#{model}" do |&block|
62
- has_many_class = model.to_s.singularize.classify.constantize
63
-
65
+ has_many_class = options[:class_name] ? options[:class_name].to_s.classify.constantize : model.to_s.singularize.classify.constantize
64
66
  record_array = instance_variable_get("@#{model}")
65
67
  if record_array.is_loaded
66
68
  block.call(record_array) if block.is_a? Proc
@@ -76,7 +78,8 @@ module Loco
76
78
  record_array.load(array)
77
79
  else
78
80
  query = {}
79
- query["#{self.class.to_s.underscore.singularize}_id"] = self.id
81
+ foreign_key = options[:foreign_key] ? options[:foreign_key] : "#{self.class.to_s.underscore.singularize}_id"
82
+ query[foreign_key] = self.id
80
83
  array = has_many_class.find(query) do |records|
81
84
  record_array.load(records)
82
85
  instance_variable_set("@#{model}", record_array)
@@ -90,8 +93,7 @@ module Loco
90
93
  end
91
94
 
92
95
  define_method "#{model}=" do |records|
93
- has_many_class = model.to_s.singularize.classify.constantize
94
-
96
+ has_many_class = options[:class_name] ? options[:class_name].to_s.classify.constantize : model.to_s.singularize.classify.constantize
95
97
  if (records.is_a?(RecordArray) || records.is_a?(Array)) && (records.length == 0 || (records.length > 0 && records.first.class == has_many_class))
96
98
  unless records.is_a?(RecordArray)
97
99
  record_array = RecordArray.new(item_class: has_many_class, belongs_to: self)
@@ -107,8 +109,11 @@ module Loco
107
109
  records
108
110
  end
109
111
 
112
+ alias_method model.to_s.camelize(:lower), model
113
+ alias_method "#{model.to_s.camelize(:lower)}=", "#{model}="
114
+
110
115
  relationships = get_class_relationships
111
- relationships << { has_many: model }
116
+ relationships << { has_many: model, class_name: options[:class_name] }
112
117
  end
113
118
 
114
119
  end
@@ -13,7 +13,8 @@ module Loco
13
13
  def find(record, id, &block)
14
14
  type = record.class
15
15
  error = Pointer.new(:id)
16
- file = File.read(File.join(NSBundle.mainBundle.resourcePath, "fixtures", "#{type.to_s.underscore.pluralize}.json"))
16
+ filename = File.join(NSBundle.mainBundle.resourcePath, "fixtures", "#{type.to_s.underscore.gsub('nskvo_notifying_', '').pluralize}.json")
17
+ file = File.read(filename)
17
18
  data = NSJSONSerialization.JSONObjectWithData(file.to_data, options:JSON_OPTIONS, error:error).find{|obj| obj[:id] == id }
18
19
  if data
19
20
  load(type, record, data)
@@ -20,14 +20,6 @@ module Loco
20
20
  self
21
21
  end
22
22
 
23
- # Used to create observable table view cells
24
- def initWithStyle(style, reuseIdentifier:reuseIdentifier)
25
- super
26
- initialize_bindings
27
- set_properties({})
28
- self
29
- end
30
-
31
23
  # Change one or many properties from a hash of properties with values.
32
24
  # @param [Hash] properties_hash
33
25
  def set_properties(properties_hash)
@@ -36,6 +28,7 @@ module Loco
36
28
  self.send("#{key}=", value)
37
29
  end
38
30
  end
31
+ alias_method :setProperties, :set_properties
39
32
 
40
33
  # Change one or many properties from a hash of properties with values.
41
34
  # Only updates attributes defined with #property.
@@ -48,6 +41,7 @@ module Loco
48
41
  end
49
42
  end
50
43
  end
44
+ alias_method :updateAttributes, :update_attributes
51
45
 
52
46
  def method_missing(method, *args, &block)
53
47
  if method.end_with?('_binding=') || method.end_with?('Binding=')
@@ -143,6 +137,8 @@ module Loco
143
137
 
144
138
  unless @class_properties.include? name
145
139
  attr_accessor name
140
+ alias_method name.to_s.camelize(:lower), name
141
+ alias_method "#{name.to_s.camelize(:lower)}=", "#{name}="
146
142
  end
147
143
 
148
144
  if type.is_a? Proc
@@ -9,6 +9,7 @@ module Loco
9
9
  property :is_loaded, :string
10
10
  property :item_class
11
11
  property :length, :integer
12
+ property :relationship, :hash
12
13
 
13
14
  def <<(record)
14
15
  self.addObjectsFromArray([record])
@@ -56,7 +57,7 @@ module Loco
56
57
  def update_properties
57
58
  self.length = self.content.length
58
59
  if self.belongs_to
59
- self.belongs_to.send("#{self.item_class.to_s.underscore.singularize}_ids=", self.content.map(&:id))
60
+ self.belongs_to.send("#{self.relationship[:has_many].to_s.singularize}_ids=", self.content.map(&:id))
60
61
  end
61
62
  end
62
63
 
@@ -0,0 +1,47 @@
1
+ module Loco
2
+
3
+ module TextAlignable
4
+
5
+ def text_align
6
+ case self.textAlignment
7
+ when NSTextAlignmentLeft
8
+ 'left'
9
+ when NSTextAlignmentCenter
10
+ 'center'
11
+ when NSTextAlignmentRight
12
+ 'right'
13
+ when NSTextAlignmentJustified
14
+ 'justified'
15
+ when NSTextAlignmentNatural
16
+ 'natural'
17
+ end
18
+ end
19
+ alias_method :textAlign, :text_align
20
+
21
+ def text_align=(alignment)
22
+ case alignment
23
+ when 'left'
24
+ self.textAlignment = NSTextAlignmentLeft
25
+ when 'center'
26
+ self.textAlignment = NSTextAlignmentCenter
27
+ when 'right'
28
+ self.textAlignment = NSTextAlignmentRight
29
+ when 'justified'
30
+ self.textAlignment = NSTextAlignmentJustified
31
+ when 'natural'
32
+ self.textAlignment = NSTextAlignmentNatural
33
+ end
34
+ end
35
+ alias_method :textAlign=, :text_align=
36
+
37
+ def textAlignment=(alignment)
38
+ if alignment.is_a? String
39
+ self.text_align = alignment
40
+ else
41
+ super
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -1,3 +1,3 @@
1
1
  module Loco
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -19,46 +19,7 @@ module Loco
19
19
 
20
20
  class Label < UILabel
21
21
  include Resizable
22
-
23
- def text_align
24
- case self.textAlignment
25
- when NSTextAlignmentLeft
26
- 'left'
27
- when NSTextAlignmentCenter
28
- 'center'
29
- when NSTextAlignmentRight
30
- 'right'
31
- when NSTextAlignmentJustified
32
- 'justified'
33
- when NSTextAlignmentNatural
34
- 'natural'
35
- end
36
- end
37
- alias_method :textAlign, :text_align
38
-
39
- def text_align=(alignment)
40
- case alignment
41
- when 'left'
42
- self.textAlignment = NSTextAlignmentLeft
43
- when 'center'
44
- self.textAlignment = NSTextAlignmentCenter
45
- when 'right'
46
- self.textAlignment = NSTextAlignmentRight
47
- when 'justified'
48
- self.textAlignment = NSTextAlignmentJustified
49
- when 'natural'
50
- self.textAlignment = NSTextAlignmentNatural
51
- end
52
- end
53
- alias_method :textAlign=, :text_align=
54
-
55
- def textAlignment=(alignment)
56
- if alignment.is_a? String
57
- self.text_align = alignment
58
- else
59
- super
60
- end
61
- end
22
+ include TextAlignable
62
23
  end
63
24
 
64
25
  class PageControl < UIPageControl
@@ -80,6 +41,28 @@ module Loco
80
41
  class Slider < UISlider
81
42
  include Resizable
82
43
  end
44
+
45
+ class TableView < UITableView
46
+ include Resizable
47
+ end
48
+
49
+ class TableViewCell < UITableViewCell
50
+ include Observable
51
+
52
+ property :content
53
+
54
+ def initWithStyle(style, reuseIdentifier:reuseIdentifier)
55
+ initialize_bindings
56
+ set_properties({})
57
+ view_setup # Needed because it's not Loco::Resizable
58
+ self
59
+ end
60
+ end
61
+
62
+ class TextField < UITextField
63
+ include Resizable
64
+ include TextAlignable
65
+ end
83
66
 
84
67
  class TextView < UITextView
85
68
  include Resizable
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-loco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Pattison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-19 00:00:00.000000000 Z
11
+ date: 2013-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print_motion
@@ -104,7 +104,7 @@ files:
104
104
  - lib/motion-loco/resizable.rb
105
105
  - lib/motion-loco/rest_adapter.rb
106
106
  - lib/motion-loco/sqlite_adapter.rb
107
- - lib/motion-loco/table_view.rb
107
+ - lib/motion-loco/text_alignable.rb
108
108
  - lib/motion-loco/version.rb
109
109
  - lib/motion-loco/view_controller.rb
110
110
  - lib/motion-loco/views.rb
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  requirements: []
131
131
  rubyforge_project:
132
- rubygems_version: 2.1.4
132
+ rubygems_version: 2.1.5
133
133
  signing_key:
134
134
  specification_version: 4
135
135
  summary: Library for RubyMotion that includes Ember.js inspired bindings, computed
@@ -1,114 +0,0 @@
1
- motion_require 'observable'
2
- motion_require 'resizable'
3
-
4
- module Loco
5
-
6
- module UI
7
-
8
- class TableViewCell < UITableViewCell
9
- include Observable
10
-
11
- property :content
12
-
13
- def initWithStyle(style, reuseIdentifier:reuseIdentifier)
14
- if super
15
- view_setup # Needed because it's not Loco::Resizable
16
- end
17
- self
18
- end
19
-
20
- def select
21
- Loco.debug("Override #{self.class}#select with the action to take when the row is selected")
22
- end
23
-
24
- def view_controller(superview=nil)
25
- if superview
26
- view_controller = superview.nextResponder
27
- else
28
- view_controller = self.superview.nextResponder
29
- end
30
- if view_controller.is_a? UIViewController
31
- view_controller
32
- elsif view_controller.is_a? UIView
33
- self.view_controller(view_controller)
34
- end
35
- end
36
- alias_method :viewController, :view_controller
37
-
38
- def view_setup
39
- viewSetup
40
- end
41
-
42
- def viewSetup
43
- Loco.debug("Override #{self.class}#viewSetup or #{self.class}#view_setup to customize the view")
44
- end
45
- end
46
-
47
- class TableView < UITableView
48
- include Resizable
49
- include Observable
50
-
51
- property :content
52
-
53
- def content=(value)
54
- super
55
- self.reloadData
56
- end
57
-
58
- def cell_id
59
- @cell_id ||= "CELL_#{self.object_id}"
60
- end
61
-
62
- def initWithFrame(frame)
63
- super(frame)
64
- self.dataSource = self.delegate = self
65
- end
66
-
67
- def item_view_class
68
- self.class.get_item_view_class
69
- end
70
-
71
- # UITableViewDelegate implementation for returning the cell at a given indexPath.
72
- # @return [Loco::TableViewCell]
73
- def tableView(tableView, cellForRowAtIndexPath:indexPath)
74
- cell = tableView.dequeueReusableCellWithIdentifier(cell_id)
75
- unless cell
76
- cell = self.item_view_class.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:cell_id)
77
- end
78
- cell.content = self.content[indexPath.row]
79
- cell
80
- end
81
-
82
- # UITableViewDelegate implementation for selecting a cell at a given indexPath.
83
- def tableView(tableView, didSelectRowAtIndexPath:indexPath)
84
- cell = tableView.cellForRowAtIndexPath(indexPath)
85
- cell.select
86
- end
87
-
88
- # UITableViewDelegate implementation for returning the number of rows in a section.
89
- # @return [Integer]
90
- def tableView(tableView, numberOfRowsInSection:section)
91
- if self.content.is_a? Array
92
- self.content.length
93
- else
94
- 0
95
- end
96
- end
97
-
98
- class << self
99
-
100
- def item_view_class(view_class)
101
- @item_view_class = view_class
102
- end
103
- alias_method :itemViewClass, :item_view_class
104
-
105
- def get_item_view_class
106
- @item_view_class ||= TableViewCell
107
- end
108
-
109
- end
110
- end
111
-
112
- end
113
-
114
- end