PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/drillbit/Resources/tests/ruby/markaby/lib/markaby/cssproxy.rb

http://github.com/appcelerator/titanium_desktop
Ruby | 53 lines | 35 code | 5 blank | 13 comment | 3 complexity | 8ec71a60adbb9f28dbf3a4a91c5978c0 MD5 | raw file
Possible License(s): Apache-2.0
  1. module Markaby
  2. # Class used by Markaby::Builder to store element options. Methods called
  3. # against the CssProxy object are added as element classes or IDs.
  4. #
  5. # See the README for examples.
  6. class CssProxy
  7. # Creates a CssProxy object. The +opts+ and +block+ passed in are
  8. # stored until the element is created by Builder.tag!
  9. def initialize(opts = {}, &blk)
  10. @opts = opts
  11. @blk = blk
  12. end
  13. # Adds attributes to an element, for internal use only. For example, if you
  14. # want to write a wrapper which sets a bunch of default attributes for a certain
  15. # tag. Like the default `img' method included with Markaby automatically sets an
  16. # empty alt attribute.
  17. def merge!(opts)
  18. @opts.merge! opts
  19. self
  20. end
  21. # Adds attributes to an element. Bang methods set the :id attribute.
  22. # Other methods add to the :class attribute. If a block is supplied,
  23. # it is executed with a merged hash (@opts + args).
  24. def method_missing(id_or_class, *args, &blk)
  25. idc = id_or_class.to_s
  26. case idc
  27. when "pass"
  28. when /!$/
  29. @opts[:id] = $`
  30. else
  31. @opts[:class] = "#{@opts[:class]} #{idc}".strip
  32. end
  33. if args.empty? and blk.nil?
  34. self
  35. else
  36. if args.last.respond_to? :to_hash
  37. @opts.merge!(args.pop.to_hash)
  38. end
  39. args.push @opts
  40. @blk.call(args, blk)
  41. end
  42. end
  43. def to_str
  44. @blk.call([[@opts]]).to_s
  45. end
  46. alias_method :to_s, :to_str
  47. end
  48. end