Class: Torque::PostgreSQL::Attributes::Builder::Period
- Inherits:
-
Object
- Object
- Torque::PostgreSQL::Attributes::Builder::Period
- Defined in:
- lib/torque/postgresql/attributes/builder/period.rb
Overview
TODO: Allow documenting by building the methods outside and importing only the raw string
Constant Summary collapse
- DIRECT_ACCESS_REGEX =
/_?%s_?/
- SUPPORTED_TYPES =
%i[daterange tsrange tstzrange].freeze
- CURRENT_GETTERS =
{ daterange: 'Date.today', tsrange: 'Time.zone.now', tstzrange: 'Time.zone.now', }.freeze
- TYPE_CASTERS =
{ daterange: :date, tsrange: :timestamp, tstzrange: :timestamp, }.freeze
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#current_getter ⇒ Object
Returns the value of attribute current_getter.
-
#default ⇒ Object
Returns the value of attribute default.
-
#dynamic_threshold ⇒ Object
Returns the value of attribute dynamic_threshold.
-
#instance_module ⇒ Object
Returns the value of attribute instance_module.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#klass_module ⇒ Object
Returns the value of attribute klass_module.
-
#options ⇒ Object
Returns the value of attribute options.
-
#threshold ⇒ Object
Check if can identify a threshold field.
-
#type ⇒ Object
Returns the value of attribute type.
-
#type_caster ⇒ Object
Returns the value of attribute type_caster.
Instance Method Summary collapse
-
#build ⇒ Object
Create all methods needed.
- #build_method_helper(type, key, args = []) ⇒ Object
-
#conflicting? ⇒ Boolean
Check if any of the methods that will be created get in conflict with the base class methods.
-
#initialize(klass, attribute, options) ⇒ Period
constructor
Start a new builder of methods for period values on ActiveRecord::Base.
-
#instance_method_names ⇒ Object
Get the list of methods associated withe the instances.
-
#klass_method_names ⇒ Object
Get the list of methods associated withe the class.
-
#method_names ⇒ Object
Generate all the method names.
Constructor Details
#initialize(klass, attribute, options) ⇒ Period
Start a new builder of methods for period values on ActiveRecord::Base
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 29 def initialize(klass, attribute, ) @klass = klass @attribute = attribute.to_s @options = @type = klass.attribute_types[@attribute].type raise ArgumentError, <<-MSG.squish unless SUPPORTED_TYPES.include?(type) Period cannot be generated for #{attribute} because its type #{type} is not supported. Only #{SUPPORTED_TYPES.join(', ')} are supported. MSG @current_getter = CURRENT_GETTERS[type] @type_caster = TYPE_CASTERS[type] @default = [:pessimistic].blank? end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def attribute @attribute end |
#current_getter ⇒ Object
Returns the value of attribute current_getter.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def current_getter @current_getter end |
#default ⇒ Object
Returns the value of attribute default.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def default @default end |
#dynamic_threshold ⇒ Object
Returns the value of attribute dynamic_threshold.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def dynamic_threshold @dynamic_threshold end |
#instance_module ⇒ Object
Returns the value of attribute instance_module.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def instance_module @instance_module end |
#klass ⇒ Object
Returns the value of attribute klass.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def klass @klass end |
#klass_module ⇒ Object
Returns the value of attribute klass_module.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def klass_module @klass_module end |
#options ⇒ Object
Returns the value of attribute options.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def @options end |
#threshold ⇒ Object
Check if can identify a threshold field
47 48 49 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 47 def threshold @threshold end |
#type ⇒ Object
Returns the value of attribute type.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def type @type end |
#type_caster ⇒ Object
Returns the value of attribute type_caster.
24 25 26 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 24 def type_caster @type_caster end |
Instance Method Details
#build ⇒ Object
Create all methods needed
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 104 def build @klass_module = Module.new @instance_module = Module.new value_args = ['value'] left_right_args = ['left', 'right = nil'] ## Klass methods build_method_helper :klass, :current_on, value_args # 00 build_method_helper :klass, :current # 01 build_method_helper :klass, :not_current # 02 build_method_helper :klass, :containing, value_args # 03 build_method_helper :klass, :not_containing, value_args # 04 build_method_helper :klass, :overlapping, left_right_args # 05 build_method_helper :klass, :not_overlapping, left_right_args # 06 build_method_helper :klass, :starting_after, value_args # 07 build_method_helper :klass, :starting_before, value_args # 08 build_method_helper :klass, :finishing_after, value_args # 09 build_method_helper :klass, :finishing_before, value_args # 10 if threshold.present? build_method_helper :klass, :real_containing, value_args # 11 build_method_helper :klass, :real_overlapping, left_right_args # 12 build_method_helper :klass, :real_starting_after, value_args # 13 build_method_helper :klass, :real_starting_before, value_args # 14 build_method_helper :klass, :real_finishing_after, value_args # 15 build_method_helper :klass, :real_finishing_before, value_args # 16 end unless type.eql?(:daterange) build_method_helper :klass, :containing_date, value_args # 17 build_method_helper :klass, :not_containing_date, value_args # 18 build_method_helper :klass, :overlapping_date, left_right_args # 19 build_method_helper :klass, :not_overlapping_date, left_right_args # 20 if threshold.present? build_method_helper :klass, :real_containing_date, value_args # 21 build_method_helper :klass, :real_overlapping_date, left_right_args # 22 end end ## Instance methods build_method_helper :instance, :current? # 23 build_method_helper :instance, :current_on?, value_args # 24 build_method_helper :instance, :start # 25 build_method_helper :instance, :finish # 26 if threshold.present? build_method_helper :instance, :real # 27 build_method_helper :instance, :real_start # 28 build_method_helper :instance, :real_finish # 29 end klass.extend klass_module klass.include instance_module end |
#build_method_helper(type, key, args = []) ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 161 def build_method_helper(type, key, args = []) method_name = method_names[key] return if method_name.nil? method_content = send("#{type}_#{key}") method_content = define_string_method(method_name, method_content, args) source_module = send("#{type}_module") source_module.module_eval(method_content) end |
#conflicting? ⇒ Boolean
Check if any of the methods that will be created get in conflict with the base class methods
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 90 def conflicting? return if [:force] == true klass_method_names.values.each { |name| dangerous?(name, true) } instance_method_names.values.each { |name| dangerous?(name) } rescue Interrupt => err raise ArgumentError, <<-MSG.squish #{subtype.class.name} was not able to generate requested methods because the method #{err} already exists in #{klass.name}. MSG end |
#instance_method_names ⇒ Object
Get the list of methods associated withe the instances
84 85 86 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 84 def instance_method_names @instance_method_names ||= method_names.to_a[23..29].to_h end |
#klass_method_names ⇒ Object
Get the list of methods associated withe the class
79 80 81 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 79 def klass_method_names @klass_method_names ||= method_names.to_a[0..22].to_h end |
#method_names ⇒ Object
Generate all the method names
74 75 76 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 74 def method_names @method_names ||= default_method_names.merge(.fetch(:methods, {})) end |