Class: Tr8n::Base
- Inherits:
-
Object
- Object
- Tr8n::Base
- Defined in:
- lib/tr8n/base.rb
Overview
– Copyright © 2014 Michael Berkovich, TranslationExchange.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++
Direct Known Subclasses
ApiClient, Application, Component, Decorators::Base, Language, LanguageCase, LanguageCaseRule, LanguageContext, LanguageContextRule, Source, Translation, TranslationKey, Translator
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .attributes(*attrs) ⇒ Object
- .belongs_to(*attrs) ⇒ Object
- .has_many(*attrs) ⇒ Object
- .hash_value(hash, key, opts = {}) ⇒ Object
Instance Method Summary collapse
- #hash_value(hash, key, opts = {}) ⇒ Object
-
#initialize(attrs = {}) ⇒ Base
constructor
A new instance of Base.
- #method_missing(meth, *args, &block) ⇒ Object
- #to_hash(*attrs) ⇒ Object
- #update_attributes(attrs = {}) ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
28 29 30 31 |
# File 'lib/tr8n/base.rb', line 28 def initialize(attrs = {}) @attributes = {} update_attributes(attrs) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tr8n/base.rb', line 49 def method_missing(meth, *args, &block) method_name = meth.to_s method_suffix = method_name[-1, 1] method_key = method_name.to_sym if ['=', '?'].include?(method_suffix) method_key = method_name[0..-2].to_sym end if self.class.attributes.index(method_key) if method_suffix == '=' attributes[method_key] = args.first return attributes[method_key] end return attributes[method_key] end super end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
26 27 28 |
# File 'lib/tr8n/base.rb', line 26 def attributes @attributes end |
Class Method Details
.attributes(*attrs) ⇒ Object
41 42 43 44 45 |
# File 'lib/tr8n/base.rb', line 41 def self.attributes(*attrs) @attribute_names ||= [] @attribute_names += attrs.collect{|a| a.to_sym} unless attrs.nil? @attribute_names end |
.belongs_to(*attrs) ⇒ Object
46 |
# File 'lib/tr8n/base.rb', line 46 def self.belongs_to(*attrs) self.attributes(*attrs); end |
.has_many(*attrs) ⇒ Object
47 |
# File 'lib/tr8n/base.rb', line 47 def self.has_many(*attrs) self.attributes(*attrs); end |
.hash_value(hash, key, opts = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tr8n/base.rb', line 68 def self.hash_value(hash, key, opts = {}) return nil unless hash.is_a?(Hash) return hash[key.to_s] || hash[key.to_sym] if opts[:whole] value = hash key.to_s.split('.').each do |part| return nil unless value.is_a?(Hash) value = value[part.to_sym] || value[part.to_s] end value end |
Instance Method Details
#hash_value(hash, key, opts = {}) ⇒ Object
80 81 82 |
# File 'lib/tr8n/base.rb', line 80 def hash_value(hash, key, opts = {}) self.class.hash_value(hash, key, opts) end |
#to_hash(*attrs) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tr8n/base.rb', line 84 def to_hash(*attrs) if attrs.nil? or attrs.empty? # default hashing only includes basic types keys = [] self.class.attributes.each do |key| value = attributes[key] next if value.kind_of?(Tr8n::Base) or value.kind_of?(Hash) or value.kind_of?(Array) keys << key end else keys = attrs end hash = {} keys.each do |key| hash[key] = attributes[key] end proc = Proc.new { |k, v| v.kind_of?(Hash) ? (v.delete_if(&proc); nil) : v.nil? } hash.delete_if(&proc) hash end |
#update_attributes(attrs = {}) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/tr8n/base.rb', line 33 def update_attributes(attrs = {}) attrs.each do |key, value| #pp [self.class.name, key, self.class.attributes, self.class.attributes.include?(key.to_sym)] next unless self.class.attributes.include?(key.to_sym) @attributes[key.to_sym] = value end end |