Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Finished inheritance rule
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Feb 9, 2015
1 parent 0fc6b11 commit 2c2d1b5
Show file tree
Hide file tree
Showing 7 changed files with 517 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/drafter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fs = require 'fs'
class Drafter

# List of data structures
@dataStructures: []
@dataStructures: {}

# Default configuration
@defaultConfig:
Expand Down Expand Up @@ -41,10 +41,10 @@ class Drafter
protagonist.parse source, @config, (error, result) =>
callback error if error

ruleList = []
ruleList = ['mson-inheritance']
rules = (require './rules/' + rule for rule in ruleList)

@dataStructures = []
@dataStructures = {}

@expandNode result.ast, rules, 'blueprint'
callback error, result
Expand All @@ -63,15 +63,15 @@ class Drafter

if element.element is 'category'
for subElement in element.content
@dataStructures.push subElement if subElement.element is 'dataStructure'
@dataStructures[subElement.name.literal] = subElement if subElement.element is 'dataStructure'

# Expand the gathered data structures
for rule in rules
rule.dataStructures.call @dataStructures if 'dataStructures' in rule
rule.init.call rule, @dataStructures if 'dataStructures' in Object.keys(rule)

# Apply rules to the current node
for rule in rules
rule[elementType].call node, @dataStructures if elementType in rule
rule[elementType].call rule, node if elementType in Object.keys(rule)

# Recursively do the same for children nodes
switch elementType
Expand Down
85 changes: 85 additions & 0 deletions src/rules/mson-inheritance.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module.exports =

# Variables
expanded: {}
dataStructures: {}

# Copy all member types from one data structure to another
#
# @param supertTypeName [String] The name of the super type data structure
# @param memberTypeSection [Object] Member Type Section to be copied into
copyMembers: (superTypeName, memberTypeSection) ->
return if not @dataStructures[superTypeName]

for section in @dataStructures[superTypeName].sections
if section['class'] is 'memberType'

for member in section.content
memberTypeSection.content.push member if member['class'] is 'property'

# Given a data structure, expand it's inheritance recursively
#
# @param name [String] Name of the data structure
# @param dataStructure [Object] Data structure
expandInheritance: (name, dataStructure) ->
return if @expanded[name]

# Check for inheritance
superType = dataStructure.typeDefinition.typeSpecification.name

if typeof superType isnt 'object' or not superType?.literal
return @expanded[superType] = true

# Expand the super type first
@expandInheritance superType, @dataStructures[superType.literal]

# If super type is not an object
superTypeBaseName = @dataStructures[superType.literal].typeDefinition.typeSpecification.name

if superTypeBaseName isnt 'object'
dataStructure.typeDefinition.typeSpecification.name = superTypeBaseName
memberTypeSection =
content: []

memberTypeSection['class'] = 'memberType'
@copyMembers superType.literal, memberTypeSection

dataStructure.sections.push memberTypeSection if memberTypeSection.content.length
return @expanded[name] = true

# Find member type section of the current data structure
memberTypeSection = null
push = false

for section in dataStructure.sections
memberTypeSection = section if section['class'] is 'memberType'

# If no member type sections, create one
if not memberTypeSection
memberTypeSection =
content: []

memberTypeSection['class'] = 'memberType'
push = true

# Copy super-type and all the member types to sub type
dataStructure.typeDefinition.typeSpecification.name = superTypeBaseName
@copyMembers superType.literal, memberTypeSection

# Push the created type section
dataStructure.sections.push memberTypeSection if push and memberTypeSection.content.length

# Denote this type as expanded
@expanded[name] = true

init: (dataStructures) ->
@expanded = {}
@dataStructures = dataStructures

# Initiate flags
for name, dataStructure of @dataStructures
@expanded[name] = false

# Actual expansion
for name, dataStructure of @dataStructures
@expandInheritance name, dataStructure
3 changes: 3 additions & 0 deletions src/rules/mson-member-type-name.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports =

dataStructures: (dataStructures) ->
3 changes: 3 additions & 0 deletions src/rules/mson-mixin.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports =

dataStructures: (dataStructures) ->
34 changes: 34 additions & 0 deletions test/fixtures/dataStructures.apib
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FORMAT: 1A

# Stripe
Inspired by stripe API

# Data Structures

## Timestamp (number)
Unix timestamp as an integer

## Timestamps (object)
This object contains the following unix timestamps

+ created - Denoting the time when the object is created
+ modified - Denoting the time when the object has been recently updated

### Properties
+ created (Timestamp)

### Properties
+ modified (Timestamp)

## Coupon Base (Timestamps)
+ percent_off: 25 (number)

A positive integer between 1 and 100 that represents the discount the coupon will apply.

+ redeem_by (number) - Date after which the coupon can no longer be redeemed

## Coupon Base Clone (Coupon Base)
A clone of Coupon Base to be used for testing

## Timestamp Clone (Timestamp)
A clone of timestamp to be used for testing
Loading

0 comments on commit 2c2d1b5

Please sign in to comment.