Class: Plyushkin::BaseValue

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/plyushkin/base_value.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr = {}) ⇒ BaseValue

Returns a new instance of BaseValue.



4
5
6
7
8
9
10
11
12
# File 'lib/plyushkin/base_value.rb', line 4

def initialize(attr={})
  @new_record = attr[:new_record].nil? ? true : attr[:new_record] 

  attr   = attr.dup
  @date  = attr.delete(:date) || DateTime.current
  attr.each do |k,v|
    send("#{k}=", v) if respond_to?("#{k}=")
  end
end

Class Method Details

.formattersObject



56
57
58
59
60
61
62
# File 'lib/plyushkin/base_value.rb', line 56

def self.formatters
  if self.superclass == Object
    @formatters ||= {}
  else
    @formatters ||= superclass.formatters.dup
  end
end

.persisted_attr(*attributes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plyushkin/base_value.rb', line 26

def self.persisted_attr(*attributes)
  opts = attributes.last.is_a?(Hash) ? attributes.pop : {}
  names = attributes
  formatter = opts[:formatter]

  names.each{|name| formatters[name] = formatter}

  persisted_attributes.concat(names)

  attr_writer *names
  names.each do |name|
    define_method(name) do
      value = instance_variable_get("@#{name}")
      if formatter
        send(formatter, value)
      else
        value
      end
    end
  end
end

.persisted_attributesObject



48
49
50
51
52
53
54
# File 'lib/plyushkin/base_value.rb', line 48

def self.persisted_attributes
  if self.superclass == Object && self.superclass.class == Class
    @persisted_attributes ||= []
  else
    @persisted_attributes ||= superclass.persisted_attributes.dup
  end
end

Instance Method Details

#equal_value?(other) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/plyushkin/base_value.rb', line 69

def equal_value?(other)
  self.class.persisted_attributes.each do |attribute|
    return false if attribute != :date && send(attribute) != other.send(attribute)
  end
  true
end

#formattersObject



22
23
24
# File 'lib/plyushkin/base_value.rb', line 22

def formatters
  @formatters.dup
end

#mark_persistedObject



18
19
20
# File 'lib/plyushkin/base_value.rb', line 18

def mark_persisted
  @new_record = false
end

#new_record?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/plyushkin/base_value.rb', line 14

def new_record?
  @new_record
end

#to_bool(value) ⇒ Object



95
96
97
98
99
# File 'lib/plyushkin/base_value.rb', line 95

def to_bool(value)
  return true if [1, "1", "true"].include?(value)
  return false if [0, "0", "false"].include?(value)
  return value
end

#to_date(value) ⇒ Object



91
92
93
# File 'lib/plyushkin/base_value.rb', line 91

def to_date(value)
  value.is_a?(String) ? DateTime.parse(value) : value
end

#to_f(value) ⇒ Object

TODO: Maybe this can be nicer.



81
82
83
84
85
86
87
88
89
# File 'lib/plyushkin/base_value.rb', line 81

def to_f(value)
  begin
    Float(value)
  rescue ArgumentError => e
    value
  rescue TypeError => e
    value
  end
end

#to_i(value) ⇒ Object



76
77
78
# File 'lib/plyushkin/base_value.rb', line 76

def to_i(value)
  value =~ /\A\d+(\.\d+)?\Z/ ? value.to_i : value
end