Class: Paggio::CSS::Definition

Inherits:
BasicObject
Defined in:
lib/paggio/css/definition.rb

Defined Under Namespace

Classes: Gradient, Style

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Definition

Returns a new instance of Definition.



16
17
18
19
20
21
22
23
24
# File 'lib/paggio/css/definition.rb', line 16

def initialize(&block)
  @style = []

  if block.arity == 0
    instance_exec(&block)
  else
    block.call(self)
  end if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/paggio/css/definition.rb', line 221

def method_missing(name, *args, &block)
  name = name.to_s

  if name.end_with? ?!
    name = name[0 .. -2]

    @important = true
    __send__ name, *args, &block
    @important = false

    return
  end

  if args.length == 1
    argument = args.first

    if ::Hash === argument
      argument.each {|sub, value|
        style "#{name}-#{sub}", value
      }
    else
      style name, argument
    end
  else
    style name, args.join(' ')
  end

  @important = false

  self
end

Instance Method Details

#animation(*args) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/paggio/css/definition.rb', line 176

def animation(*args)
  if Hash === args.first
    if args.length == 1
      options = args.first
    end

    options.each {|name, value|
      style "-webkit-animation-#{name}", value
      style "animation-#{name}", value
    }
  else
    style 'animation', args
    style '-webkit-animation', args
  end
end

#background(*args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/paggio/css/definition.rb', line 72

def background(*args)
  if Gradient === args.first
    if args.length > 1
      raise NotImplementedError, "multiple gradients not implemented yet"
    end

    args.first.each {|s|
      style s.name || 'background-image', s.value
    }
  else
    if ::Hash === args.first
      args.first.each {|sub, value|
        style "background-#{sub}", value
      }
    else
      style :background, args
    end
  end
end

#border(*args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/paggio/css/definition.rb', line 92

def border(*args)
  if ::Hash === args.first
    if args.length == 1
      options = args.first
    end

    options.each {|name, value|
      case name
      when :top, :bottom, :left, :right
        if ::Hash === value
          value.each {|n, v|
            style "border-#{name}-#{n}", v
          }
        else
          style "border-#{name}", value
        end

      when :radius
        if ::Hash === value
          value.each {|horizontal, value|
            value.each {|vertical, value|
              style "-moz-border-radius-#{horizontal}#{vertical}", value
              style "-webkit-border-#{horizontal}-#{vertical}-radius", value
              style "border-#{horizontal}-#{vertical}-radius", value
            }
          }
        else
          style '-moz-border-radius', value
          style '-webkit-border-radius', value
          style 'border-radius', value
        end

      when :color
        if ::Hash === value
          value.each {|name, value|
            style "border-#{name}-color", value
          }
        else
          style 'border-color', value
        end

      else
        style "border-#{name}", value
      end
    }
  else
    style :border, args
  end
end

#box(options) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/paggio/css/definition.rb', line 142

def box(options)
  if ::Hash === options
    options.each {|name, value|
      case name
      when :shadow
        if ::Array === value
          if ::Array === value[0]
            value = value.map { |v| v.join ' ' }.join(', ')
          else
            value = value.join ' '
          end
        end

        style '-moz-box-shadow', value
        style '-webkit-box-shadow', value
        style 'box-shadow', value

      else
        style "box-#{name}", value
      end
    }
  else
    style :box, options
  end
end

#each(&block) ⇒ Object



30
31
32
# File 'lib/paggio/css/definition.rb', line 30

def each(&block)
  @style.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/paggio/css/definition.rb', line 26

def empty?
  @style.empty?
end

#filter(*args) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/paggio/css/definition.rb', line 213

def filter(*args)
  style 'filter', args
  style '-webkit-filter', args
  style '-moz-filter', args
  style '-ms-filter', args
  style '-o-filter', args
end

#gradient(*args) ⇒ Object



34
35
36
# File 'lib/paggio/css/definition.rb', line 34

def gradient(*args)
  Gradient.new(*args)
end

#opacity(value) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/paggio/css/definition.rb', line 168

def opacity(value)
  style 'opacity', value
  style '-moz-opacity', value

  style '-ms-filter', %Q{"progid:DXImageTransform.Microsoft.Alpha(Opacity=#{(value * 100).to_i})"}
  style 'filter', "alpha(opacity=#{(value * 100).to_i})"
end

#rgb(r, g, b) ⇒ Object



48
49
50
# File 'lib/paggio/css/definition.rb', line 48

def rgb(r, g, b)
  "rgb(#{r}, #{g}, #{b}, #{a})"
end

#rgba(r, g, b, a) ⇒ Object



52
53
54
# File 'lib/paggio/css/definition.rb', line 52

def rgba(r, g, b, a)
  "rgba(#{r}, #{g}, #{b}, #{a})"
end

#style(name, value = nil, important = @important) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/paggio/css/definition.rb', line 253

def style(name, value = nil, important = @important)
  if ::Array === value
    value = value.join ' '
  end

  if Style === name
    @style << name
  else
    @style << Style.new(name, value, important)
  end
end

#style!(name, value = nil) ⇒ Object



265
266
267
# File 'lib/paggio/css/definition.rb', line 265

def style!(name, value = nil)
  style(name, value, true)
end

#transform(*args) ⇒ Object



205
206
207
208
209
210
211
# File 'lib/paggio/css/definition.rb', line 205

def transform(*args)
  style 'transform', args
  style '-webkit-transform', args
  style '-moz-transform', args
  style '-ms-transform', args
  style '-o-transform', args
end

#transition(*args) ⇒ Object



192
193
194
195
196
# File 'lib/paggio/css/definition.rb', line 192

def transition(*args)
  style 'transition', args
  style '-webkit-transition', args
  style '-moz-transition', args
end

#url(value) ⇒ Object



38
39
40
# File 'lib/paggio/css/definition.rb', line 38

def url(value)
  "url(#{value.to_s.inspect})"
end

#user_select(*args) ⇒ Object



198
199
200
201
202
203
# File 'lib/paggio/css/definition.rb', line 198

def user_select(*args)
  style 'user-select', args
  style '-webkit-user-select', args
  style '-moz-user-select', args
  style '-ms-user-select', args
end