Class: Tml::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tml/base.rb

Overview

– Copyright © 2017 Translation Exchange, Inc

_______                  _       _   _             ______          _

|__ __| | | | | (_) | __| | |

| |_ __ __ _ _ __  ___| | __ _| |_ _  ___  _ __ | |__  __  _____| |__   __ _ _ __   __ _  ___
| | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \|  __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
| | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ >  < (__| | | | (_| | | | | (_| |  __/
|_|_|  \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
                                                                                    __/ |
                                                                                   |___/

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. ++

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



36
37
38
39
# File 'lib/tml/base.rb', line 36

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tml/base.rb', line 57

def method_missing(meth, *args, &block)
  method_name = meth.to_s
  method_suffix = method_name[-1, 1]
  method_key = method_name.to_sym
  if %w(= ?).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

#attributesObject (readonly)

Returns the value of attribute attributes.



34
35
36
# File 'lib/tml/base.rb', line 34

def attributes
  @attributes
end

Class Method Details

.attributes(*attrs) ⇒ Object



49
50
51
52
53
# File 'lib/tml/base.rb', line 49

def self.attributes(*attrs)
  @attribute_names ||= []
  @attribute_names += attrs.collect{|a| a.to_sym} unless attrs.nil?
  @attribute_names
end

.belongs_to(*attrs) ⇒ Object



54
# File 'lib/tml/base.rb', line 54

def self.belongs_to(*attrs) self.attributes(*attrs); end

.has_many(*attrs) ⇒ Object



55
# File 'lib/tml/base.rb', line 55

def self.has_many(*attrs) self.attributes(*attrs); end

.hash_value(hash, key, opts = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tml/base.rb', line 76

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



88
89
90
# File 'lib/tml/base.rb', line 88

def hash_value(hash, key, opts = {})
  self.class.hash_value(hash, key, opts)
end

#to_hash(*attrs) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tml/base.rb', line 92

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?(Tml::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



41
42
43
44
45
46
47
# File 'lib/tml/base.rb', line 41

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