Class: PlasticCup::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/plastic_cup/base.rb

Constant Summary collapse

OSVersions =
%w(all ios4 ios5 ios6 ios7)

Class Method Summary collapse

Class Method Details

.add_style_sheet(name, properties, os_version = :all) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/plastic_cup/base.rb', line 25

def self.add_style_sheet(name, properties, os_version=:all)
  if OSVersions.include?(os_version.to_s)
    styles[to_key(name)] ||= {}
    styles[to_key(name)][os_version.to_sym] = Stylesheet.new(properties)
  else
    raise ArgumentError.new "OS version only accept #{OSVersions}"
  end
end

.apply_method(target, assign, setter, value) ⇒ Object

teacup/lib/teacup-ios/handler.rb



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/plastic_cup/base.rb', line 116

def self.apply_method(target, assign, setter, value)
  if target.respondsToSelector(setter)
    #NSLog "Calling target.#{setter}(#{value.inspect})"
    target.send(setter, value)
  elsif assign and target.respond_to?(assign)
    #NSLog "Setting #{assign} #{value.inspect}"
    target.send(assign, value)
  else
    NSLog "WARNING: Can't apply #{setter.inspect}#{assign and " or " + assign.inspect or ""} to #{target.inspect}"
  end
end

.apply_properties(target, properties) ⇒ Object

teacup/lib/teacup/handler.rb



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/plastic_cup/base.rb', line 76

def self.apply_properties(target, properties)
  klass = target.class
  properties.each do |key, proxy_value|
    value = if proxy_value.is_a?(Proc)
              proxy_value.call
            else
              proxy_value
            end
    handled = false
    klass.ancestors.each do |ancestor|
      ancestor_name=ancestor.name
      key = to_key(key)
      if handlers[ancestor_name].has_key? key
        handlers[ancestor_name][key].call(target, value)
        handled = true
        break
      end
    end
    unless handled
      # you can send methods to subviews (e.g. UIButton#titleLabel) and CALayers
      # (e.g. UIView#layer) by assigning a hash to a style name.
      if value.is_a?(Hash)
        apply_properties(target.send(key), value)
      else
        if key =~ /^set[A-Z]/
          assign = nil
          setter = key.to_s + ':'
        else
          assign = key.to_s + '='
          setter = 'set' + key.to_s.sub(/^./) {|c| c.capitalize} + ':'
        end

        apply_method(target, assign, setter, value)
      end
    end

  end
end

.clear_handlersObject



135
136
137
# File 'lib/plastic_cup/base.rb', line 135

def self.clear_handlers
  @handlers = nil
end

.clear_style_sheetsObject



131
132
133
# File 'lib/plastic_cup/base.rb', line 131

def self.clear_style_sheets
  @styles = nil
end

.get_style_sheet(style) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/plastic_cup/base.rb', line 34

def self.get_style_sheet(style)
  version_string = UIDevice.currentDevice.systemVersion.split('.').first
  if styles[to_key(style)].is_a?(Hash)
    style_hash = styles[to_key(style)]["ios#{version_string}".to_sym]
    style_hash ||= styles[to_key(style)][:all]
  end
  NSLog "WARNING: Style #{style} undefined." if style_hash.nil?
  style_hash
end

.handler(klass, *style_names, &block) ⇒ Object

teacup/lib/teacup/handler.rb



45
46
47
48
49
50
51
52
53
54
# File 'lib/plastic_cup/base.rb', line 45

def self.handler(klass, *style_names, &block)
  if style_names.length == 0
    raise TypeError.new "No style names assigned"
  else
    style_names.each do |style_name|
      handlers[klass.name][to_key(style_name)] = block
    end
  end
  self
end

.handlersObject

teacup/lib/teacup/handler.rb



69
70
71
# File 'lib/plastic_cup/base.rb', line 69

def self.handlers
  @handlers ||= Hash.new{ |hash,klass| hash[klass] = {} }
end

.style(target, style) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/plastic_cup/base.rb', line 7

def self.style(target, style)
  if style.is_a?(Hash)
    apply_properties(target, style)
  else
    style_sheet = get_style_sheet(style)
    unless style_sheet.nil?
      extends = style_sheet.extends
      final_style = {}
      extends.each do |ext|
        ext_style_sheet = get_style_sheet(ext)
        final_style.merge!(ext_style_sheet.properties) unless ext_style_sheet.nil?
      end
      apply_properties(target, final_style.merge(style_sheet.properties))
    end
  end
  target
end

.stylesObject



64
65
66
# File 'lib/plastic_cup/base.rb', line 64

def self.styles
  @styles||={}
end

.to_key(key) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/plastic_cup/base.rb', line 56

def self.to_key(key)
  begin
    key = key.to_sym
  rescue NoMethodError
    raise TypeError.new "#{key.inspect} is not a symbol"
  end
end