Class: Munger::Item

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Item

Returns a new instance of Item.



7
8
9
# File 'lib/munger/item.rb', line 7

def initialize(data)
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/munger/item.rb', line 24

def method_missing( id, *args )
  if @data[id].nil?
    m = id.to_s
    if /=$/ =~ m
		  @data[m.chomp!] = (args.length < 2 ? args[0] : args)
	  else
		  @data[m]
	  end
  else
    @data[id]
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/munger/item.rb', line 5

def data
  @data
end

Class Method Details

.ensure(item) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/munger/item.rb', line 37

def self.ensure(item)
  if item.is_a? Munger::Item
    return item
  else
    return Item.new(item)
  end
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/munger/item.rb', line 11

def [](key)
  return @data[key] if @data[key]
  if key.is_a? Symbol
    return @data[key.to_s] if @data[key.to_s]
  elsif key.is_a? String
    return @data[key.to_sym] if @data[key.to_sym]
  end
end

#[]=(key, value) ⇒ Object



20
21
22
# File 'lib/munger/item.rb', line 20

def []=(key, value)
  @data[key] = value
end

#to_hashObject



45
46
47
# File 'lib/munger/item.rb', line 45

def to_hash
  @data
end