Last active
December 24, 2015 06:29
-
-
Save jdewind/6757667 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
form1 = Formotion::Form.new({ | |
sections: [{ | |
title: "Section 1", | |
key: :section_one, | |
select_one: true, | |
rows: [{ | |
title: "First", | |
key: :first, | |
type: :check | |
}, | |
{ | |
title: "Second", | |
key: :second, | |
type: :check, | |
value: true | |
} | |
] | |
} | |
]}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def initialize(form, primary_key) | |
... | |
@primary_value = @data_signal.map! do |data| | |
lookup_title(@form, data, primary_key) | |
end.replayLast | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RACForm | |
attr_reader :form, | |
:data_signal | |
def initialize(form, primary_key, *secondary_keys) | |
@form = form | |
@data_signal = RACReplaySubject.replaySubjectWithCapacity RACReplaySubjectUnlimitedCapacity | |
self.data_signal.sendNext @form.render | |
@form.on_submit do |f| | |
self.data_signal.sendNext @form.render | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RACForm | |
attr_reader :form, | |
:data_signal, | |
:primary_value | |
def initialize(form, primary_key, *secondary_keys) | |
@form = form | |
@data_signal = RACReplaySubject.replaySubjectWithCapacity RACReplaySubjectUnlimitedCapacity | |
@primary_value = @data_signal.map! do |data| | |
lookup_title(@form, data, primary_key) | |
end.replayLast | |
self.data_signal.sendNext @form.render | |
@form.on_submit do |f| | |
self.data_signal.sendNext @form.render | |
end | |
end | |
private | |
def lookup_title(form, data, key) | |
row = form.row(key) || form.row(data[key]) | |
if row | |
case row.type | |
when :subform | |
subform = row.subform.to_form | |
return lookup_title(subform, subform.render, key) | |
when :check | |
return row.title | |
else | |
return row.value | |
end | |
end | |
data[key] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RACForm | |
attr_reader :form, | |
:data_signal, | |
:data, | |
:primary_value | |
def initialize(form, primary_key, *secondary_keys) | |
@form = form | |
@data_signal = RACReplaySubject.replaySubjectWithCapacity RACReplaySubjectUnlimitedCapacity | |
@primary_value = @data_signal.map! do |data| | |
lookup_title(@form, data, primary_key) | |
end.replayLast | |
@secondary_values = @data_signal.map! do |data| | |
secondary_keys.map { |k| lookup_title(@form, data, k) } | |
end.replayLast | |
self.data_signal.sendNext @form.render | |
@form.on_submit do |f| | |
self.data_signal.sendNext @form.render | |
end | |
end | |
private | |
attr_writer :data | |
def update | |
self.data = form.render | |
end | |
def lookup_title(form, data, key) | |
row = form.row(key) || form.row(data[key]) | |
if row | |
case row.type | |
when :subform | |
subform = row.subform.to_form | |
return lookup_title(subform, subform.render, key) | |
when :check | |
return row.title | |
else | |
return row.value | |
end | |
end | |
data[key] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def initialize(form, primary_key, *secondary_keys) | |
... | |
@secondary_values = @data_signal.map! do |data| | |
secondary_keys.map { |k| lookup_title(@form, data, k) } | |
end.replayLast | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UITableViewCell | |
def initWithForm(form) | |
self.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier: "IDENTIFIER") do | |
rac.textLabel.text = form.primary_value | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UITableViewCell | |
def initWithForm(form) | |
self.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier: "IDENTIFIER") do | |
rac.textLabel.text = form.primary_value | |
rac.detailTextLabel.text = form.secondary_values.map! do |x| | |
x.join(", ") | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FormViewController | |
def initWithForms(forms) | |
self.initWithNibName(nil, bundle: nil).tap do | |
@forms = forms | |
@composite_signal = RACSignal.combineLatest(@forms.map { |f| f.data_signal }).map! do |tuple| | |
combined = {} | |
tuple.allObjects.each do |x| | |
combined.merge! flatten_values(x) | |
end | |
combined | |
end | |
end | |
end | |
private | |
def flatten_values(x) | |
hash = {} | |
x.each do |k,v| | |
if v.is_a? Hash | |
hash.merge! flatten_values(v) | |
else | |
hash.merge!({k => v}) | |
end | |
end | |
hash | |
end | |
end |
Ooops. I intended to self.initWithNibName.tap. Fixed! Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 4 isn’t correct Ruby syntax. In Ruby you call either
super
to call the super implementation of the current method or just the method to call a different one. I.e. in Ruby it is not possible to call the super implementation of a method other than the current method in the call stack.(I’m not sure what the
initWithNibName
method in this case is –as you also pass it a block, which the normal objc implementation ofinitWithNibName
knows nothing about– so unfortunately I cannot offer a correct version.)