Class: Soup::Snip

Inherits:
EmptyClass show all
Defined in:
lib/soup/snip.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, backend) ⇒ Snip

Returns a new instance of Snip.



7
8
9
10
# File 'lib/soup/snip.rb', line 7

def initialize(attributes, backend)
  @attributes = attributes
  @backend = backend
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/soup/snip.rb', line 45

def method_missing(method, *args)
  value = args.length > 1 ? args : args.first
  if method.to_s =~ /(.*)=\Z/
    @attributes[$1.to_sym] = value
  else
    @attributes[method]
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/soup/snip.rb', line 5

def attributes
  @attributes
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/soup/snip.rb', line 22

def ==(other)
  other.is_a?(Snip) && matching_attributes(other)
end

#destroyObject



17
18
19
20
# File 'lib/soup/snip.rb', line 17

def destroy
  @backend.destroy(self.name)
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/soup/snip.rb', line 26

def eql?(other)
  self == other
end

#hashObject



37
38
39
# File 'lib/soup/snip.rb', line 37

def hash
  @attributes.hash
end

#inspectObject



30
31
32
33
34
35
# File 'lib/soup/snip.rb', line 30

def inspect
  keys = @attributes.keys.dup
  keys.delete(:name)
  attrs = keys.inject([[:name, self.name]]) { |a, key| a + [[key, @attributes[key]]] }
  "<Snip #{attrs.map { |(key,value)| "#{key}:#{value.inspect}"}.join(" ")}>"
end

#respond_to?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/soup/snip.rb', line 41

def respond_to?(method, include_all=false)
  @attributes.keys.any? { |k| k.to_s == method.to_s }
end

#saveObject



12
13
14
15
# File 'lib/soup/snip.rb', line 12

def save
  @backend.save_snip(@attributes)
  self
end