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?, #zero?

Constructor Details

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

Returns a new instance of Base.



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

def initialize(name, opts={})
  @name = name.to_sym
  @opts = default_opts.merge(opts)
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

Instance Method Details

#defaultObject



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

def default
  opts[:default]
end

#default?Boolean

Returns:



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

def default?
  !!default
end

#limitObject



19
20
21
# File 'lib/purview/columns/base.rb', line 19

def limit
  opts[:limit]
end

#limit?Boolean

Returns:



23
24
25
# File 'lib/purview/columns/base.rb', line 23

def limit?
  !!limit
end

#nullableObject



27
28
29
# File 'lib/purview/columns/base.rb', line 27

def nullable
  coalesced(opts[:nullable], true)
end

#nullable?Boolean

Returns:



31
32
33
# File 'lib/purview/columns/base.rb', line 31

def nullable?
  !!nullable
end

#parse(value) ⇒ Object



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

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

#primary_keyObject



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

def primary_key
  opts[:primary_key]
end

#primary_key?Boolean

Returns:



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

def primary_key?
  !!primary_key
end

#typeObject



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

def type
  coalesced(opts[:type], Purview::Types::String)
end