Module: Maml

Defined in:
lib/motion-maml/maml.rb,
lib/motion-maml/version.rb

Defined Under Namespace

Classes: Tag

Constant Summary collapse

DEFAULT_TAG =
'view'.freeze
TAGS =
{
  # 'annotation-view' => ::MKAnnotationView,
  # 'map-view'        => ::MKMapView,
  # 'volume-view'     => ::MPVolumeView,
  'action-sheet'    => ::UIActionSheet,
  'activity-indicator-view' => ::UIActivityIndicatorView,
  'bar-button-item' => ::UIBarButtonItem,
  'button'          => ::UIButton,
  'collection-view' => ::UICollectionView,
  'collection-view-cell' => ::UICollectionViewCell,
  'date-picker'     => ::UIDatePicker,
  'image-view'      => ::UIImageView,
  'label'           => ::UILabel,
  'navigation-bar'  => ::UINavigationBar,
  'page-control'    => ::UIPageControl,
  'picker-view'     => ::UIPickerView,
  'refresh-control' => ::UIRefreshControl,
  'scroll-view'     => ::UIScrollView,
  'search-bar'      => ::UISearchBar,
  'segmented-control' => ::UISegmentedControl,
  'slider'          => ::UISlider,
  'stepper'         => ::UIStepper,
  'switch'          => ::UISwitch,
  'tab-bar'         => ::UITabBar,
  'tab-bar-item'    => ::UITabBarItem,
  'progress-view'   => ::UIProgressView,
  'table-view'      => ::UITableView,
  'table-view-headerfooter-view' => ::UITableViewHeaderFooterView,
  'text-field'      => ::UITextField,
  'text-view'       => ::UITextView,
  'toolbar'         => ::UIToolbar,
  'view'            => ::UIView,
  'web-view'        => ::UIWebView,
  'table-view-cell' => ::UITableViewCell
}.freeze
VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.[](selector) ⇒ Object



94
95
96
# File 'lib/motion-maml/maml.rb', line 94

def [](selector)
  init(selector, nil)
end

.alloc(selector) ⇒ Object



44
45
46
# File 'lib/motion-maml/maml.rb', line 44

def alloc(selector)
  build(selector)
end

.build(selector) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/motion-maml/maml.rb', line 56

def build(selector)
  tag = parse(selector)

  klass = klass_for(tag.element)
  raise "#{tag.element.inspect} not a valid tag name" unless klass

  element = klass.alloc
  element.styleId    = tag.id         if tag.id
  element.styleClass = tag.class_name if tag.class_name
  element
end

.init(selector, options = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/motion-maml/maml.rb', line 48

def init(selector, options = nil)
  element = alloc(selector).init
  options && options.each do |key, value|
    element.send("#{key}=", value)
  end
  element
end

.klass_for(name) ⇒ Object



68
69
70
# File 'lib/motion-maml/maml.rb', line 68

def klass_for(name)
  TAGS[name]
end

.parse(selector) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/motion-maml/maml.rb', line 72

def parse(selector)
  selector = selector.to_s.strip

  rgx = /^([\w\-]+)?(#[\w\-]+)?(\.[\w\.\-]+)?/

  captures   = selector.match(rgx)

  name = captures[1] || 'view'
  name.gsub!('_', '-')

  id = captures[2]
  id.gsub!('#', '') if id

  class_name = captures[3]
  if class_name
    class_name.gsub!('.', ' ')
    class_name.strip!
  end

  Tag.new(name,  id,  class_name)
end