Class: Primalize::Single

Inherits:
Object
  • Object
show all
Defined in:
lib/primalize/single.rb

Defined Under Namespace

Modules: Type Classes: Any, Array, Enum, Float, Integer, Number, Object, Optional, Primalizer, String, Timestamp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Single

Returns a new instance of Single.

Raises:

  • (ArgumentError)


104
105
106
107
# File 'lib/primalize/single.rb', line 104

def initialize object
  raise ArgumentError, "#{self.class.inspect} cannot serialize `nil'" if object.nil?
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



102
103
104
# File 'lib/primalize/single.rb', line 102

def object
  @object
end

Class Method Details

.add_attributes(attrs) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/primalize/single.rb', line 25

def add_attributes attrs
  return if attrs.none?

  @attributes.merge! attrs

  attrs.each do |attr, type|
    define_method attr do
      type.coerce(object.public_send(attr))
    end
  end
end

.any(*types, &coerce) ⇒ Object



77
78
79
# File 'lib/primalize/single.rb', line 77

def any *types, &coerce
  Any.new(types, &coerce)
end

.array(*types, &coerce) ⇒ Object



49
50
51
# File 'lib/primalize/single.rb', line 49

def array *types, &coerce
  Array.new(types, &coerce)
end

.attributes(attrs = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/primalize/single.rb', line 13

def attributes attrs={}
    @attributes ||= if self.equal? Primalize::Single
                      {}
                    else
                      superclass.attributes.dup
                    end

  add_attributes attrs

  @attributes
end

.boolean(&coerce) ⇒ Object



45
46
47
# File 'lib/primalize/single.rb', line 45

def boolean &coerce
  enum(true, false, &coerce)
end

.enum(*values, &coerce) ⇒ Object



69
70
71
# File 'lib/primalize/single.rb', line 69

def enum *values, &coerce
  Enum.new(values, &coerce)
end

.float(&coerce) ⇒ Object



57
58
59
# File 'lib/primalize/single.rb', line 57

def float &coerce
  Float.new(&coerce)
end

.inspectObject



97
98
99
# File 'lib/primalize/single.rb', line 97

def inspect
  "#{name}(#{attributes.map { |attr, type| "#{attr}: #{type.inspect}" }.join(', ') })"
end

.integer(&coerce) ⇒ Object



37
38
39
# File 'lib/primalize/single.rb', line 37

def integer &coerce
  Integer.new(&coerce)
end

.number(&coerce) ⇒ Object



61
62
63
# File 'lib/primalize/single.rb', line 61

def number &coerce
  Number.new(&coerce)
end

.object(**types, &coerce) ⇒ Object



53
54
55
# File 'lib/primalize/single.rb', line 53

def object **types, &coerce
  Object.new(types, &coerce)
end

.optional(*types, &coerce) ⇒ Object



65
66
67
# File 'lib/primalize/single.rb', line 65

def optional *types, &coerce
  Optional.new(types, &coerce)
end

.primalize(primalizer, &coerce) ⇒ Object



81
82
83
# File 'lib/primalize/single.rb', line 81

def primalize primalizer, &coerce
  Primalizer.new(primalizer, &coerce)
end

.string(&coerce) ⇒ Object



41
42
43
# File 'lib/primalize/single.rb', line 41

def string &coerce
  String.new(&coerce)
end

.timestamp(&coerce) ⇒ Object



73
74
75
# File 'lib/primalize/single.rb', line 73

def timestamp &coerce
  Timestamp.new(&coerce)
end

.type_mismatch_handlerObject



89
90
91
92
93
94
95
# File 'lib/primalize/single.rb', line 89

def type_mismatch_handler
  if @type_mismatch_handler
    @type_mismatch_handler
  else
    superclass.type_mismatch_handler
  end
end

.type_mismatch_handler=(handler) ⇒ Object



85
86
87
# File 'lib/primalize/single.rb', line 85

def type_mismatch_handler= handler
  @type_mismatch_handler = handler
end

Instance Method Details

#callObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/primalize/single.rb', line 109

def call
  self.class.attributes.each_with_object({}) do |(attr, type), hash|
    value = public_send(attr)

    if type === value
      hash[attr] = value
    else
      self.class.type_mismatch_handler.call self.class, attr, type, value
    end
  end
end

#csv_headersObject



127
128
129
# File 'lib/primalize/single.rb', line 127

def csv_headers
  self.class.attributes.keys.map(&:to_s)
end

#to_csvObject



131
132
133
134
# File 'lib/primalize/single.rb', line 131

def to_csv
  hash = call
  CSV.generate { |csv| csv << hash.values }
end

#to_jsonObject

CONVERSION



123
124
125
# File 'lib/primalize/single.rb', line 123

def to_json
  call.to_json
end