Class: PgGraph::Reflection
- Inherits:
-
Object
- Object
- PgGraph::Reflection
- Defined in:
- lib/pg_graph/reflector.rb
Constant Summary collapse
- METHODS =
%w(match this that multi pluralize default_reflection).map(&:to_sym)
Instance Attribute Summary collapse
-
#components ⇒ Object
readonly
Number of name components (database, schema, table, column).
-
#default_reflection ⇒ Object
readonly
True if this is a default reflection.
-
#match ⇒ Object
readonly
Textual representation of match RE (String).
-
#multi ⇒ Object
readonly
If true or nil, the reflection applies to multiple references in a table to the same referenced table.
-
#pluralize ⇒ Object
readonly
Pluralize the result of #that if true.
-
#re ⇒ Object
readonly
RE corresponding to #match.
-
#that ⇒ Object
readonly
Template for ‘that’ field name or false if the field should not be included in the model.
-
#this ⇒ Object
readonly
Template for ‘this’ field name.
Class Method Summary collapse
-
.load_yaml(hash) ⇒ Object
hash
has the keys :match, :this, :that, and :pluralize.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #dump ⇒ Object
-
#initialize(match, this, that, multi = nil, pluralize = nil, default_reflection = false) ⇒ Reflection
constructor
this
andthat
are template strings, nil, or false. - #inspect ⇒ Object
-
#multi? ⇒ Boolean
True if this reflection applies to multiple references in a table.
- #to_yaml ⇒ Object
Constructor Details
#initialize(match, this, that, multi = nil, pluralize = nil, default_reflection = false) ⇒ Reflection
this
and that
are template strings, nil, or false
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pg_graph/reflector.rb', line 56 def initialize(match, this, that, multi = nil, pluralize = nil, default_reflection = false) constrain match, Regexp, String constrain this, String, NilClass constrain that, String, FalseClass, NilClass constrain multi, true, false, nil constrain pluralize, TrueClass, FalseClass, NilClass constrain default_reflection, TrueClass, FalseClass, NilClass @match = match.is_a?(Regexp) ? match.source : match if @match =~ /^\/(.*)\/$/ re = $1 @re = Regexp.new("^(?:\\w+\\.)*#{re}$") @components = re.scan(/(?<!\\)\\(?:\\\\)*\./).count + 1 else @re = Regexp.new("^(?:\\w+\\.)*#{@match}$") @components = @match.count(".") + 1 end (1..4).include?(@components) or raise "Illegal number of name components: #{@match}" @this = this @that = that @multi = multi @pluralize = pluralize @default_reflection = default_reflection || false end |
Instance Attribute Details
#components ⇒ Object (readonly)
Number of name components (database, schema, table, column). It can be between one and four. By ordering reflections from highest to lowest number of components, specific matches will be tested before more general matches
45 46 47 |
# File 'lib/pg_graph/reflector.rb', line 45 def components @components end |
#default_reflection ⇒ Object (readonly)
True if this is a default reflection
48 49 50 |
# File 'lib/pg_graph/reflector.rb', line 48 def default_reflection @default_reflection end |
#match ⇒ Object (readonly)
Textual representation of match RE (String)
14 15 16 |
# File 'lib/pg_graph/reflector.rb', line 14 def match @match end |
#multi ⇒ Object (readonly)
If true or nil, the reflection applies to multiple references in a table to the same referenced table. nil is used to mark the rule to not be included in the count of references. This is used to exclude explicit patterns like /^parent_id$/ that can co-exist with a another reference to the table
31 32 33 |
# File 'lib/pg_graph/reflector.rb', line 31 def multi @multi end |
#pluralize ⇒ Object (readonly)
Pluralize the result of #that if true. Default nil. Note that if pluralize is nil, the Reflector will pluralize unless the column is unique
36 37 38 |
# File 'lib/pg_graph/reflector.rb', line 36 def pluralize @pluralize end |
#re ⇒ Object (readonly)
RE corresponding to #match. #re always match a full UID
39 40 41 |
# File 'lib/pg_graph/reflector.rb', line 39 def re @re end |
#that ⇒ Object (readonly)
Template for ‘that’ field name or false if the field should not be included in the model. Can be nil
21 22 23 |
# File 'lib/pg_graph/reflector.rb', line 21 def that @that end |
#this ⇒ Object (readonly)
Template for ‘this’ field name. Can be nil
17 18 19 |
# File 'lib/pg_graph/reflector.rb', line 17 def this @this end |
Class Method Details
.load_yaml(hash) ⇒ Object
hash
has the keys :match, :this, :that, and :pluralize. The keys can also be strings
95 96 97 98 99 100 |
# File 'lib/pg_graph/reflector.rb', line 95 def self.load_yaml(hash) Reflection.new *METHODS.map { |key| value = hash[key].nil? ? hash[key.to_s] : hash[key] value == "nil" ? nil : value } end |
Instance Method Details
#==(other) ⇒ Object
87 88 89 |
# File 'lib/pg_graph/reflector.rb', line 87 def ==(other) METHODS.all? { |method| self.send(method) == other.send(method) } end |
#dump ⇒ Object
50 51 52 53 |
# File 'lib/pg_graph/reflector.rb', line 50 def dump puts "Reflection" indent { putv :match, :re, :this, :that, :multi?, :pluralize, :components } end |
#inspect ⇒ Object
91 |
# File 'lib/pg_graph/reflector.rb', line 91 def inspect() "#<Reflection #{match}>" end |
#multi? ⇒ Boolean
True if this reflection applies to multiple references in a table
24 |
# File 'lib/pg_graph/reflector.rb', line 24 def multi? = multi || multi.nil? |
#to_yaml ⇒ Object
80 81 82 83 84 85 |
# File 'lib/pg_graph/reflector.rb', line 80 def to_yaml { match: match, this: this, that: that, multi: multi, pluralize: pluralize, default_reflection: default_reflection } end |