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
# 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
# File 'lib/purview/columns/base.rb', line 35

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

#primary_keyObject



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

def primary_key
  opts[:primary_key]
end

#primary_key?Boolean

Returns:



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

def primary_key?
  !!primary_key
end

#typeObject



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

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