Module: ObjectSugar

Defined in:
lib/object_sugar/version.rb,
lib/object_sugar/object_sugar.rb

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.extend_class_and_module_with_bitwise_enums!Object

Extend with #bitwise_enums



150
151
152
153
154
155
156
157
158
159
# File 'lib/object_sugar/object_sugar.rb', line 150

def self.extend_class_and_module_with_bitwise_enums!
  [Class, Module].each do |object|
    object.class_eval do
      def bitwise_enums(name, *args)
        create_enum(name, 2, *args )
        create_pluralized_constant(name)
      end
    end
  end
end

.extend_class_and_module_with_constants!Object

Extend with #object_constants



122
123
124
125
126
127
128
129
130
131
# File 'lib/object_sugar/object_sugar.rb', line 122

def self.extend_class_and_module_with_constants!
  [Class, Module].each do |object|
    object.class_eval do
      def object_constants(name, *args)
        create_constants(name, *args)
        create_pluralized_constant(name)
      end
    end
  end
end

.extend_class_and_module_with_value_enums!Object

Extend with #value_enums



136
137
138
139
140
141
142
143
144
145
# File 'lib/object_sugar/object_sugar.rb', line 136

def self.extend_class_and_module_with_value_enums!
  [Class, Module].each do |object|
    object.class_eval do
      def value_enums(name, *args)
        create_enum(name, 1, *args)
        create_pluralized_constant(name)
      end
    end
  end
end

.extend_object_with_constant_helpers!Object

Extend object with helper methods to create class/module constants



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/object_sugar/object_sugar.rb', line 16

def self.extend_object_with_constant_helpers!
  Object.class_eval do
    private

    ##
    # Creates a class +name+ and creates constants for each +args+
    #
    # Also accepts hashes as key/value pairs for constant
    #
    # class Foo
    #   attr_accessor :my_constants
    #   
    #   object_constants :my_constants, :one, :two, :three => "tre", :define_predicates => true
    # end
    #
    # Will create the following constants, values on class Foo
    #
    # Foo::MyConstants::ONE   => ONE
    # Foo::MyConstants::TWO   => TWO
    # Foo::MyConstants::THREE => tre
    #
    # foo = Foo.new
    # foo.my_constants_one? => false
    # foo.my_consatnts = Foo::MyConstants::ONE
    # foo.my_constants_one? => true

    def create_constants(name, *args)
      const = name.to_s.camelize.to_sym
      klass = const_defined?(const) ? const_get(const) : const_set(name.to_s.camelize.to_sym, Class.new)
      klass.extend(ObjectSugar::InstanceMethods)

      define_predicates = args.last.is_a?(Hash) ? args.last.delete(:define_predicates) : false
      
      args.flatten.each do |enum|
        if enum.is_a?(Hash)
          enum.each { |key, value| klass.const_set(key.to_s.underscore.upcase, value) }
        else
          key, value = Array.new(2, enum.to_s.underscore.upcase)
          klass.const_set(key, value)
        end
      end

      if define_predicates
        klass.constants.each do |constant|
          define_method("#{name}_#{constant.underscore}?") { send(name) == klass.const_get(constant) }
        end
      end

      klass
    end

    ##
    # Creates a class +name+ on self and creates a simple enumeration whereas
    # each subsequent argument in +args+ has a value +factor+^index

    def create_enum(name, factor, *args)
      const = name.to_s.camelize.to_sym
      klass = const_defined?(const) ? const_get(const) : const_set(name.to_s.camelize.to_sym, Class.new)
      klass.extend ObjectSugar::InstanceMethods

      define_predicates = args.extract_options!.delete(:define_predicates)
      
      offset = klass.constants.size

      args.flatten.each_with_index do |const, index|
        value = (factor == 1) ? (factor * (index + offset)) : (factor.power!(index + offset))

        klass.const_set(const.to_s.underscore.upcase, value.to_i)
      end

      if define_predicates
        klass.constants.each do |constant|
          define_method("#{name}_#{constant.underscore}?") { send(name) == klass.const_get(constant) }
        end
      end

      klass
    end

    ##
    # Create a constant with a name of the pluralized version of +name+
    # that returns all of the class constants of +name+
    #
    # class Foo
    #   object_constants :my_constants, :one, :two, :three
    # end
    #
    # Will add the following constant on class Foo
    #
    # Foo::MY_CONSTANTS
    #
    # Which would return
    # => ONE, TWO, THREE

    def create_pluralized_constant(name)
      pluralized = ActiveSupport::Inflector.pluralize(name.to_s).upcase.to_sym
      constants  = const_get(name.to_s.camelize.to_sym).constants

      const_set(pluralized, constants)
    end
  end
end

.setup!Object

Setup



6
7
8
9
10
11
# File 'lib/object_sugar/object_sugar.rb', line 6

def self.setup!
  extend_object_with_constant_helpers!
  extend_class_and_module_with_constants!
  extend_class_and_module_with_value_enums!
  extend_class_and_module_with_bitwise_enums!
end