Class: Teacup::Style

Inherits:
Hash
  • Object
show all
Defined in:
lib/teacup/style.rb

Overview

The Style class is where the precedence rules are applied. A Style can query the Stylesheet that created it to look up other styles (for ‘extends:`) and to import other Stylesheets. If it is handed a target (e.g. a `UIView` instance) and orientation, it will merge those in appropriately as well.

Constant Summary collapse

Orientations =
[:portrait, :upside_up, :upside_down, :landscape, :landscape_left, :landscape_right]
Overrides =
{
  0 => [:portrait, :upside_up],
  UIInterfaceOrientationPortrait => [:portrait, :upside_up],
  UIInterfaceOrientationPortraitUpsideDown => [:portrait, :upside_down],
  UIInterfaceOrientationLandscapeLeft => [:landscape, :landscape_left],
  UIInterfaceOrientationLandscapeRight => [:landscape, :landscape_right],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stylenameObject

Returns the value of attribute stylename.



8
9
10
# File 'lib/teacup/style.rb', line 8

def stylename
  @stylename
end

#stylesheetObject

Returns the value of attribute stylesheet.



9
10
11
# File 'lib/teacup/style.rb', line 9

def stylesheet
  @stylesheet
end

Instance Method Details

#build(target = nil, orientation = nil, seen = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/teacup/style.rb', line 31

def build(target=nil, orientation=nil, seen={})
  properties = Style.new.update(self)
  properties.stylename = self.stylename
  properties.stylesheet = self.stylesheet

  # at this point, we really DO need the orientation
  unless orientation
    orientation = UIApplication.sharedApplication.statusBarOrientation
  end

  # first, move orientation settings into properties "base" level.
  if orientation
    Overrides[orientation].each do |orientation_key|
      if override = properties.delete(orientation_key)
        # override is first, so it takes precedence
        if override.is_a? Hash
          Teacup::merge_defaults override, properties, properties
        end
        properties.supports[orientation_key] = !!override
      end
    end
  end

  # delete all of them from `properties`
  Orientations.each do |orientation_key|
    if properties.delete(orientation_key)
      properties.supports[orientation_key] = true
    end
  end

  # now we can merge extends, and importing.  before merging, these will go
  # through the same process that we just finished on the local style
  if stylesheet && stylesheet.is_a?(Teacup::Stylesheet)
    stylesheet.imported_stylesheets.reverse.each do |stylesheet|
      imported_properties = stylesheet.query(self.stylename, target, orientation, seen)
      Teacup::merge_defaults! properties, imported_properties
    end

    if also_includes = properties.delete(:extends)
      also_includes = [also_includes] unless also_includes.is_a? Array

      # turn style names into Hashes by querying them on the stylesheet
      # (this does not pass `seen`, because this is a new query)
      also_includes.each do |also_include|
        extended_properties = stylesheet.query(also_include, target, orientation)
        Teacup::merge_defaults! properties, extended_properties
      end
    end

    # if we know the class of the target, we can apply styles via class
    # inheritance.  We do not pass `target` in this case.
    if target
      target.class.ancestors.each do |ancestor|
        extended_properties = stylesheet.query(ancestor, nil, orientation)
        Teacup::merge_defaults!(properties, extended_properties)
      end
    end
  end

  properties
end

#supportsObject

A hash of orientation => true/false. true means the orientation is supported.



22
23
24
# File 'lib/teacup/style.rb', line 22

def supports
  @supports ||= {}
end

#supports?(orientation_key) ⇒ Boolean

returns the value - ‘nil` has special meaning when querying :portrait or :upside_up

Returns:

  • (Boolean)


27
28
29
# File 'lib/teacup/style.rb', line 27

def supports? orientation_key
  supports[orientation_key]
end