Class: Walletone::Fields
- Inherits:
-
Hash
- Object
- Hash
- Walletone::Fields
- Defined in:
- lib/walletone/fields.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #as_list ⇒ Object
- #fetch(key, default = nil) ⇒ Object
-
#initialize(attrs = {}) ⇒ Fields
constructor
A new instance of Fields.
- #to_s ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Fields
Returns a new instance of Fields.
22 23 24 25 26 27 |
# File 'lib/walletone/fields.rb', line 22 def initialize attrs={} fields = super return fields if attrs.empty? symbolyzed_attrs = attrs.inject({}) { |acc, e| acc[e.first.to_sym]=e.last; acc } fields.merge! symbolyzed_attrs end |
Class Method Details
.define_fields(fields) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/walletone/fields.rb', line 5 def self.define_fields fields # Определяем методы для прямого доступа # > payment.WMI_MERCHANT_ID # > payment.WMI_MERCHANT_ID=123 # # > payment.WMI_PTENABLED = fields.each do |k| define_method k do fetch k end define_method "#{k}=" do |value| self[k]= value end end end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 |
# File 'lib/walletone/fields.rb', line 29 def [] key fetch key.to_sym, nil end |
#[]=(key, value) ⇒ Object
33 34 35 |
# File 'lib/walletone/fields.rb', line 33 def []= key, value super key.to_sym, value end |
#as_list ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/walletone/fields.rb', line 41 def as_list keys.inject([]) do |acc, name| # Делаем именно так, чтобы работало переопределение методов # в Payment value = respond_to?(name) ? send(name) : fetch(name) Array(value).each { |v| acc << [name.to_s, v.to_s] } acc end end |
#fetch(key, default = nil) ⇒ Object
37 38 39 |
# File 'lib/walletone/fields.rb', line 37 def fetch key, default=nil super key.to_sym, default end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/walletone/fields.rb', line 51 def to_s as_list.to_s end |