metaclass

Version, currently 0.1.13 versions

github.com/mosop/metaclass

A Crystal library for manipulating class-level definitions.

1 stars
0 dependents
License: MIT

Nothing has been indexed for 0.1.1 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  metaclass:
    github: mosop/metaclass
    version: ~> 0.1.1

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1.1. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

Metaclass

A Crystal library for manipulating class-level definitions.

Build Status

Installation

Add this to your application's shard.yml:

dependencies:
  metaclass:
    github: mosop/metaclass

Features

Class Property

require "metaclass/object"

class Misc
  metaclass!
  class_getter faces = {:smile => ":)", :frown => ":("}
  class_setter passcode = Random.rand(10000)
  class_property movie = "Jaws"
end

Misc.faces # => {:smile => ":)", :frown => ":("}
Misc.faces = {:smile => ":D", :frown => ":{"} # raises error (getter only)
Misc.passcode = Random.rand(10000)
Misc.passcode # raises error (setter only)
Misc.movie # => "Jaws"
Misc.movie = "La Strada"
Misc.movie # => "La Strada"

Predicate

require "metaclass/object"

class Misc
  metaclass!
  class_getter? exists = true
  class_setter? promise_broken = false
  class_property? happy = false, neg: unhappy
end

Misc.exists? # => true
Misc.exists! # => raises error (getter only)
Misc.promise_broken!
Misc.promise_broken? # raises error (setter only)
Misc.happy? # => false
Misc.happy!
Misc.happy? # => true
Misc.unhappy!
Misc.happy? # => false

Computed Property

require "metaclass/object"

class Constant
  metaclass!
  class_getter uuid : String { SecureRandom.uuid }
end

class Inconstant
  metaclass!
  class_getter uuid : String, memo: false { SecureRandom.uuid }
end

Constant.uuid == Constant.uuid # => true
Inconstant.uuid != Inconstant.uuid # => true

Variable Inheritance

require "metaclass/object"

class A
  metaclass!
  class_getter inheritance, Hash(Symbol, String) do
    {:a => "a"}
  end
end

class B < A
  metaclass!
  inherited_class_getter inheritance do |parent|
    parent.merge(:b => "b")
  end
end

A.inheritance # => {:a => "a"}
B.inheritance # => {:a => "a", :b => "b"}

Usage

require "metaclass/object"

class A
  metaclass!
end

Or more clean:

require "metaclass"

class A
  Metaclass.metaclass!
end

And see Features

Release Notes

  • v0.1.1
    • Predicate

Contributing

  1. Fork it ( https://github.com/mosop/metaclass/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • mosop - creator, maintainer