Class: Striuct Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/striuct.rb,
lib/striuct/structs.rb,
lib/striuct/singleton_class.rb,
lib/striuct/classmethods/fix.rb,
lib/striuct/classmethods/copy.rb,
lib/striuct/classmethods/enum.rb,
lib/striuct/classmethods/inner.rb,
lib/striuct/classmethods/macro.rb,
lib/striuct/classmethods/names.rb,
lib/striuct/classmethods/length.rb,
lib/striuct/classmethods/default.rb,
lib/striuct/instancemethods/cast.rb,
lib/striuct/instancemethods/enum.rb,
lib/striuct/instancemethods/lock.rb,
lib/striuct/instancemethods/to_s.rb,
lib/striuct/instancemethods/hashy.rb,
lib/striuct/classmethods/predicate.rb,
lib/striuct/classmethods/to_struct.rb,
lib/striuct/instancemethods/assign.rb,
lib/striuct/instancemethods/getter.rb,
lib/striuct/instancemethods/object.rb,
lib/striuct/instancemethods/safety.rb,
lib/striuct/instancemethods/setter.rb,
lib/striuct/instancemethods/values.rb,
lib/striuct/classmethods/adjustment.rb,
lib/striuct/classmethods/attributes.rb,
lib/striuct/classmethods/validation.rb,
lib/striuct/instancemethods/compare.rb,
lib/striuct/instancemethods/default.rb,
lib/striuct/classmethods/constructor.rb,
lib/striuct/instancemethods/validation.rb,
lib/striuct/instancemethods/requirements.rb,
lib/striuct/classmethods/conflict_management.rb,
lib/striuct/classmethods/hashdeepdupulicatable.rb,
lib/striuct/instancemethods/delegate_class_methods.rb

Overview

This class is abstract.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, Structs Classes: Error, InvalidAdjustingError, InvalidOperationError, InvalidReadingError, InvalidValueError, InvalidWritingError

Class Method Summary collapse

Class Method Details

.adjustable?(object) ⇒ Boolean

Return `true` if given object is sufficient as an adjuster role

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/striuct/singleton_class.rb', line 42

def adjustable?(object)
  case object
  when Proc
    object.arity == 1
  else
    if object.respond_to?(:to_proc)
      object.to_proc.arity == 1
    else
      false
    end
  end
end

.define(&block) ⇒ void

This method returns an undefined value.

Yield Returns:

  • (Class)

    (see Striuct.new) - reject floating class

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/striuct/singleton_class.rb', line 29

def define(&block)
  raise ArgumentError, 'block not supplied' unless block

  new(&block).tap { |subclass|
    subclass.class_eval {
      raise 'not yet finished' if @autonyms.empty?

      close
    }
  }
end

.new(*autonyms, &block) ⇒ Class

Parameters:

  • autonyms (Symbol, String)

Yield Returns:

  • (Class)

Returns:

  • (Class)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/striuct/singleton_class.rb', line 11

def new(*autonyms, &block)
  # warning for Ruby's Struct.new user
  first = autonyms.first
  if first.instance_of?(String) && /\A[A-Z]/ =~ first
    warn("no define constant first-arg(#{first}), the Struct behavior is not supported in Striuct")
  end

  Class.new(self) {
    autonyms.each do |autonym|
      add_member autonym
    end

    class_eval(&block) if block
  }
end

.new_instanceObject



5
# File 'lib/striuct/singleton_class.rb', line 5

alias_method :new_instance, :new