Class: Jung::Recipient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Recipient

Returns a new instance of Recipient.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jung/recipient.rb', line 8

def initialize(attributes)
  @attribute_names = []
  @attributes = {}

  @name = attributes.delete(:name)
  @address = attributes.delete(:address)

  @attribute_names = attributes.keys.uniq.map(&:to_sym)

  attributes.each_pair do |k, v|
    @attributes[k.to_sym] = v
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



37
38
39
40
41
42
43
44
# File 'lib/jung/recipient.rb', line 37

def method_missing(method_name, *args, &block)
  accessor, attr_name = attribute_method?(method_name)
  if accessor
    send("#{accessor}_attribute", attr_name, *args, &block)
  else
    super method_name, *args, &block
  end
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



4
5
6
# File 'lib/jung/recipient.rb', line 4

def address
  @address
end

#attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



5
6
7
# File 'lib/jung/recipient.rb', line 5

def attribute_names
  @attribute_names
end

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/jung/recipient.rb', line 6

def attributes
  @attributes
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/jung/recipient.rb', line 4

def name
  @name
end

Instance Method Details

#attribute_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/jung/recipient.rb', line 30

def attribute_method?(method_name)
  match = method_name.to_s.match(/^(?<attr_name>.*?)(?<setter>=?)$/)
  accessor = match[:setter] == '=' ? :set : :get
  attr_name = match[:attr_name].to_sym
  attribute_names.include?(attr_name) ? [accessor, attr_name] : false
end

#get_attribute(attr_name) ⇒ Object



22
23
24
# File 'lib/jung/recipient.rb', line 22

def get_attribute(attr_name)
  @attributes[attr_name]
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/jung/recipient.rb', line 46

def respond_to?(method_name, include_private = false)
  if attribute_method?(method_name)
    true
  else
    super method_name, include_private
  end
end

#set_attribute(attr_name, value) ⇒ Object



26
27
28
# File 'lib/jung/recipient.rb', line 26

def set_attribute(attr_name, value)
  @attributes[attr_name] = value
end