Module: TimestampStates

Extended by:
ActiveSupport::Concern
Defined in:
lib/timestamp_states.rb,
lib/timestamp_states/railtie.rb,
lib/timestamp_states/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Error, Railtie

Constant Summary collapse

VERSION =
'1.0.3'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/timestamp_states.rb', line 98

def method_missing(method_name, *args, &block)
  self.class.base_class.timestamp_state_configs.to_h.each do |column, options|
    return timestamp_state?(column) if method_name.to_s == "#{options[:words][:past]}?" || method_name.to_s == options[:words][:past].to_s
    return !timestamp_state?(column) if method_name.to_s == "#{options[:words][:past_not]}?" || method_name.to_s == options[:words][:past_not].to_s
    return touch_timestamp_state(column) if method_name.to_s == options[:words][:action].to_s
    return unset_timestamp_state(column) if method_name.to_s == options[:words][:not_action].to_s
    return touch_timestamp_state!(column) if method_name.to_s == "#{options[:words][:action]}!"
    return unset_timestamp_state!(column) if method_name.to_s == "#{options[:words][:not_action]}!"
    return set_timestamp_state(column, args.first) if method_name.to_s == "#{options[:words][:past]}="
    return set_timestamp_state(column, !args.first) if method_name.to_s == "#{options[:words][:past_not]}="
  end

  super
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
# File 'lib/timestamp_states.rb', line 113

def respond_to_missing?(method_name, include_private = false)
  self.class.base_class.timestamp_state_configs.to_h.each_value do |options|
    return true if method_name.to_s == "#{options[:words][:past]}?" || method_name.to_s == options[:words][:past].to_s
    return true if method_name.to_s == options[:words][:action].to_s || method_name.to_s == "#{options[:words][:action]}!"
    return true if method_name.to_s == options[:words][:not_action].to_s || method_name.to_s == "#{options[:words][:not_action]}!"
    return true if ["#{options[:words][:past]}=", "#{options[:words][:past_not]}="].include?(method_name.to_s)
  end

  super
end