Class: TinyDS::PropertyDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_ds/property_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(pname, ptype, opts) ⇒ PropertyDefinition

Returns a new instance of PropertyDefinition.



3
4
5
6
7
# File 'lib/tiny_ds/property_definition.rb', line 3

def initialize(pname, ptype, opts)
  @pname = pname
  @ptype = ptype
  @opts  = opts
end

Instance Method Details

#default_valueObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/tiny_ds/property_definition.rb', line 8

def default_value
  raise "no default." unless has_default?
  default = @opts[:default]
  case default
  when Proc
    default.call
  else
    default
  end
end

#has_default?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/tiny_ds/property_definition.rb', line 18

def has_default?
  @opts.has_key?(:default)
end

#to_ds_value(v) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tiny_ds/property_definition.rb', line 21

def to_ds_value(v)
  case @ptype
  when :string
    v.nil? ? nil : v.to_s
  when :integer
    v.nil? ? nil : v.to_i
  when :text
    v.nil? ? nil : com.google.appengine.api.datastore::Text.new(v.to_s)
  when :time
    case v
    when Time
      v
    when NilClass
      nil
    else
      raise "not Time value."
    end
  when :list
    case v
    when Array
      v.empty? ? nil : v
    when NilClass
      nil
    else
      raise "not Array value."
    end
  else
    raise "unknown property type '#{@ptype}'"
  end
end

#to_ruby_value(ds_v) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tiny_ds/property_definition.rb', line 51

def to_ruby_value(ds_v)
  case @ptype
  when :string
    ds_v.nil? ? nil : ds_v.to_s
  when :integer
    ds_v.nil? ? nil : ds_v.to_i
  when :text
    ds_v.nil? ? nil : ds_v.to_s
  when :time
    ds_v.nil? ? nil : ds_v
  when :list
    ds_v.nil? ? []  : ds_v.to_a
  else
    raise "unknown property type '#{@ptype}'"
  end
end