Module: Riveter::Attributes::ClassMethods

Defined in:
lib/riveter/attributes.rb

Instance Method Summary collapse

Instance Method Details

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



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

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, options) }
  end

  attr_writer name

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

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/riveter/attributes.rb', line 143

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, options
  alias_method "#{name}?", name

  attr_writer name

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

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/riveter/attributes.rb', line 234

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

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

  attr_reader_with_converter name, converter, options

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

  attr_writer name

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

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



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

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

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



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/riveter/attributes.rb', line 61

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

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

  [:min, :max].each do |limit|
    limit_value = options.delete(limit)

    define_method :"#{name}_#{limit}" do
      instance_variable_get("@#{name}_#{limit}") ||
        (limit_value.respond_to?(:call) ? instance_exec(&limit_value) : limit_value)
    end

    # can manually assign the min/max
    define_method :"#{name}_#{limit}=" do |value|
      instance_variable_set("@#{name}_#{limit}", value)
    end

  end

  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

  add_attr(name, :date_range, converter, options)

  if options[:validate]

    validate :"#{name}_validation"

    define_method :"#{name}_validation" do
      date_from = send(:"#{name}_from")
      date_to = send(:"#{name}_to")

      errors.add(name, :blank) if
        required && (date_from.blank? || date_to.blank?)

      errors.add(name, :invalid) if
        !(date_from.blank? || date_to.blank?) && (date_from > date_to || date_to < date_from)
    end

  end

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

    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)
  end

  # helper for determining if both from and to dates have been provided
  define_method :"#{name}_present?" do
    date_from = send(:"#{name}_from")
    date_to = send(:"#{name}_to")
    date_from && date_to
  end

end

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



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

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

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

NB: enum must respond to values

Raises:

  • (ArgumentError)


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

def attr_enum(name, enum, options={}, &block)
  raise ArgumentError, 'enum' unless enum && enum.respond_to?(:values)

  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, options

  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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/riveter/attributes.rb', line 201

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, options) }
  end

  attr_writer name

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

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



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

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

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



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/riveter/attributes.rb', line 220

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

  converter = block_given? ? block : Converters.converter_for(:object, options)

  attr_reader_with_converter name, converter, options

  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
36
37
38
39
# File 'lib/riveter/attributes.rb', line 28

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

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

  attr_reader_with_converter name, converter, options
  attr_writer name

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

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



55
56
57
# File 'lib/riveter/attributes.rb', line 55

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