Skip to content

Commit

Permalink
Merge pull request #227 from jamonholmgren/append-block
Browse files Browse the repository at this point in the history
Added convenience callback block after append, build, etc, and find_or_append() (with tests)
  • Loading branch information
twerth committed Mar 28, 2015
2 parents 917e517 + aaaf018 commit b682fa7
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 17 deletions.
6 changes: 3 additions & 3 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def init_buttons

def init_popup_section

rmq.append(UIView, :popup_section).tap do |q|
rmq.append(UIView, :popup_section) do |q|

@title_label = q.append!(UILabel, :title_label)
q.append(UIButton, :open_popup).on(:touch_down) do |sender|
Expand All @@ -116,7 +116,7 @@ def init_popup_section

end.on(:touch_up) do |sender|

rmq.append(UILabel, :popup_wrapper).tap do |o|
rmq.append(UILabel, :popup_wrapper) do |o|
o.animations.fade_in
o.append(UILabel, :popup_text_label)
end
Expand All @@ -132,7 +132,7 @@ def init_popup_section
def init_validation_section

# let's lay this out using the grid!
rmq.append(UIView, :validation_section).tap do |q|
rmq.append(UIView, :validation_section) do |q|
q.append(UILabel, :validation_title)
@digits_only = q.append(UITextField, :only_digits).validates(:digits).on(:change) do |sender|
rmq(sender).valid?
Expand Down
2 changes: 1 addition & 1 deletion motion/ruby_motion_query/stylesheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def apply_style_to_view(view, style_name)
view.rmq_style_applied
rescue NoMethodError => e
if e.message =~ /.*#{style_name.to_s}.*/
puts "\n[RMQ ERROR] style_name :#{style_name} doesn't exist for a #{view.class.name}. Add 'def #{style_name}(st)' to #{stylesheet.class.name} class\n\n"
$stderr.puts "\n[RMQ ERROR] style_name :#{style_name} doesn't exist for a #{view.class.name}. Add 'def #{style_name}(st)' to #{stylesheet.class.name} class\n\n"
else
raise e
end
Expand Down
56 changes: 45 additions & 11 deletions motion/ruby_motion_query/subviews.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def remove
# @return [RMQ]
def add_subview(view_or_constant, opts={})
subviews_added = []
style = opts[:style]

selected.each do |selected_view|
created = false
Expand Down Expand Up @@ -63,11 +62,14 @@ def add_subview(view_or_constant, opts={})
new_view.rmq_appended if appended

if self.stylesheet
apply_style_to_view(new_view, style) if style
apply_style_to_view(new_view, opts[:style]) if opts[:style]
end
end

RMQ.create_with_array_and_selectors(subviews_added, selectors, @context, self)
view = RMQ.create_with_array_and_selectors(subviews_added, selectors, @context, self)
opts[:block].call view if opts[:block]
opts[:raw_block].call view.get if opts[:raw_block]
view
end
alias :insert :add_subview

Expand All @@ -93,8 +95,9 @@ def add_subview(view_or_constant, opts={})
# rmq.append(UIImageView)
#
# @return [RMQ]
def append(view_or_constant, style=nil, opts = {})
def append(view_or_constant, style=nil, opts = {}, &block)
opts[:style] = style
opts[:block] = block if block
add_subview(view_or_constant, opts)
end

Expand All @@ -103,16 +106,42 @@ def append(view_or_constant, style=nil, opts = {})
# @example
# @my_button = rmq.append! UIButton
# @my_label = rmq.append!(UILabel, :my_label)
def append!(view_or_constant, style=nil, opts = {})
def append!(view_or_constant, style=nil, opts = {}, &block)
opts[:raw_block] = block if block
append(view_or_constant, style, opts).get
end

# Same as append, but will look for a view with the same name and reapply styles
# to it if it finds one. If it doesn't, it'll append as normal.
#
# @example
# @my_button = rmq.find_or_append(UIButton, :my_button)
# @my_button = rmq.find_or_append(UIButton, :my_button) # Only one created
def find_or_append(view_or_constant, style=nil, opts = {}, &block)
if style && (q = self.find(style)) && q.length > 0
view_or_constant = q.get
end

append(view_or_constant, style, opts, &block)
end

# Same as append!, but will look for a view with the same name and reapply styles
# to it if it finds one. If it doesn't, it'll append! as normal.
#
# @example
# @my_button = rmq.find_or_append!(UIButton, :my_button)
# @my_button = rmq.find_or_append!(UIButton, :my_button) # Only one created
def find_or_append!(view_or_constant, style=nil, opts = {}, &block)
find_or_append(view_or_constant, style, opts, &block).get
end

# Just like append, but inserts the view at index 0 of the subview array
#
# @return [RMQ]
def unshift(view_or_constant, style=nil, opts = {})
def unshift(view_or_constant, style=nil, opts = {}, &block)
opts[:at_index] = 0
opts[:style] = style
opts[:block] = block if block
add_subview view_or_constant, opts
end
alias :prepend :unshift
Expand All @@ -122,7 +151,8 @@ def unshift(view_or_constant, style=nil, opts = {})
# @example
# @my_button = rmq.prepend! UIButton
# @my_label = rmq.prepend!(UILabel, :my_label)
def unshift!(view_or_constant, style=nil, opts = {})
def unshift!(view_or_constant, style=nil, opts = {}, &block)
opts[:raw_block] = block if block
unshift(view_or_constant, style, opts).get
end
alias :prepend! :unshift!
Expand All @@ -147,18 +177,20 @@ def unshift!(view_or_constant, style=nil, opts = {})
# end
# end
#
def create(view_or_constant, style = nil, opts = {})
def create(view_or_constant, style = nil, opts = {}, &block)
# TODO, refactor so that add_subview uses create, not backwards like it is now
opts[:do_not_add] = true
opts[:style] = style
opts[:block] = block if block
add_subview view_or_constant, opts
end

