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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 27 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.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def attribute @attribute end |
#current_getter ⇒ Object
Returns the value of attribute current_getter.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def current_getter @current_getter end |
#default ⇒ Object
Returns the value of attribute default.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def default @default end |
#dynamic_threshold ⇒ Object
Returns the value of attribute dynamic_threshold.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def dynamic_threshold @dynamic_threshold end |
#instance_module ⇒ Object
Returns the value of attribute instance_module.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def instance_module @instance_module end |
#klass ⇒ Object
Returns the value of attribute klass.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def klass @klass end |
#klass_module ⇒ Object
Returns the value of attribute klass_module.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def klass_module @klass_module end |
#options ⇒ Object
Returns the value of attribute options.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def @options end |
#threshold ⇒ Object
Check if can identify a threshold field
45 46 47 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 45 def threshold @threshold end |
#type ⇒ Object
Returns the value of attribute type.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def type @type end |
#type_caster ⇒ Object
Returns the value of attribute type_caster.
22 23 24 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 22 def type_caster @type_caster end |
Instance Method Details
#build ⇒ Object
Create all methods needed
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 102 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
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 159 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
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 88 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
82 83 84 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 82 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
77 78 79 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 77 def klass_method_names @klass_method_names ||= method_names.to_a[0..22].to_h end |
#method_names ⇒ Object
Generate all the method names
72 73 74 |
# File 'lib/torque/postgresql/attributes/builder/period.rb', line 72 def method_names @method_names ||= default_method_names.merge(.fetch(:methods, {})) end |