Class: Cockroach::Base::Node

Inherits:
Object
  • Object
show all
Includes:
LoadNodes, NodeStructure
Defined in:
lib/cockroach/base/node.rb

Overview

Node deals only with a specific records class. It makes sure that fixtures are created specific amount of time and will make sure that the associations are correctly assigned.

Direct Known Subclasses

FactoryGirl::Node

Constant Summary collapse

APPROACHES =
%w(amount ratio).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeStructure

#[]

Constructor Details

#initialize(*opts) ⇒ Node

Returns a new instance of Node.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cockroach/base/node.rb', line 49

def initialize *opts
  node_name, @structure, @options = extract_params_from_attrs *opts
  @name, @approach = self.class.extract_info(node_name.dup)
  @ids = []
  @source = define_source if @structure.is_a?(Hash)

  if @approach.blank?
    extract_options
    complicated_approch
    load_nodes
  else
    simple_approach
  end

  raise InvalideStructureError.new("Approch was not specified") if @approach.blank?
  raise MissingFixtureError.new("Approch was not specified") if @approach.blank?
  
  after_initialize
end

Instance Attribute Details

#approachObject (readonly)

Returns the value of attribute approach.



45
46
47
# File 'lib/cockroach/base/node.rb', line 45

def approach
  @approach
end

#idsObject (readonly)

Returns the value of attribute ids.



45
46
47
# File 'lib/cockroach/base/node.rb', line 45

def ids
  @ids
end

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/cockroach/base/node.rb', line 45

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



45
46
47
# File 'lib/cockroach/base/node.rb', line 45

def source
  @source
end

Class Method Details

.extract_info(symbol_or_string) ⇒ Object

Deduct the information from Node instance name: will try to get:

name: compulsary
approach: optional

Returns array containing name and approach

Example:

# no apprach
["users", nil]

# ratio apprach
["users", "ratio"]


37
38
39
40
41
42
# File 'lib/cockroach/base/node.rb', line 37

def extract_info symbol_or_string
  symbol_or_string = symbol_or_string.to_s
  approach = (symbol_or_string.match(/_(#{APPROACHES.join('|')})$/)[1] rescue nil)
  symbol_or_string.gsub!(/(_#{APPROACHES.join('|_')})$/,'') unless approach.blank?
  [symbol_or_string.to_s.singularize, approach]
end

.valid_structure?(structure) ⇒ Boolean

Method validating structure for Node operations. It will only accepts a structure if it is a Hash object, with one key only. In any other case a false value will be returned

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/cockroach/base/node.rb', line 19

def valid_structure? structure
  structure.is_a?(Hash) && structure.keys.size == 1 ||
    structure.is_a?(Array) && structure.size == 2
end

Instance Method Details

#after_initializeObject

Just in case Fixturer requires anything to be done after the node initialization



70
71
# File 'lib/cockroach/base/node.rb', line 70

def after_initialize
end

#amountObject

Keeps the number, that represents the amount of the records that will created for this node. This will be the final number of the records that are going to be created. In case there was a ratio specified, that method will randomly get a number within the provided range and keep it.



78
79
80
# File 'lib/cockroach/base/node.rb', line 78

def amount
  @amount || get_random_amount
end

#node_nameObject

Returns an aliased node name. The one that will be used for keeping it in sup and sub structures



84
85
86
# File 'lib/cockroach/base/node.rb', line 84

def node_name
  @alias_as || @name
end