Class: DataShift::MethodDetail
- Includes:
- Logging
- Defined in:
- lib/datashift/method_detail.rb
Instance Attribute Summary collapse
-
#col_type ⇒ Object
readonly
The rel col type from the DB.
-
#column_index ⇒ Object
Returns the value of attribute column_index.
-
#find_by_operator ⇒ Object
TODO make it a list/primary keys Additional helpers for where clauses.
-
#find_by_value ⇒ Object
TODO make it a list/primary keys Additional helpers for where clauses.
-
#klass ⇒ Object
Klass is the class of the ‘parent’ object i.e with the associations, For example Product which may have operator orders.
-
#name ⇒ Object
Name is the raw, client supplied name e.g Orders.
-
#operator ⇒ Object
readonly
The :operator that can be called to assign e.g orders or Products.new.orders << Order.new.
-
#operator_type ⇒ Object
readonly
The :operator that can be called to assign e.g orders or Products.new.orders << Order.new.
Class Method Summary collapse
- .association_types_enum ⇒ Object
- .is_association_type?(type) ⇒ Boolean
- .supported_types_enum ⇒ Object
Instance Method Summary collapse
-
#initialize(client_name, klass, operator, type, col_types = {}, find_by_operator = nil, find_by_value = nil) ⇒ MethodDetail
constructor
Store the raw (client supplied) name against the active record klass(model).
- #operator?(name, case_sensitive = false) ⇒ Boolean
-
#operator_class ⇒ Object
Return the operator’s expected class, if can be derived, else nil.
-
#operator_class_name ⇒ Object
Return the operator’s expected class name, if can be derived, else nil.
-
#operator_for(type) ⇒ Object
Return the actual operator’s name for supplied method type where type one of :assignment, :has_one, :belongs_to, :has_many etc.
- #pp ⇒ Object
Methods included from Logging
Constructor Details
#initialize(client_name, klass, operator, type, col_types = {}, find_by_operator = nil, find_by_value = nil) ⇒ MethodDetail
Store the raw (client supplied) name against the active record klass(model). Operator is the associated method call on klass, i.e client supplies name ‘Price’ in a spreadsheet, but true operator to call on klass is price
col_types can typically be derived from klass.columns - set of ActiveRecord::ConnectionAdapters::Column
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/datashift/method_detail.rb', line 64 def initialize(client_name, klass, operator, type, col_types = {}, find_by_operator = nil, find_by_value = nil ) @klass, @name = klass, client_name @find_by_operator = find_by_operator @find_by_value = find_by_value if( MethodDetail::supported_types_enum.member?(type.to_sym) ) @operator_type = type.to_sym else raise "Bad operator Type #{type} passed to Method Detail" end @operator = operator # Note : Not all assignments will currently have a column type, for example # those that are derived from a delegate_belongs_to if(col_types.empty?) @col_type = klass.columns.find{ |col| col.name == operator } else @col_type = col_types[operator] end @column_index = -1 end |
Instance Attribute Details
#col_type ⇒ Object (readonly)
The rel col type from the DB
46 47 48 |
# File 'lib/datashift/method_detail.rb', line 46 def col_type @col_type end |
#column_index ⇒ Object
Returns the value of attribute column_index.
43 44 45 |
# File 'lib/datashift/method_detail.rb', line 43 def column_index @column_index end |
#find_by_operator ⇒ Object
TODO make it a list/primary keys Additional helpers for where clauses
55 56 57 |
# File 'lib/datashift/method_detail.rb', line 55 def find_by_operator @find_by_operator end |
#find_by_value ⇒ Object
TODO make it a list/primary keys Additional helpers for where clauses
55 56 57 |
# File 'lib/datashift/method_detail.rb', line 55 def find_by_value @find_by_value end |
#klass ⇒ Object
Klass is the class of the ‘parent’ object i.e with the associations, For example Product which may have operator orders
39 40 41 |
# File 'lib/datashift/method_detail.rb', line 39 def klass @klass end |
#name ⇒ Object
Name is the raw, client supplied name e.g Orders
42 43 44 |
# File 'lib/datashift/method_detail.rb', line 42 def name @name end |
#operator ⇒ Object (readonly)
The :operator that can be called to assign e.g orders or Products.new.orders << Order.new
The type of operator e.g :assignment, :belongs_to, :has_one, :has_many etc
51 52 53 |
# File 'lib/datashift/method_detail.rb', line 51 def operator @operator end |
#operator_type ⇒ Object (readonly)
The :operator that can be called to assign e.g orders or Products.new.orders << Order.new
The type of operator e.g :assignment, :belongs_to, :has_one, :has_many etc
51 52 53 |
# File 'lib/datashift/method_detail.rb', line 51 def operator_type @operator_type end |
Class Method Details
.association_types_enum ⇒ Object
26 27 28 29 |
# File 'lib/datashift/method_detail.rb', line 26 def self.association_types_enum @assoc_type_enum ||= Set[:belongs_to, :has_one, :has_many] @assoc_type_enum end |
.is_association_type?(type) ⇒ Boolean
32 33 34 |
# File 'lib/datashift/method_detail.rb', line 32 def self.is_association_type? ( type ) association_types_enum.member?( type ) end |
.supported_types_enum ⇒ Object
21 22 23 24 |
# File 'lib/datashift/method_detail.rb', line 21 def self.supported_types_enum @type_enum ||= Set[:assignment, :belongs_to, :has_one, :has_many] @type_enum end |
Instance Method Details
#operator?(name, case_sensitive = false) ⇒ Boolean
96 97 98 |
# File 'lib/datashift/method_detail.rb', line 96 def operator?(name, case_sensitive = false) case_sensitive ? operator == name : operator.downcase == name.downcase end |
#operator_class ⇒ Object
Return the operator’s expected class, if can be derived, else nil
117 118 119 120 |
# File 'lib/datashift/method_detail.rb', line 117 def operator_class() @operator_class ||= get_operator_class() @operator_class end |
#operator_class_name ⇒ Object
Return the operator’s expected class name, if can be derived, else nil
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/datashift/method_detail.rb', line 101 def operator_class_name() @operator_class_name ||= if(operator_for(:has_many) || operator_for(:belongs_to) || operator_for(:has_one)) get_operator_class.name elsif(@col_type) @col_type.type.to_s.classify else "" end @operator_class_name end |
#operator_for(type) ⇒ Object
Return the actual operator’s name for supplied method type where type one of :assignment, :has_one, :belongs_to, :has_many etc
91 92 93 94 |
# File 'lib/datashift/method_detail.rb', line 91 def operator_for( type ) return operator if(@operator_type == type.to_sym) nil end |
#pp ⇒ Object
122 123 124 |
# File 'lib/datashift/method_detail.rb', line 122 def pp "#{@name} => #{operator}" end |