Module: Riveter::Attributes::ClassMethods

Defined in:
lib/riveter/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attr_array(name, options = {}, &block) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/riveter/attributes.rb', line 160

def attr_array(name, options={}, &block)
  options = {
    :data_type => :integer,
    :validate => true
  }.merge(options)
  data_type = options.delete(:data_type)

  converter = block_given? ? block : Converters.converter_for(data_type)

  define_method name do
    array = instance_variable_get("@#{name}") || []
    array.map {|v| converter.call(v) }
  end

  attr_writer name

  add_attr(name, :array, converter, options)
end

#attr_boolean(name, options = {}, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/riveter/attributes.rb', line 113

def attr_boolean(name, options={}, &block)
  options = {
    :validate => true
  }.merge(options)

  converter = block_given? ? block : Converters.converter_for(:boolean)

  attr_reader_with_converter name, converter
  alias_method "#{name}?", name

  attr_writer name

  add_attr(name, :boolean, converter, options)
end

#attr_date(name, options = {}, &block) ⇒ Object



47
48
49
# File 'lib/riveter/attributes.rb', line 47

def attr_date(name, options={}, &block)
  attr_date_or_time(:date, name, options, &block)
end

#attr_date_range(name, options = {}, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/riveter/attributes.rb', line 57

def attr_date_range(name, options={}, &block)
  options = {
    :validate => true
  }.merge(options)

  required = (true == options.delete(:required))

  # expecting the default value to be a date range
  # so extract out the first and last parts for the from and to
  # otherwise, it may just be a date or nil
  default = options.delete(:default)
  default = default.is_a?(Range) ? default : (default..default)
  defaults = {
    :from => default.first,
    :to => default.last
  }

  converter = block_given? ? block : Converters.converter_for(:date)

  # return from and to as range
  define_method name do
    # can't have range starting or ending with nil, so return nil
    send(:"#{name}_from")..send(:"#{name}_to") rescue nil
  end

  define_method "#{name}=" do |value|
    value ||= nil..nil
    range = value.is_a?(Range) ? value : value..value
    send(:"#{name}_from=", range.first)
    send(:"#{name}_to=", range.last)
  end

  validates name,
            :allow_nil => !required,
            :date_range => true if options[:validate]

  add_attr(name, :date_range)

  # break down into parts
  [:from, :to].each do |part|
    attr_reader_with_converter :"#{name}_#{part}", converter

    define_method :"#{name}_#{part}?" do
      send(:"#{name}_#{part}").present?
    end

    attr_writer :"#{name}_#{part}"

    validates :"#{name}_#{part}",
              :allow_nil => !required,
              :timeliness => { :type => :date } if options[:validate]

    add_attr(:"#{name}_#{part}", :date, converter, options.merge(:default => defaults[part]))
  end
end

#attr_decimal(name, options = {}, &block) ⇒ Object



43
44
45
# File 'lib/riveter/attributes.rb', line 43

def attr_decimal(name, options={}, &block)
  attr_numeric(:decimal, name, options, &block)
end

#attr_enum(name, enum, options = {}, &block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/riveter/attributes.rb', line 128

def attr_enum(name, enum, options={}, &block)
  options = {
    :enum => enum,
    :validate => true
  }.merge(options)

  required = options[:required] == true
  converter = block_given? ? block : Converters.converter_for(:enum, options)

  attr_reader_with_converter name, converter

  # helpers
  # TODO: would be nicer to emulate an association

  define_singleton_method "#{name}_enum" do
    enum
  end

  define_singleton_method name.to_s.pluralize do
    enum.collection
  end

  validates name,
            :allow_blank => !required,
            :allow_nil => !required,
            :inclusion => { :in => enum.values } if options[:validate]

  attr_writer name

  add_attr(name, :enum, converter, options)
end

#attr_hash(name, options = {}, &block) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/riveter/attributes.rb', line 179

def attr_hash(name, options={}, &block)
  options = {
    :data_type => :integer,
    :validate => true
  }.merge(options)
  data_type = options.delete(:data_type)

  converter = block_given? ? block : Converters.converter_for(data_type)

  define_method name do
    hash = instance_variable_get("@#{name}") || {}
    hash.merge(hash) {|k, v| converter.call(v) }
  end

  attr_writer name

  add_attr(name, :hash, converter, options)
end

#attr_integer(name, options = {}, &block) ⇒ Object



39
40
41
# File 'lib/riveter/attributes.rb', line 39

def attr_integer(name, options={}, &block)
  attr_numeric(:integer, name, options, &block)
end

#attr_model(name, model_or_scope, options = {}, &block) ⇒ Object

FIXME: this doesn’t work as expected



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/riveter/attributes.rb', line 201

def attr_model(name, model_or_scope, options={}, &block)
  options = {
    :model => model_or_scope,
    :validate => true,
    :find_by => :id
  }.merge(options)

  required = options[:required] == true
  converter = if block_given?
                block
              elsif model_or_scope.respond_to?(:find_by)
                Converters.converter_for(:model, options)
              else
                Converters.converter_for(:object, options)
              end

  # helpers
  define_singleton_method "#{name}_model" do
    model_or_scope
  end

  attr_reader_with_converter name, converter

  # only add validation of the model instance if supported
  if model_or_scope.instance_methods.include?(:valid?) && options[:validate]
    validate :"validate_#{name}"

    # need a "custom" associated validation since
    # we don't reference active record...
    define_method :"validate_#{name}" do
      instance = self.send(name)
      return unless required && instance.present?
      self.errors.add(name, :invalid) unless instance.valid?
    end
  end

  attr_writer name

  add_attr(name, :model, converter, options)
end

#attr_object(name, options = {}, &block) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'lib/riveter/attributes.rb', line 242

def attr_object(name, options={}, &block)
  converter = block_given? ? block : Converters.converter_for(:object, options)

  attr_reader_with_converter name, converter

  attr_writer name

  add_attr(name, :object, converter, options)
end

#attr_string(name, options = {}, &block) ⇒ Object Also known as: attr_text



28
29
30
31
32
33
34
35
# File 'lib/riveter/attributes.rb', line 28

def attr_string(name, options={}, &block)
  converter = block_given? ? block : Converters.converter_for(:string)

  attr_reader_with_converter name, converter
  attr_writer name

  add_attr(name, :string, converter, options)
end

#attr_time(name, options = {}, &block) ⇒ Object Also known as: attr_datetime



51
52
53
# File 'lib/riveter/attributes.rb', line 51

def attr_time(name, options={}, &block)
  attr_date_or_time(:time, name, options, &block)
end