Class: MotionPixateLayout::ViewProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-pixate-layout/proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ ViewProxy

Returns a new instance of ViewProxy.



55
56
57
58
# File 'lib/motion-pixate-layout/proxy.rb', line 55

def initialize(view)
  @view = view
  self.blocks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/motion-pixate-layout/proxy.rb', line 91

def method_missing(method_name, *args, &block)
  if Kernel.constants.include?(method_name.to_sym)
    view_class = Kernel.const_get(method_name.to_s)
    raise "#{view_class.name} is not a known UIView subclass" unless view_class <= UIView
    self.class.add_subview_method(view_class)

    send method_name, *args, &block
  else
    raise "#{method_name} is not defined. Should be a subclass of UIView."
  end
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



52
53
54
# File 'lib/motion-pixate-layout/proxy.rb', line 52

def blocks
  @blocks
end

#viewObject (readonly)

Returns the value of attribute view.



53
54
55
# File 'lib/motion-pixate-layout/proxy.rb', line 53

def view
  @view
end

Class Method Details

.add_subview_method(klass) ⇒ Object



83
84
85
86
87
88
# File 'lib/motion-pixate-layout/proxy.rb', line 83

def self.add_subview_method(klass)
  define_method klass.name do |*args, &block|
    selector, options, = *args
    subview klass, selector, options, &block
  end
end

Instance Method Details

#run_blocksObject



103
104
105
# File 'lib/motion-pixate-layout/proxy.rb', line 103

def run_blocks
  blocks.each { |block| self.instance_eval &block }
end

#subview(subview_class, selector, attributes = {}, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/motion-pixate-layout/proxy.rb', line 60

def subview(subview_class, selector, attributes = {}, &block)
  selector = MotionPixateLayout::Selector.new(selector)

  subview_class.new.tap do |subview|
    subview.styleId = selector.style_id
    subview.styleClass = selector.style_classes.join(" ")

    unless attributes.nil?
      attributes.each do |key, value|
        subview.send "#{key}=", value
      end
    end

    view.addSubview subview
    if block_given?
      ViewProxy.new(subview).tap do |proxy|
        proxy.blocks << block
        proxy.run_blocks
      end
    end
  end
end