Module: Yamori::BaseMethods

Defined in:
lib/yamori/base_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(c) ⇒ Object



3
4
5
# File 'lib/yamori/base_methods.rb', line 3

def self.included(c)
  c.extend ClassMethods
end

Instance Method Details

#initialize(attributes = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yamori/base_methods.rb', line 21

def initialize(attributes = {})
  @original_attributes = {}
  @current_attributes = {}
  @updated_attributes = {}

  attributes.each do |k, v|
    field_name = k.to_sym
    if self.class.field_names.include?(field_name)
      @original_attributes[field_name] = v
      __send__ (field_name.to_s + '='), v
    elsif self.class.parent_relations.find{|r| r[:name] == field_name}
      __send__ (field_name.to_s + '='), v
    elsif self.class.child_relations.find{|r| r[:name] == field_name}
      __send__ (field_name.to_s + '='), (v.nil? ? [] : v)
    end
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/yamori/base_methods.rb', line 49

def new_record?
  self.Id.nil?
end

#persisted?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/yamori/base_methods.rb', line 53

def persisted?
  new_record? == false
end

#to_h(keys: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/yamori/base_methods.rb', line 39

def to_h(keys: nil)
  self.class.field_names.each_with_object({}) do |name, hash|
    if keys&.instance_of?(Array)
      hash[name] = __send__(name) if keys.include?(name)
    else
      hash[name] = __send__(name)
    end
  end
end