Class: Jung::Recipient
- Inherits:
-
Object
- Object
- Jung::Recipient
- Defined in:
- lib/jung/recipient.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#attribute_names ⇒ Object
readonly
Returns the value of attribute attribute_names.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #attribute_method?(method_name) ⇒ Boolean
- #get_attribute(attr_name) ⇒ Object
-
#initialize(attributes) ⇒ Recipient
constructor
A new instance of Recipient.
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to?(method_name, include_private = false) ⇒ Boolean
- #set_attribute(attr_name, value) ⇒ Object
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
#address ⇒ Object
Returns the value of attribute address.
4 5 6 |
# File 'lib/jung/recipient.rb', line 4 def address @address end |
#attribute_names ⇒ Object (readonly)
Returns the value of attribute attribute_names.
5 6 7 |
# File 'lib/jung/recipient.rb', line 5 def attribute_names @attribute_names end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/jung/recipient.rb', line 6 def attributes @attributes end |
#name ⇒ Object
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
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
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 |