Class: Dress

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

Defined Under Namespace

Classes: ActiveView, Line, Maker

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Dress

Returns a new instance of Dress.



73
74
75
76
77
78
79
# File 'lib/dress.rb', line 73

def initialize(node)
  self.class.transforms.each do |(method,matcher,block)|
    # method == :at | :search
    @me = node.send(method,matcher)
    self.instance_eval(&block) # this is so we can define helper methods on the dress
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



81
82
83
84
85
86
87
# File 'lib/dress.rb', line 81

def method_missing(method,*args,&block)
  if block
    @me.send(method,*args,&block)
  else
    @me.send(method,*args)
  end
end

Class Attribute Details

.transformsObject (readonly)

Returns the value of attribute transforms.



34
35
36
# File 'lib/dress.rb', line 34

def transforms
  @transforms
end

Instance Attribute Details

#meObject (readonly)

Returns the value of attribute me.



72
73
74
# File 'lib/dress.rb', line 72

def me
  @me
end

Class Method Details

.at(matcher, &block) ⇒ Object



40
41
42
43
# File 'lib/dress.rb', line 40

def at(matcher,&block)
  @transforms ||= []
  @transforms << [:at,matcher,block]
end

.match(matcher, &block) ⇒ Object



35
36
37
38
# File 'lib/dress.rb', line 35

def match(matcher,&block)
  @transforms ||= []
  @transforms << [:search,matcher,block]
end

.on(node) ⇒ Object

destructive transform of node



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dress.rb', line 46

def on(node)
  node =
    case node
    when Nokogiri::XML::Node
      node
    when String,IO
      Nokogiri::XML(node)
    else
      raise "bad xml document: #{node}"
    end
  dresser = self.new(node)
  node
end

.style(&block) ⇒ Object



60
61
62
63
64
# File 'lib/dress.rb', line 60

def style(&block)
  c = Class.new(Dress)
  c.class_eval(&block)
  c
end

.|(dress2) ⇒ Object



66
67
68
69
# File 'lib/dress.rb', line 66

def |(dress2)
  raise "expects a Dress" unless dress2.ancestors.include?(Dress)
  Line.new([self,dress2])
end