Class: Purview::Columns::Base

Inherits:
Object
  • Object
show all
Includes:
Mixins::Helpers
Defined in:
lib/purview/columns/base.rb

Direct Known Subclasses

Boolean, Date, Float, Integer, Money, String, Text, Time, Timestamp, UUID

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Helpers

#blank?, #coalesced, #filter_blank_values, #filter_nil_values, #present?, #timestamp, #with_timestamp, #zero?

Constructor Details

#initialize(name, opts = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
# File 'lib/purview/columns/base.rb', line 6

def initialize(name, opts={})
  @name = name.to_sym
  @opts = default_opts.merge(opts)
  @table = table_opt
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/purview/columns/base.rb', line 4

def name
  @name
end

#tableObject

Returns the value of attribute table.



4
5
6
# File 'lib/purview/columns/base.rb', line 4

def table
  @table
end

Instance Method Details

#defaultObject



12
13
14
# File 'lib/purview/columns/base.rb', line 12

def default
  opts[:default]
end

#default?Boolean

Returns:



16
17
18
# File 'lib/purview/columns/base.rb', line 16

def default?
  !!default
end

#eql?(other) ⇒ Boolean

Returns:



20
21
22
23
24
25
26
27
# File 'lib/purview/columns/base.rb', line 20

def eql?(other)
  self.class == other.class &&
    limit == other.limit &&
    name == other.name &&
    nullable == other.nullable &&
    primary_key == other.primary_key &&
    type == other.type
end

#hashObject



29
30
31
32
33
34
35
36
# File 'lib/purview/columns/base.rb', line 29

def hash
  default.hash +
    limit.hash +
    name.hash +
    nullable.hash +
    primary_key.hash +
    type.hash
end

#limitObject



38
39
40
# File 'lib/purview/columns/base.rb', line 38

def limit
  opts[:limit]
end

#limit?Boolean

Returns:



42
43
44
# File 'lib/purview/columns/base.rb', line 42

def limit?
  !!limit
end

#nullableObject



46
47
48
# File 'lib/purview/columns/base.rb', line 46

def nullable
  [nil, true].include?(opts[:nullable])
end

#nullable?Boolean

Returns:



50
51
52
# File 'lib/purview/columns/base.rb', line 50

def nullable?
  !!nullable
end

#parse(value) ⇒ Object



54
55
56
57
58
# File 'lib/purview/columns/base.rb', line 54

def parse(value)
  blank = blank?(value)
  raise %{Unexpected blank value for column: "#{name}"} if blank && !nullable?
  blank ? nil : type.parse(value)
end

#primary_keyObject



60
61
62
# File 'lib/purview/columns/base.rb', line 60

def primary_key
  opts[:primary_key]
end

#primary_key?Boolean

Returns:



64
65
66
# File 'lib/purview/columns/base.rb', line 64

def primary_key?
  !!primary_key
end

#source_nameObject



68
69
70
# File 'lib/purview/columns/base.rb', line 68

def source_name
  (opts[:source_name] || name).to_sym
end

#typeObject



77
78
79
# File 'lib/purview/columns/base.rb', line 77

def type
  opts[:type] || Purview::Types::String
end