Class: SimpleHdGraph::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-hd-graph/node.rb

Direct Known Subclasses

ContextNode, ResourceNode

Defined Under Namespace

Classes: RequiredFieldNotFilled

Constant Summary collapse

CAMELIZE_SEPARATOR =
/[ ,.、。]/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



18
19
20
# File 'lib/simple-hd-graph/node.rb', line 18

def initialize
  @inflector = Dry::Inflector.new
end

Class Method Details

.required(*names) ⇒ Object

Parameters:

  • names (Array)


11
12
13
# File 'lib/simple-hd-graph/node.rb', line 11

def required(*names)
  @required_fields = names
end

Instance Method Details

#camelize(str) ⇒ String

Parameters:

  • str (String)

Returns:

  • (String)


53
54
55
# File 'lib/simple-hd-graph/node.rb', line 53

def camelize(str)
  @inflector.camelize(@inflector.underscore(str.gsub(CAMELIZE_SEPARATOR, "_")))
end

#load(struct) ⇒ Object

Parameters:

  • struct (Hash)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simple-hd-graph/node.rb', line 25

def load(struct)
  klass = self.class

  required_fields =
    if klass.instance_variables.grep(/@required_fields/).size > 0
      klass.instance_variable_get(:@required_fields)
    end

  filled =
    if required_fields.is_a? Array
      required_fields.all? { |field|
        if struct.has_key? field
          true
        else
          raise RequiredFieldNotFilled, field
        end
      }
    else
      true
    end

  @content = struct if filled
end