Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/test-factory/core_ext.rb
Overview
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Class Method Summary collapse
-
.random(params = {}) ⇒ Object
Using the :year_range option (or no option), this method creates a Time object of a random value, within the year range specified (default is 5 years in the past).
Class Method Details
.random(params = {}) ⇒ Object
Using the :year_range option (or no option), this method creates a Time object of a random value, within the year range specified (default is 5 years in the past).
Using the :series option, this method returns an array containing a randomized Time object as its first element (limited by the specified :year_range value). Subsequent elements will be Time objects with values putting them later than the prior element, within the specified range value (see examples).
Usage Examples:
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/test-factory/core_ext.rb', line 54 def self.random(params={}) years_back = params[:year_range] || 5 year = (rand * (years_back)).ceil + (Time.now.year - years_back) month = (rand * 12).ceil day = (rand * 31).ceil series = [date = Time.local(year, month, day)] if params[:series] params[:series].each do |some_time_after| series << series.last + (rand * some_time_after).ceil end return series end date end |