Module: LocaleDating::ClassMethods

Defined in:
lib/locale_dating.rb

Instance Method Summary collapse

Instance Method Details

#locale_date(*args) ⇒ Object

Define how to split out a single date_time column/attribute into two attributes that can set the date and time portions independently. Can be called multiple times per

Arguments

Accepts a list of attribute names that access date related model data.

  • options - Options for overriding configuration.

Options

  • :format - The desired date format name from the locale file to use for displaying and parsing

    the value as text.
    
  • :ending - The ending used on the wrapping methods for accessing and assigning the value.

    Defaults to :as_text
    
  • :name - The explicit wrapper method name to use for reading and writing the value as text.

    Overrides and :ending option.
    

Example

Standard usage accepting defaults.

locale_date :starts_on
# creates methods "starts_on_as_text" and "starts_on_as_text=".

Specify a different format from the locale file and a different suffix.

locale_date :starts_on, :format => :special, :ending => :text
# creates methods "starts_on_text" and "starts_on_text=". The :special format will be used
# for display and parsing.


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/locale_dating.rb', line 37

def locale_date(*args)
  options = args.extract_options!
  locale_dating_naming_checks(args, options)

  # Loop through all the given attributes that should be wrapped using the same settings.
  args.each do |attrib|
    getter_name, setter_name = locale_dating_wrapper_method_names(attrib, options)
    # Define the code to execute when the method is called
    # Create new methods for get and set calls with blocks for implementation.
    class_eval do
      # == Create the GET methods
      # EX: def birth_date_as_text()
      define_method getter_name do
        value = self.send(attrib)
        I18n.l(value, :format => options[:format]) if value
      end
      # == Create the SET methods
      # EX: def birth_date_as_text=()
      define_method setter_name do |value|
        date_value = DateTime.strptime(value.to_s, I18n.t("date.formats.#{options[:format]}")) unless value.blank?
        # Keep the date from the given value and preserve the original time part
        self.send("#{attrib}=", date_value)
      end
    end
  end
end

#locale_datetime(*args) ⇒ Object

Define how to split out a single date_time column/attribute into two attributes that can set the date and time portions independently. Can be called multiple times per

Arguments

Accepts a list of attribute names that access date related model data.

  • options - Options for overriding configuration.

Options

  • :format - The desired datetime format name from the locale file to use for displaying and parsing

    the value as text.
    
  • :ending - The suffix used on the wrapping methods for accessing and assigning the value.

  • :name - The explicit wrapper method name to use for reading and writing the value as text.

    Overrides and :ending option.
    

Example

Standard usage accepting defaults.

date_time_split :starts_at
# creates methods "starts_at_date" and "starts_at_time" that both write to the "starts_at" column.

Override default method maps to custom ones.

date_time_split :starts_at, :date => :date_starts_at, :time_starts_at
# creates methods "date_starts_at" and "time_starts_at" that both write to the "starts_at" column.


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/locale_dating.rb', line 141

def locale_datetime(*args)
  options = args.extract_options!
  locale_dating_naming_checks(args, options)

  # Loop through all the given attributes that should be wrapped using the same settings.
  args.each do |attrib|
    getter_name, setter_name = locale_dating_wrapper_method_names(attrib, options)
    # Define the code to execute when the method is called
    # Create new methods for get and set calls with blocks for implementation.
    class_eval do
      # == Create the GET methods
      # EX: def completed_at_as_text()
      define_method getter_name do
        value = self.send(attrib)
        I18n.l(value.in_time_zone, :format => options[:format]) if value
      end
      # == Create the SET methods
      # EX: def completed_at_as_text=()
      define_method setter_name do |value|
        if !value.blank?
          date_value = DateTime.strptime(value.to_s, I18n.t("datetime.formats.#{options[:format]}"))
          date_value = Time.zone.local(date_value.year, date_value.month, date_value.day,
                                       date_value.hour, date_value.min, date_value.sec)
        end
        # Keep the date from the given value and preserve the original time part
        self.send("#{attrib}=", date_value)
      end
    end
  end

end

#locale_time(*args) ⇒ Object

Define how to split out a single date_time column/attribute into two attributes that can set the date and time portions independently. Can be called multiple times per

Arguments

Accepts a list of attribute names that access date related model data.

  • options - Options for overriding configuration.

Options

  • :format - The desired time format name from the locale file to use for displaying and parsing

    the value as text.
    
  • :ending - The suffix used on the wrapping methods for accessing and assigning the value.

  • :name - The explicit wrapper method name to use for reading and writing the value as text.

    Overrides and :ending option.
    

Example

Standard usage accepting defaults.

date_time_split :starts_at
# creates methods "starts_at_date" and "starts_at_time" that both write to the "starts_at" column.

Override default method maps to custom ones.

date_time_split :starts_at, :date => :date_starts_at, :time_starts_at
# creates methods "date_starts_at" and "time_starts_at" that both write to the "starts_at" column.


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
# File 'lib/locale_dating.rb', line 87

def locale_time(*args)
  options = args.extract_options!
  locale_dating_naming_checks(args, options)

  # Loop through all the given attributes that should be wrapped using the same settings.
  args.each do |attrib|
    getter_name, setter_name = locale_dating_wrapper_method_names(attrib, options)
    # Define the code to execute when the method is called
    # Create new methods for get and set calls with blocks for implementation.
    class_eval do
      # == Create the GET methods
      # EX: def start_time_as_text()
      define_method getter_name do
        value = self.send(attrib)
        I18n.l(value.in_time_zone, :format => options[:format]) if value
      end
      # == Create the SET methods
      # EX: def start_time_as_text=()
      define_method setter_name do |value|
        if !value.blank?
          time_value = DateTime.strptime(value.to_s, I18n.t("time.formats.#{options[:format]}"))
          time_value = Time.zone.local(time_value.year, time_value.month, time_value.day,
                                       time_value.hour, time_value.min, time_value.sec)
        end
        # Keep the date from the given value and preserve the original time part
        self.send("#{attrib}=", time_value)
      end
    end
  end
end