# Same as create, but instantly returns the view, without having to use .get
#
# @example
# @my_button = rmq.create! UIButton
def create!(view_or_constant, style=nil, opts = {})
def create!(view_or_constant, style=nil, opts = {}, &block)
opts[:raw_block] = block if block
create(view_or_constant, style, opts).get
end

Expand All @@ -174,17 +206,19 @@ def create!(view_or_constant, style=nil, opts = {})
# def rmq_build
# rmq.append(UIView, :foo)
# end
def build(view, style = nil, opts = {})
def build(view, style = nil, opts = {}, &block)
opts[:do_not_add] = true
opts[:style] = style
opts[:block] = block if block
add_subview view, opts
end

# Same as build, but instantly returns the view, without having to use .get
#
# @example
# @my_cell = rmq.build! cell
def build!(view, style = nil, opts = {})
def build!(view, style = nil, opts = {}, &block)
opts[:raw_block] = block if block
build(view, style, opts).get
end

Expand Down
100 changes: 99 additions & 1 deletion spec/subviews.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@vc.view.subviews.first.should == view
end

it 'should addend to the end of the subviews' do
it 'should append to the end of the subviews' do
view = @vc.rmq.append(UIView).get
@vc.view.subviews[@vc.view.subviews.length - 1].should == view
end
Expand Down Expand Up @@ -138,6 +138,25 @@
#TODO
end

it 'should insert then yield to a block with the rmq object' do
block_called = false
@vc.rmq.append(UILabel) do |view|
view.should.be.kind_of(RubyMotionQuery::RMQ)
view.get.should.be.kind_of(UILabel)
block_called = true
end
block_called.should == true
end

it 'should insert then yield to a block with the created view' do
block_called = false
@vc.rmq.append!(UILabel) do |view|
view.should.be.kind_of(UILabel)
block_called = true
end
block_called.should == true
end

describe 'create' do
it 'should allow you to create a view using rmq, without appending it to the view tree' do
test_view_wrapped = @vc.rmq.create(SubviewTestView)
Expand Down Expand Up @@ -203,6 +222,26 @@
table_sub.class.should == SubTableTest
table_sub.style.should == UITableViewStyleGrouped
end

it 'should create then yield to a block with the rmq object' do
block_called = false
@vc.rmq.create(UILabel) do |view|
view.should.be.kind_of(RubyMotionQuery::RMQ)
view.get.should.be.kind_of(UILabel)
block_called = true
end
block_called.should == true
end

it 'should create then yield to a block with the created view' do
block_called = false
@vc.rmq.create!(UILabel) do |view|
view.should.be.kind_of(UILabel)
block_called = true
end
block_called.should == true
end

end

describe 'build' do
Expand Down Expand Up @@ -273,6 +312,65 @@
@vc.rmq.append(view)
view.number_of_builds.should == 1
end

it 'should build an existing view then yield to a block with the rmq wrapped view' do
existing_view = UIView.new
block_called = false
@vc.rmq.build(existing_view) do |view|
view.should.be.kind_of(RubyMotionQuery::RMQ)
view.get.should == existing_view
block_called = true
end
block_called.should == true
end

it 'should build an existing view then yield to a block with the existing view' do
existing_view = UIView.new
block_called = false
@vc.rmq.build!(existing_view) do |view|
view.should == existing_view
block_called = true
end
block_called.should == true
end
end

describe 'find_or_append' do
it 'appends if none existing' do
@vc.view.subviews.length.should == 0
view = @vc.rmq.find_or_append(UIView).get
@vc.view.subviews.length.should == 1
@vc.view.subviews.first.should == view
end

it 'finds if existing' do
@vc.rmq.stylesheet = StyleSheetForSubviewsTests
@vc.view.subviews.length.should == 0
existing_view = @vc.rmq.append(UIView, :my_style).get
@vc.view.subviews.length.should == 1
found_view = @vc.rmq.find_or_append(UIView, :my_style).get
@vc.view.subviews.length.should == 1
found_view.should == existing_view
end
end

describe 'find_or_append!' do
it 'appends if none existing' do
@vc.view.subviews.length.should == 0
view = @vc.rmq.find_or_append!(UIView)
@vc.view.subviews.length.should == 1
@vc.view.subviews.first.should == view
end

it 'finds if existing' do
@vc.rmq.stylesheet = StyleSheetForSubviewsTests
@vc.view.subviews.length.should == 0
existing_view = @vc.rmq.append!(UIView, :my_style)
@vc.view.subviews.length.should == 1
found_view = @vc.rmq.find_or_append!(UIView, :my_style)
@vc.view.subviews.length.should == 1
found_view.should == existing_view
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/traversing.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe 'transversing' do
describe 'traversing' do
before do
@vc = UIViewController.alloc.init
@root_view = @vc.view
Expand Down

0 comments on commit b682fa7

Please sign in to comment.